blob: 052f59c1917f4f477420f9df0504a49a6c46d43a [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);
Mark Brownaaee8ef2011-01-26 20:53:50 +0000314
Mark Brown2624d5f2009-11-03 21:56:13 +0000315 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200316 codec->component.debugfs_root,
Mark Brown2624d5f2009-11-03 21:56:13 +0000317 codec, &codec_reg_fops);
318 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200319 dev_warn(codec->dev,
320 "ASoC: Failed to create codec register debugfs file\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000321}
322
Mark Brownc3c5a192010-09-15 18:15:14 +0100323static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
325{
326 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100327 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100328 struct snd_soc_codec *codec;
329
330 if (!buf)
331 return -ENOMEM;
332
Mark Brown2b194f9d2010-10-13 10:52:16 +0100333 list_for_each_entry(codec, &codec_list, list) {
334 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200335 codec->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100336 if (len >= 0)
337 ret += len;
338 if (ret > PAGE_SIZE) {
339 ret = PAGE_SIZE;
340 break;
341 }
342 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100343
344 if (ret >= 0)
345 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
346
347 kfree(buf);
348
349 return ret;
350}
351
352static const struct file_operations codec_list_fops = {
353 .read = codec_list_read_file,
354 .llseek = default_llseek,/* read accesses f_pos */
355};
356
Mark Brownf3208782010-09-15 18:19:07 +0100357static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
358 size_t count, loff_t *ppos)
359{
360 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100361 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100362 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100363 struct snd_soc_dai *dai;
364
365 if (!buf)
366 return -ENOMEM;
367
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100368 list_for_each_entry(component, &component_list, list) {
369 list_for_each_entry(dai, &component->dai_list, list) {
370 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
371 dai->name);
372 if (len >= 0)
373 ret += len;
374 if (ret > PAGE_SIZE) {
375 ret = PAGE_SIZE;
376 break;
377 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100378 }
379 }
Mark Brownf3208782010-09-15 18:19:07 +0100380
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100382
383 kfree(buf);
384
385 return ret;
386}
387
388static const struct file_operations dai_list_fops = {
389 .read = dai_list_read_file,
390 .llseek = default_llseek,/* read accesses f_pos */
391};
392
Mark Brown19c7ac22010-09-15 18:22:40 +0100393static ssize_t platform_list_read_file(struct file *file,
394 char __user *user_buf,
395 size_t count, loff_t *ppos)
396{
397 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100398 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100399 struct snd_soc_platform *platform;
400
401 if (!buf)
402 return -ENOMEM;
403
Mark Brown2b194f9d2010-10-13 10:52:16 +0100404 list_for_each_entry(platform, &platform_list, list) {
405 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200406 platform->component.name);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 if (len >= 0)
408 ret += len;
409 if (ret > PAGE_SIZE) {
410 ret = PAGE_SIZE;
411 break;
412 }
413 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100414
Mark Brown2b194f9d2010-10-13 10:52:16 +0100415 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100416
417 kfree(buf);
418
419 return ret;
420}
421
422static const struct file_operations platform_list_fops = {
423 .read = platform_list_read_file,
424 .llseek = default_llseek,/* read accesses f_pos */
425};
426
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200427static void soc_init_card_debugfs(struct snd_soc_card *card)
428{
429 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000430 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200431 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200432 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100433 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200434 return;
435 }
436
437 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
438 card->debugfs_card_root,
439 &card->pop_time);
440 if (!card->debugfs_pop_time)
441 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000442 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200443}
444
445static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
446{
447 debugfs_remove_recursive(card->debugfs_card_root);
448}
449
Mark Brown2624d5f2009-11-03 21:56:13 +0000450#else
451
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200452#define soc_init_codec_debugfs NULL
453
454static inline void soc_init_component_debugfs(
455 struct snd_soc_component *component)
Mark Brown2624d5f2009-11-03 21:56:13 +0000456{
457}
458
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +0200459static inline void soc_cleanup_component_debugfs(
460 struct snd_soc_component *component)
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000461{
462}
463
Axel Linb95fccb2010-11-09 17:06:44 +0800464static inline void soc_init_card_debugfs(struct snd_soc_card *card)
465{
466}
467
468static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
469{
470}
Mark Brown2624d5f2009-11-03 21:56:13 +0000471#endif
472
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100473struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
474 const char *dai_link, int stream)
475{
476 int i;
477
478 for (i = 0; i < card->num_links; i++) {
479 if (card->rtd[i].dai_link->no_pcm &&
480 !strcmp(card->rtd[i].dai_link->name, dai_link))
481 return card->rtd[i].pcm->streams[stream].substream;
482 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000483 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100484 return NULL;
485}
486EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
487
488struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
489 const char *dai_link)
490{
491 int i;
492
493 for (i = 0; i < card->num_links; i++) {
494 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
495 return &card->rtd[i];
496 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000497 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100498 return NULL;
499}
500EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
501
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200502#ifdef CONFIG_SND_SOC_AC97_BUS
503/* unregister ac97 codec */
504static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
505{
506 if (codec->ac97->dev.bus)
507 device_unregister(&codec->ac97->dev);
508 return 0;
509}
510
511/* stop no dev release warning */
512static void soc_ac97_device_release(struct device *dev){}
513
514/* register ac97 codec to bus */
515static int soc_ac97_dev_register(struct snd_soc_codec *codec)
516{
517 int err;
518
519 codec->ac97->dev.bus = &ac97_bus_type;
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200520 codec->ac97->dev.parent = codec->component.card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200521 codec->ac97->dev.release = soc_ac97_device_release;
522
Kay Sieversbb072bf2008-11-02 03:50:35 +0100523 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Lars-Peter Clausen00200102014-07-17 22:01:07 +0200524 codec->component.card->snd_card->number, 0,
525 codec->component.name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200526 err = device_register(&codec->ac97->dev);
527 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000528 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200529 codec->ac97->dev.bus = NULL;
530 return err;
531 }
532 return 0;
533}
534#endif
535
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100536static void codec2codec_close_delayed_work(struct work_struct *work)
537{
538 /* Currently nothing to do for c2c links
539 * Since c2c links are internal nodes in the DAPM graph and
540 * don't interface with the outside world or application layer
541 * we don't have to do any special handling on close.
542 */
543}
544
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000545#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200546/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000547int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200548{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000549 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200550 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200551 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200553 /* If the card is not initialized yet there is nothing to do */
554 if (!card->instantiated)
Daniel Macke3509ff2009-06-03 17:44:49 +0200555 return 0;
556
Andy Green6ed25972008-06-13 16:24:05 +0100557 /* Due to the resume being scheduled into a workqueue we could
558 * suspend before that's finished - wait for it to complete.
559 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000560 snd_power_lock(card->snd_card);
561 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
562 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100563
564 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000565 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100566
Mark Browna00f90f2010-12-02 16:24:24 +0000567 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100569
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000570 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100571 continue;
572
Benoit Cousson88bd8702014-07-08 23:19:34 +0200573 for (j = 0; j < card->rtd[i].num_codecs; j++) {
574 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
575 struct snd_soc_dai_driver *drv = dai->driver;
576
577 if (drv->ops->digital_mute && dai->playback_active)
578 drv->ops->digital_mute(dai, 1);
579 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200580 }
581
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100582 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 for (i = 0; i < card->num_rtd; i++) {
584 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100585 continue;
586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100588 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100589
Mark Brown87506542008-11-18 20:50:34 +0000590 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000591 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000593 for (i = 0; i < card->num_rtd; i++) {
594 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
595 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100596
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000597 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100598 continue;
599
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000600 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
601 cpu_dai->driver->suspend(cpu_dai);
602 if (platform->driver->suspend && !platform->suspended) {
603 platform->driver->suspend(cpu_dai);
604 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100605 }
606 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200607
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000608 /* close any waiting streams and save state */
609 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200610 struct snd_soc_dai **codec_dais = card->rtd[i].codec_dais;
Tejun Heo43829732012-08-20 14:51:24 -0700611 flush_delayed_work(&card->rtd[i].delayed_work);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200612 for (j = 0; j < card->rtd[i].num_codecs; j++) {
613 codec_dais[j]->codec->dapm.suspend_bias_level =
614 codec_dais[j]->codec->dapm.bias_level;
615 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000616 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100617
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000618 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619
620 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100621 continue;
622
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800623 snd_soc_dapm_stream_event(&card->rtd[i],
624 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800625 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800627 snd_soc_dapm_stream_event(&card->rtd[i],
628 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800629 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000630 }
631
Mark Browne2d32ff2012-08-31 17:38:32 -0700632 /* Recheck all analogue paths too */
633 dapm_mark_io_dirty(&card->dapm);
634 snd_soc_dapm_sync(&card->dapm);
635
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000636 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200637 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 /* If there are paths active then the CODEC will be held with
639 * bias _ON and should not be suspended. */
640 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200641 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000642 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000643 /*
644 * If the CODEC is capable of idle
645 * bias off then being in STANDBY
646 * means it's doing something,
647 * otherwise fall through.
648 */
649 if (codec->dapm.idle_bias_off) {
650 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200651 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000652 break;
653 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000654 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100655 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900657 codec->cache_sync = 1;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +0200658 if (codec->component.regmap)
659 regcache_mark_dirty(codec->component.regmap);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800660 /* deactivate pins to sleep state */
661 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 break;
663 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200664 dev_dbg(codec->dev,
665 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000666 break;
667 }
668 }
669 }
670
671 for (i = 0; i < card->num_rtd; i++) {
672 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
673
674 if (card->rtd[i].dai_link->ignore_suspend)
675 continue;
676
677 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
678 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800679
680 /* deactivate pins to sleep state */
681 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200682 }
683
Mark Brown87506542008-11-18 20:50:34 +0000684 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000685 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200686
687 return 0;
688}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000689EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Andy Green6ed25972008-06-13 16:24:05 +0100691/* deferred resume work, so resume can complete before we finished
692 * setting our codec back up, which can be very slow on I2C
693 */
694static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200695{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000696 struct snd_soc_card *card =
697 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200698 struct snd_soc_codec *codec;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200699 int i, j;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700
Andy Green6ed25972008-06-13 16:24:05 +0100701 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
702 * so userspace apps are blocked from touching us
703 */
704
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000705 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100706
Mark Brown99497882010-05-07 20:24:05 +0100707 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000708 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100709
Mark Brown87506542008-11-18 20:50:34 +0000710 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000711 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200712
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000713 /* resume AC97 DAIs */
714 for (i = 0; i < card->num_rtd; i++) {
715 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100716
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000717 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100718 continue;
719
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000720 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
721 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200722 }
723
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200724 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 /* If the CODEC was idle over suspend then it will have been
726 * left with bias OFF or STANDBY and suspended so we must now
727 * resume. Otherwise the suspend was suppressed.
728 */
729 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200730 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000731 case SND_SOC_BIAS_STANDBY:
732 case SND_SOC_BIAS_OFF:
733 codec->driver->resume(codec);
734 codec->suspended = 0;
735 break;
736 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200737 dev_dbg(codec->dev,
738 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 break;
740 }
Mark Brown1547aba2010-05-07 21:11:40 +0100741 }
742 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200743
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000744 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100745
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000746 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100747 continue;
748
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800749 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000750 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800751 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752
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_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800755 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200756 }
757
Mark Brown3ff3f642008-05-19 12:32:25 +0200758 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100760
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000761 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100762 continue;
763
Benoit Cousson88bd8702014-07-08 23:19:34 +0200764 for (j = 0; j < card->rtd[i].num_codecs; j++) {
765 struct snd_soc_dai *dai = card->rtd[i].codec_dais[j];
766 struct snd_soc_dai_driver *drv = dai->driver;
767
768 if (drv->ops->digital_mute && dai->playback_active)
769 drv->ops->digital_mute(dai, 0);
770 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200771 }
772
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000773 for (i = 0; i < card->num_rtd; i++) {
774 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
775 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100776
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000777 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100778 continue;
779
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000780 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
781 cpu_dai->driver->resume(cpu_dai);
782 if (platform->driver->resume && platform->suspended) {
783 platform->driver->resume(cpu_dai);
784 platform->suspended = 0;
785 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200786 }
787
Mark Brown87506542008-11-18 20:50:34 +0000788 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000789 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200790
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000791 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100792
793 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000794 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700795
796 /* Recheck all analogue paths too */
797 dapm_mark_io_dirty(&card->dapm);
798 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100799}
800
801/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000802int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100803{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000804 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600805 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200806
Lars-Peter Clausenc5599b82014-08-19 15:51:30 +0200807 /* If the card is not initialized yet there is nothing to do */
808 if (!card->instantiated)
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800809 return 0;
810
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800811 /* activate pins from sleep state */
812 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +0200813 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
814 struct snd_soc_dai **codec_dais = rtd->codec_dais;
815 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
816 int j;
817
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800818 if (cpu_dai->active)
819 pinctrl_pm_select_default_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200820
821 for (j = 0; j < rtd->num_codecs; j++) {
822 struct snd_soc_dai *codec_dai = codec_dais[j];
823 if (codec_dai->active)
824 pinctrl_pm_select_default_state(codec_dai->dev);
825 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800826 }
827
Mark Brown64ab9ba2009-03-31 11:27:03 +0100828 /* AC97 devices might have other drivers hanging off them so
829 * need to resume immediately. Other drivers don't have that
830 * problem and may take a substantial amount of time to resume
831 * due to I/O costs and anti-pop so handle them out of line.
832 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 for (i = 0; i < card->num_rtd; i++) {
834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 ac97_control |= cpu_dai->driver->ac97_control;
836 }
837 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000838 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600839 soc_resume_deferred(&card->deferred_resume_work);
840 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000841 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600842 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000843 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100844 }
Andy Green6ed25972008-06-13 16:24:05 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 return 0;
847}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000848EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000850#define snd_soc_suspend NULL
851#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852#endif
853
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100854static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800855};
856
Lars-Peter Clausen65d93612014-08-19 15:51:22 +0200857static struct snd_soc_component *soc_find_component(
858 const struct device_node *of_node, const char *name)
859{
860 struct snd_soc_component *component;
861
862 list_for_each_entry(component, &component_list, list) {
863 if (of_node) {
864 if (component->dev->of_node == of_node)
865 return component;
866 } else if (strcmp(component->name, name) == 0) {
867 return component;
868 }
869 }
870
871 return NULL;
872}
873
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200874static struct snd_soc_dai *snd_soc_find_dai(
875 const struct snd_soc_dai_link_component *dlc)
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100876{
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200877 struct snd_soc_component *component;
878 struct snd_soc_dai *dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100879
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200880 /* Find CPU DAI from registered DAIs*/
881 list_for_each_entry(component, &component_list, list) {
882 if (dlc->of_node && component->dev->of_node != dlc->of_node)
883 continue;
884 if (dlc->name && strcmp(dev_name(component->dev), dlc->name))
885 continue;
886 list_for_each_entry(dai, &component->dai_list, list) {
887 if (dlc->dai_name && strcmp(dai->name, dlc->dai_name))
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100888 continue;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100889
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200890 return dai;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100891 }
892 }
893
894 return NULL;
895}
896
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000897static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200898{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000899 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
900 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Benoit Cousson88bd8702014-07-08 23:19:34 +0200901 struct snd_soc_dai_link_component *codecs = dai_link->codecs;
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200902 struct snd_soc_dai_link_component cpu_dai_component;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200903 struct snd_soc_dai **codec_dais = rtd->codec_dais;
Mark Brown435c5e22008-12-04 15:32:53 +0000904 struct snd_soc_platform *platform;
Mark Brown848dd8b2011-04-27 18:16:32 +0100905 const char *platform_name;
Benoit Cousson88bd8702014-07-08 23:19:34 +0200906 int i;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200907
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000908 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000909
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200910 cpu_dai_component.name = dai_link->cpu_name;
911 cpu_dai_component.of_node = dai_link->cpu_of_node;
912 cpu_dai_component.dai_name = dai_link->cpu_dai_name;
913 rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component);
Mark Brownb19e6e72012-03-14 21:18:39 +0000914 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000915 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000916 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000918 }
919
Benoit Cousson88bd8702014-07-08 23:19:34 +0200920 rtd->num_codecs = dai_link->num_codecs;
921
922 /* Find CODEC from registered CODECs */
923 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen14621c72014-08-19 15:51:27 +0200924 codec_dais[i] = snd_soc_find_dai(&codecs[i]);
Benoit Cousson88bd8702014-07-08 23:19:34 +0200925 if (!codec_dais[i]) {
926 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
927 codecs[i].dai_name);
928 return -EPROBE_DEFER;
929 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000930 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100931
Benoit Cousson88bd8702014-07-08 23:19:34 +0200932 /* Single codec links expect codec and codec_dai in runtime data */
933 rtd->codec_dai = codec_dais[0];
934 rtd->codec = rtd->codec_dai->codec;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100935
Mark Brown848dd8b2011-04-27 18:16:32 +0100936 /* if there's no platform we match on the empty platform */
937 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700938 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100939 platform_name = "snd-soc-dummy";
940
Mark Brownb19e6e72012-03-14 21:18:39 +0000941 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000942 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700943 if (dai_link->platform_of_node) {
944 if (platform->dev->of_node !=
945 dai_link->platform_of_node)
946 continue;
947 } else {
Lars-Peter Clausenf4333202014-06-16 18:13:02 +0200948 if (strcmp(platform->component.name, platform_name))
Stephen Warren5a504962011-12-21 10:40:59 -0700949 continue;
950 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700951
952 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000953 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000954 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000955 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000956 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000957 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000958 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000959
960 card->num_rtd++;
961
962 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963}
964
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200965static void soc_remove_component(struct snd_soc_component *component)
Stephen Warrend12cd192012-06-08 12:34:22 -0600966{
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +0200967 if (!component->probed)
968 return;
969
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200970 /* This is a HACK and will be removed soon */
971 if (component->codec)
972 list_del(&component->codec->card_list);
Stephen Warrend12cd192012-06-08 12:34:22 -0600973
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200974 if (component->remove)
975 component->remove(component);
Stephen Warrend12cd192012-06-08 12:34:22 -0600976
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200977 snd_soc_dapm_free(snd_soc_component_get_dapm(component));
Stephen Warrend12cd192012-06-08 12:34:22 -0600978
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +0200979 soc_cleanup_component_debugfs(component);
980 component->probed = 0;
981 module_put(component->dev->driver->owner);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200982}
983
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200984static void soc_remove_dai(struct snd_soc_dai *dai, int order)
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100985{
986 int err;
987
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200988 if (dai && dai->probed &&
989 dai->driver->remove_order == order) {
990 if (dai->driver->remove) {
991 err = dai->driver->remove(dai);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100992 if (err < 0)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200993 dev_err(dai->dev,
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100994 "ASoC: failed to remove %s: %d\n",
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200995 dai->name, err);
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100996 }
Lars-Peter Clausene60cd142014-08-19 15:51:26 +0200997 dai->probed = 0;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +0100998 }
999}
1000
Stephen Warren62ae68f2012-06-08 12:34:23 -06001001static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002{
1003 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001004 int i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001005
1006 /* unregister the rtd device */
1007 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001008 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1009 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1010 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001011 rtd->dev_registered = 0;
1012 }
1013
1014 /* remove the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001015 for (i = 0; i < rtd->num_codecs; i++)
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001016 soc_remove_dai(rtd->codec_dais[i], order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001017
Lars-Peter Clausene60cd142014-08-19 15:51:26 +02001018 soc_remove_dai(rtd->cpu_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019}
1020
Stephen Warren62ae68f2012-06-08 12:34:23 -06001021static void soc_remove_link_components(struct snd_soc_card *card, int num,
1022 int order)
1023{
1024 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1025 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001026 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001027 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001028 int i;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001029
1030 /* remove the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001031 if (platform && platform->component.driver->remove_order == order)
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001032 soc_remove_component(&platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001033
1034 /* remove the CODEC-side CODEC */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001035 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001036 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001037 if (component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001038 soc_remove_component(component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039 }
1040
1041 /* remove any CPU-side CODEC */
1042 if (cpu_dai) {
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001043 if (cpu_dai->component->driver->remove_order == order)
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001044 soc_remove_component(cpu_dai->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001045 }
1046}
1047
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001048static void soc_remove_dai_links(struct snd_soc_card *card)
1049{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001050 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001051
Liam Girdwood0168bf02011-06-07 16:08:05 +01001052 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1053 order++) {
1054 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001055 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001056 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001057
1058 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1059 order++) {
1060 for (dai = 0; dai < card->num_rtd; dai++)
1061 soc_remove_link_components(card, dai, order);
1062 }
1063
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001064 card->num_rtd = 0;
1065}
1066
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001067static void soc_set_name_prefix(struct snd_soc_card *card,
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001068 struct snd_soc_component *component)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001069{
1070 int i;
1071
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001072 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001073 return;
1074
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001075 for (i = 0; i < card->num_configs; i++) {
1076 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001077 if (map->of_node && component->dev->of_node != map->of_node)
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001078 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001079 if (map->dev_name && strcmp(component->name, map->dev_name))
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001080 continue;
Lars-Peter Clausen94f99c82014-06-16 18:13:01 +02001081 component->name_prefix = map->name_prefix;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001082 break;
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001083 }
1084}
1085
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001086static int soc_probe_component(struct snd_soc_card *card,
1087 struct snd_soc_component *component)
Jarkko Nikula589c3562010-12-06 16:27:07 +02001088{
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001089 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Mark Brown888df392012-02-16 19:37:51 -08001090 struct snd_soc_dai *dai;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001091 int ret;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001092
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001093 if (component->probed)
1094 return 0;
1095
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001096 component->card = card;
1097 dapm->card = card;
1098 soc_set_name_prefix(card, component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001099
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001100 if (!try_module_get(component->dev->driver->owner))
Jarkko Nikula70d29332011-01-27 16:24:22 +02001101 return -ENODEV;
1102
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001103 soc_init_component_debugfs(component);
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001104
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001105 if (component->dapm_widgets) {
1106 ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets,
1107 component->num_dapm_widgets);
Nariman Poushinb318ad52014-04-01 13:59:33 +01001108
1109 if (ret != 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001110 dev_err(component->dev,
Nariman Poushinb318ad52014-04-01 13:59:33 +01001111 "Failed to create new controls %d\n", ret);
1112 goto err_probe;
1113 }
1114 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001115
Lars-Peter Clausen06348142014-08-20 13:08:49 +02001116 list_for_each_entry(dai, &component->dai_list, list) {
1117 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
1118 if (ret != 0) {
1119 dev_err(component->dev,
1120 "Failed to create DAI widgets %d\n", ret);
1121 goto err_probe;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001122 }
1123 }
Mark Brown33c5f962011-08-22 18:40:30 +01001124
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001125 if (component->probe) {
1126 ret = component->probe(component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001127 if (ret < 0) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001128 dev_err(component->dev,
1129 "ASoC: failed to probe component %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001130 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001131 }
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001132
1133 WARN(dapm->idle_bias_off &&
1134 dapm->bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001135 "codec %s can not start from non-off bias with idle_bias_off==1\n",
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001136 component->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001137 }
1138
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001139 if (component->controls)
1140 snd_soc_add_component_controls(component, component->controls,
1141 component->num_controls);
1142 if (component->dapm_routes)
1143 snd_soc_dapm_add_routes(dapm, component->dapm_routes,
1144 component->num_dapm_routes);
Mark Brown89b95ac02011-03-07 16:38:44 +00001145
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001146 component->probed = 1;
1147 list_add(&dapm->list, &card->dapm_list);
1148
1149 /* This is a HACK and will be removed soon */
1150 if (component->codec)
1151 list_add(&component->codec->card_list, &card->codec_dev_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001152
Jarkko Nikula70d29332011-01-27 16:24:22 +02001153 return 0;
1154
1155err_probe:
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001156 soc_cleanup_component_debugfs(component);
1157 module_put(component->dev->driver->owner);
Liam Girdwood956245e2011-07-01 16:54:08 +01001158
1159 return ret;
1160}
1161
Mark Brown36ae1a92012-01-06 17:12:45 -08001162static void rtd_release(struct device *dev)
1163{
1164 kfree(dev);
1165}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001166
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001167static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd,
1168 const char *name)
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001169{
Jarkko Nikula589c3562010-12-06 16:27:07 +02001170 int ret = 0;
1171
Jarkko Nikula589c3562010-12-06 16:27:07 +02001172 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001173 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1174 if (!rtd->dev)
1175 return -ENOMEM;
1176 device_initialize(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001177 rtd->dev->parent = rtd->card->dev;
Mark Brown36ae1a92012-01-06 17:12:45 -08001178 rtd->dev->release = rtd_release;
1179 rtd->dev->init_name = name;
1180 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001181 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001182 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1183 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1184 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1185 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001186 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001187 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001188 /* calling put_device() here to free the rtd->dev */
1189 put_device(rtd->dev);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001190 dev_err(rtd->card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001191 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001192 return ret;
1193 }
1194 rtd->dev_registered = 1;
1195
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001196 if (rtd->codec) {
1197 /* add DAPM sysfs entries for this codec */
1198 ret = snd_soc_dapm_sys_add(rtd->dev);
1199 if (ret < 0)
1200 dev_err(rtd->dev,
1201 "ASoC: failed to add codec dapm sysfs entries: %d\n",
1202 ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203
Lars-Peter Clausen93c3ce72014-08-19 15:51:20 +02001204 /* add codec sysfs entries */
1205 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
1206 if (ret < 0)
1207 dev_err(rtd->dev,
1208 "ASoC: failed to add codec sysfs files: %d\n",
1209 ret);
1210 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001211
1212 return 0;
1213}
1214
Stephen Warren62ae68f2012-06-08 12:34:23 -06001215static int soc_probe_link_components(struct snd_soc_card *card, int num,
1216 int order)
1217{
1218 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Stephen Warren62ae68f2012-06-08 12:34:23 -06001219 struct snd_soc_platform *platform = rtd->platform;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001220 struct snd_soc_component *component;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001221 int i, ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001222
1223 /* probe the CPU-side component, if it is a CODEC */
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001224 component = rtd->cpu_dai->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001225 if (component->driver->probe_order == order) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001226 ret = soc_probe_component(card, component);
1227 if (ret < 0)
1228 return ret;
Stephen Warren62ae68f2012-06-08 12:34:23 -06001229 }
1230
Benoit Cousson88bd8702014-07-08 23:19:34 +02001231 /* probe the CODEC-side components */
1232 for (i = 0; i < rtd->num_codecs; i++) {
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02001233 component = rtd->codec_dais[i]->component;
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001234 if (component->driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001235 ret = soc_probe_component(card, component);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001236 if (ret < 0)
1237 return ret;
1238 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001239 }
1240
1241 /* probe the platform */
Lars-Peter Clausen70090bb2014-08-19 15:51:24 +02001242 if (platform->component.driver->probe_order == order) {
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02001243 ret = soc_probe_component(card, &platform->component);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001244 if (ret < 0)
1245 return ret;
1246 }
1247
1248 return 0;
1249}
1250
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001251static int soc_probe_codec_dai(struct snd_soc_card *card,
1252 struct snd_soc_dai *codec_dai,
1253 int order)
1254{
1255 int ret;
1256
1257 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1258 if (codec_dai->driver->probe) {
1259 ret = codec_dai->driver->probe(codec_dai);
1260 if (ret < 0) {
1261 dev_err(codec_dai->dev,
1262 "ASoC: failed to probe CODEC DAI %s: %d\n",
1263 codec_dai->name, ret);
1264 return ret;
1265 }
1266 }
1267
1268 /* mark codec_dai as probed and add to card dai list */
1269 codec_dai->probed = 1;
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001270 }
1271
1272 return 0;
1273}
1274
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001275static int soc_link_dai_widgets(struct snd_soc_card *card,
1276 struct snd_soc_dai_link *dai_link,
Benoit Cousson3f901a02014-07-01 09:47:54 +02001277 struct snd_soc_pcm_runtime *rtd)
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001278{
Benoit Cousson3f901a02014-07-01 09:47:54 +02001279 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1280 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001281 struct snd_soc_dapm_widget *play_w, *capture_w;
1282 int ret;
1283
Benoit Cousson88bd8702014-07-08 23:19:34 +02001284 if (rtd->num_codecs > 1)
1285 dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n");
1286
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001287 /* link the DAI widgets */
1288 play_w = codec_dai->playback_widget;
1289 capture_w = cpu_dai->capture_widget;
1290 if (play_w && capture_w) {
1291 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1292 capture_w, play_w);
1293 if (ret != 0) {
1294 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1295 play_w->name, capture_w->name, ret);
1296 return ret;
1297 }
1298 }
1299
1300 play_w = cpu_dai->playback_widget;
1301 capture_w = codec_dai->capture_widget;
1302 if (play_w && capture_w) {
1303 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1304 capture_w, play_w);
1305 if (ret != 0) {
1306 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1307 play_w->name, capture_w->name, ret);
1308 return ret;
1309 }
1310 }
1311
1312 return 0;
1313}
1314
Stephen Warren62ae68f2012-06-08 12:34:23 -06001315static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001316{
1317 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1318 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001319 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001320 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001321 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001322
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001323 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001324 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001325
1326 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001327 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001328 cpu_dai->card = card;
Benoit Cousson88bd8702014-07-08 23:19:34 +02001329 for (i = 0; i < rtd->num_codecs; i++)
1330 rtd->codec_dais[i]->card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001331
1332 /* set default power off timeout */
1333 rtd->pmdown_time = pmdown_time;
1334
1335 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001336 if (!cpu_dai->probed &&
1337 cpu_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001338 if (cpu_dai->driver->probe) {
1339 ret = cpu_dai->driver->probe(cpu_dai);
1340 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001341 dev_err(cpu_dai->dev,
1342 "ASoC: failed to probe CPU DAI %s: %d\n",
1343 cpu_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001344 return ret;
1345 }
1346 }
1347 cpu_dai->probed = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001348 }
1349
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001350 /* probe the CODEC DAI */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001351 for (i = 0; i < rtd->num_codecs; i++) {
1352 ret = soc_probe_codec_dai(card, rtd->codec_dais[i], order);
1353 if (ret)
1354 return ret;
1355 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001356
Liam Girdwood0168bf02011-06-07 16:08:05 +01001357 /* complete DAI probe during last probe */
1358 if (order != SND_SOC_COMP_ORDER_LAST)
1359 return 0;
1360
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001361 /* do machine specific initialization */
1362 if (dai_link->init) {
1363 ret = dai_link->init(rtd);
1364 if (ret < 0) {
1365 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1366 dai_link->name, ret);
1367 return ret;
1368 }
1369 }
1370
1371 ret = soc_post_component_init(rtd, dai_link->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001372 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001373 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001374
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001375#ifdef CONFIG_DEBUG_FS
1376 /* add DPCM sysfs entries */
1377 if (dai_link->dynamic) {
1378 ret = soc_dpcm_debugfs_add(rtd);
1379 if (ret < 0) {
1380 dev_err(rtd->dev,
1381 "ASoC: failed to add dpcm sysfs entries: %d\n",
1382 ret);
1383 return ret;
1384 }
1385 }
1386#endif
1387
Mark Brown36ae1a92012-01-06 17:12:45 -08001388 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001390 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1391 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392
Namarta Kohli1245b702012-08-16 17:10:41 +05301393 if (cpu_dai->driver->compress_dai) {
1394 /*create compress_device"*/
1395 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001396 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001397 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301398 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001399 return ret;
1400 }
1401 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301402
1403 if (!dai_link->params) {
1404 /* create the pcm */
1405 ret = soc_new_pcm(rtd, num);
1406 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001407 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301408 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001409 return ret;
1410 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301411 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001412 INIT_DELAYED_WORK(&rtd->delayed_work,
1413 codec2codec_close_delayed_work);
1414
Namarta Kohli1245b702012-08-16 17:10:41 +05301415 /* link the DAI widgets */
Benoit Cousson3f901a02014-07-01 09:47:54 +02001416 ret = soc_link_dai_widgets(card, dai_link, rtd);
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001417 if (ret)
1418 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001419 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001420 }
1421
1422 /* add platform data for AC97 devices */
Benoit Cousson88bd8702014-07-08 23:19:34 +02001423 for (i = 0; i < rtd->num_codecs; i++) {
1424 if (rtd->codec_dais[i]->driver->ac97_control)
1425 snd_ac97_dev_add_pdata(rtd->codec_dais[i]->codec->ac97,
1426 rtd->cpu_dai->ac97_pdata);
1427 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001428
1429 return 0;
1430}
1431
1432#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001433static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1434 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001435{
1436 int ret;
1437
1438 /* Only instantiate AC97 if not already done by the adaptor
1439 * for the generic AC97 subsystem.
1440 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001441 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001442 /*
1443 * It is possible that the AC97 device is already registered to
1444 * the device subsystem. This happens when the device is created
1445 * via snd_ac97_mixer(). Currently only SoC codec that does so
1446 * is the generic AC97 glue but others migh emerge.
1447 *
1448 * In those cases we don't try to register the device again.
1449 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001450 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001451 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001452
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001453 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001455 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001456 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001457 return ret;
1458 }
1459
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001460 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001461 }
1462 return 0;
1463}
1464
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001465static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001466{
1467 if (codec->ac97_registered) {
1468 soc_ac97_dev_unregister(codec);
1469 codec->ac97_registered = 0;
1470 }
1471}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001472
Benoit Cousson88bd8702014-07-08 23:19:34 +02001473static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1474{
1475 int i, ret;
1476
1477 for (i = 0; i < rtd->num_codecs; i++) {
1478 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
1479
1480 ret = soc_register_ac97_codec(codec_dai->codec, codec_dai);
1481 if (ret) {
1482 while (--i >= 0)
1483 soc_unregister_ac97_codec(codec_dai->codec);
1484 return ret;
1485 }
1486 }
1487
1488 return 0;
1489}
1490
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001491static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1492{
Benoit Cousson88bd8702014-07-08 23:19:34 +02001493 int i;
1494
1495 for (i = 0; i < rtd->num_codecs; i++)
1496 soc_unregister_ac97_codec(rtd->codec_dais[i]->codec);
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001497}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001498#endif
1499
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001500static int soc_bind_aux_dev(struct snd_soc_card *card, int num)
Mark Brownb19e6e72012-03-14 21:18:39 +00001501{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001502 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001503 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001504 const char *name = aux_dev->codec_name;
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001505
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001506 rtd->component = soc_find_component(aux_dev->codec_of_node, name);
1507 if (!rtd->component) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001508 if (aux_dev->codec_of_node)
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001509 name = of_node_full_name(aux_dev->codec_of_node);
Sebastian Reichel3ca041e2014-04-28 16:07:22 +02001510
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001511 dev_err(card->dev, "ASoC: %s not registered\n", name);
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001512 return -EPROBE_DEFER;
1513 }
1514
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001515 /*
1516 * Some places still reference rtd->codec, so we have to keep that
1517 * initialized if the component is a CODEC. Once all those references
1518 * have been removed, this code can be removed as well.
1519 */
1520 rtd->codec = rtd->component->codec;
1521
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001522 return 0;
Mark Brownb19e6e72012-03-14 21:18:39 +00001523}
1524
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001525static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1526{
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001527 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001528 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001529 int ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001530
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001531 ret = soc_probe_component(card, rtd->component);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001532 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001533 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001534
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001535 /* do machine specific initialization */
1536 if (aux_dev->init) {
Lars-Peter Clausen57bf7722014-08-19 15:51:23 +02001537 ret = aux_dev->init(rtd->component);
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001538 if (ret < 0) {
1539 dev_err(card->dev, "ASoC: failed to init %s: %d\n",
1540 aux_dev->name, ret);
1541 return ret;
1542 }
1543 }
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001544
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02001545 return soc_post_component_init(rtd, aux_dev->name);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001546}
1547
1548static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1549{
1550 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001551 struct snd_soc_component *component = rtd->component;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001552
1553 /* unregister the rtd device */
1554 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001555 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001556 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001557 rtd->dev_registered = 0;
1558 }
1559
Lars-Peter Clausen65d93612014-08-19 15:51:22 +02001560 if (component && component->probed)
1561 soc_remove_component(component);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001562}
1563
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001564static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001565{
1566 int ret;
1567
1568 if (codec->cache_init)
1569 return 0;
1570
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001571 ret = snd_soc_cache_init(codec);
1572 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001573 dev_err(codec->dev,
1574 "ASoC: Failed to set cache compression type: %d\n",
1575 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001576 return ret;
1577 }
1578 codec->cache_init = 1;
1579 return 0;
1580}
1581
Mark Brownb19e6e72012-03-14 21:18:39 +00001582static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001583{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001584 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001585 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001586 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587
Liam Girdwood01b9d992012-03-07 10:38:25 +00001588 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001589
Mark Brownb19e6e72012-03-14 21:18:39 +00001590 /* bind DAIs */
1591 for (i = 0; i < card->num_links; i++) {
1592 ret = soc_bind_dai_link(card, i);
1593 if (ret != 0)
1594 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001595 }
1596
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001597 /* bind aux_devs too */
Mark Brownb19e6e72012-03-14 21:18:39 +00001598 for (i = 0; i < card->num_aux_devs; i++) {
Lars-Peter Clausen44c69bb2014-07-01 22:13:47 +02001599 ret = soc_bind_aux_dev(card, i);
Mark Brownb19e6e72012-03-14 21:18:39 +00001600 if (ret != 0)
1601 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001602 }
1603
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001604 /* initialize the register cache for each available codec */
1605 list_for_each_entry(codec, &codec_list, list) {
1606 if (codec->cache_init)
1607 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001608 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001609 if (ret < 0)
1610 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001611 }
1612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001613 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001614 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615 card->owner, 0, &card->snd_card);
1616 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001617 dev_err(card->dev,
1618 "ASoC: can't create sound card for card %s: %d\n",
1619 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001620 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001622
Mark Browne37a4972011-03-02 18:21:57 +00001623 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1624 card->dapm.dev = card->dev;
1625 card->dapm.card = card;
1626 list_add(&card->dapm.list, &card->dapm_list);
1627
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001628#ifdef CONFIG_DEBUG_FS
1629 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1630#endif
1631
Mark Brown88ee1c62011-02-02 10:43:26 +00001632#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001633 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001634 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001635#endif
Andy Green6ed25972008-06-13 16:24:05 +01001636
Mark Brown9a841eb2011-04-12 17:51:37 -07001637 if (card->dapm_widgets)
1638 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1639 card->num_dapm_widgets);
1640
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641 /* initialise the sound card only once */
1642 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001643 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644 if (ret < 0)
1645 goto card_probe_error;
1646 }
1647
Stephen Warren62ae68f2012-06-08 12:34:23 -06001648 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001649 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1650 order++) {
1651 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001652 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001653 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001654 dev_err(card->dev,
1655 "ASoC: failed to instantiate card %d\n",
1656 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001657 goto probe_dai_err;
1658 }
1659 }
1660 }
1661
1662 /* probe all DAI links on this card */
1663 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1664 order++) {
1665 for (i = 0; i < card->num_links; i++) {
1666 ret = soc_probe_link_dais(card, i, order);
1667 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001668 dev_err(card->dev,
1669 "ASoC: failed to instantiate card %d\n",
1670 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001671 goto probe_dai_err;
1672 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001673 }
1674 }
1675
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676 for (i = 0; i < card->num_aux_devs; i++) {
1677 ret = soc_probe_aux_dev(card, i);
1678 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001679 dev_err(card->dev,
1680 "ASoC: failed to add auxiliary devices %d\n",
1681 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001682 goto probe_aux_dev_err;
1683 }
1684 }
1685
Mark Brown888df392012-02-16 19:37:51 -08001686 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001687 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001688
Mark Brownb7af1da2011-04-07 19:18:44 +09001689 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001690 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001691
Mark Brownb8ad29d2011-03-02 18:35:51 +00001692 if (card->dapm_routes)
1693 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1694 card->num_dapm_routes);
1695
Mark Brown75d9ac42011-09-27 16:41:01 +01001696 for (i = 0; i < card->num_links; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001697 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Mark Brown75d9ac42011-09-27 16:41:01 +01001698 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001699 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001700
Mark Brownf04209a2012-04-09 16:29:21 +01001701 if (dai_fmt) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001702 struct snd_soc_dai **codec_dais = rtd->codec_dais;
1703 int j;
1704
1705 for (j = 0; j < rtd->num_codecs; j++) {
1706 struct snd_soc_dai *codec_dai = codec_dais[j];
1707
1708 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
1709 if (ret != 0 && ret != -ENOTSUPP)
1710 dev_warn(codec_dai->dev,
1711 "ASoC: Failed to set DAI format: %d\n",
1712 ret);
1713 }
Mark Brownf04209a2012-04-09 16:29:21 +01001714 }
1715
1716 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001717 if (dai_fmt &&
1718 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001719 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1720 dai_fmt);
1721 if (ret != 0 && ret != -ENOTSUPP)
1722 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001723 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001724 ret);
1725 } else if (dai_fmt) {
1726 /* Flip the polarity for the "CPU" end */
1727 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1728 switch (dai_link->dai_fmt &
1729 SND_SOC_DAIFMT_MASTER_MASK) {
1730 case SND_SOC_DAIFMT_CBM_CFM:
1731 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1732 break;
1733 case SND_SOC_DAIFMT_CBM_CFS:
1734 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1735 break;
1736 case SND_SOC_DAIFMT_CBS_CFM:
1737 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1738 break;
1739 case SND_SOC_DAIFMT_CBS_CFS:
1740 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1741 break;
1742 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001743
1744 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001745 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001746 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001747 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001748 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001749 ret);
1750 }
1751 }
1752
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001754 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001755 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1756 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001757 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1758 "%s", card->driver_name ? card->driver_name : card->name);
1759 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1760 switch (card->snd_card->driver[i]) {
1761 case '_':
1762 case '-':
1763 case '\0':
1764 break;
1765 default:
1766 if (!isalnum(card->snd_card->driver[i]))
1767 card->snd_card->driver[i] = '_';
1768 break;
1769 }
1770 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771
Mark Brown28e9ad92011-03-02 18:36:34 +00001772 if (card->late_probe) {
1773 ret = card->late_probe(card);
1774 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001775 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001776 card->name, ret);
1777 goto probe_aux_dev_err;
1778 }
1779 }
1780
Stephen Warren16332812011-11-23 12:42:04 -07001781 if (card->fully_routed)
Lars-Peter Clausen7df37882014-06-16 18:13:04 +02001782 snd_soc_dapm_auto_nc_pins(card);
Stephen Warren16332812011-11-23 12:42:04 -07001783
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001784 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001785
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001786 ret = snd_card_register(card->snd_card);
1787 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001788 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1789 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001790 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001791 }
1792
1793#ifdef CONFIG_SND_SOC_AC97_BUS
1794 /* register any AC97 codecs */
1795 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001796 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1797 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001798 dev_err(card->dev,
1799 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001800 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001801 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001802 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001803 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001804 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001805#endif
1806
Mark Brown435c5e22008-12-04 15:32:53 +00001807 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001808 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001809 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001810
1811 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001812
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001813probe_aux_dev_err:
1814 for (i = 0; i < card->num_aux_devs; i++)
1815 soc_remove_aux_dev(card, i);
1816
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001818 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001819
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001820card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001821 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001822 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001823
1824 snd_card_free(card->snd_card);
1825
Mark Brownb19e6e72012-03-14 21:18:39 +00001826base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001827 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001828
Mark Brownb19e6e72012-03-14 21:18:39 +00001829 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001830}
1831
1832/* probes a new socdev */
1833static int soc_probe(struct platform_device *pdev)
1834{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001835 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001836
Vinod Koul70a7ca32011-01-14 19:22:48 +05301837 /*
1838 * no card, so machine driver should be registering card
1839 * we should not be here in that case so ret error
1840 */
1841 if (!card)
1842 return -EINVAL;
1843
Mark Brownfe4085e2012-03-02 13:07:41 +00001844 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001845 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001846 card->name);
1847
Mark Brown435c5e22008-12-04 15:32:53 +00001848 /* Bodge while we unpick instantiation */
1849 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001850
Mark Brown28d528c2012-08-09 18:45:23 +01001851 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001852}
1853
Vinod Koulb0e26482011-01-13 22:48:02 +05301854static int soc_cleanup_card_resources(struct snd_soc_card *card)
1855{
Vinod Koulb0e26482011-01-13 22:48:02 +05301856 int i;
1857
1858 /* make sure any delayed work runs */
1859 for (i = 0; i < card->num_rtd; i++) {
1860 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001861 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301862 }
1863
1864 /* remove auxiliary devices */
1865 for (i = 0; i < card->num_aux_devs; i++)
1866 soc_remove_aux_dev(card, i);
1867
1868 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001869 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301870
1871 soc_cleanup_card_debugfs(card);
1872
1873 /* remove the card */
1874 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001875 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301876
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001877 snd_soc_dapm_free(&card->dapm);
1878
Vinod Koulb0e26482011-01-13 22:48:02 +05301879 snd_card_free(card->snd_card);
1880 return 0;
1881
1882}
1883
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001884/* removes a socdev */
1885static int soc_remove(struct platform_device *pdev)
1886{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001887 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001888
Mark Brownc5af3a22008-11-28 13:29:45 +00001889 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001890 return 0;
1891}
1892
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001893int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001894{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001895 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896 int i;
Mark Brown51737472009-06-22 13:16:51 +01001897
1898 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001899 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001900
1901 /* Flush out pmdown_time work - we actually do want to run it
1902 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001903 for (i = 0; i < card->num_rtd; i++) {
1904 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001905 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001906 }
Mark Brown51737472009-06-22 13:16:51 +01001907
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001908 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001909
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001910 /* deactivate pins to sleep state */
1911 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02001912 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1913 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1914 int j;
1915
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001916 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Benoit Cousson88bd8702014-07-08 23:19:34 +02001917 for (j = 0; j < rtd->num_codecs; j++) {
1918 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
1919 pinctrl_pm_select_sleep_state(codec_dai->dev);
1920 }
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001921 }
1922
Mark Brown416356f2009-06-30 19:05:15 +01001923 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001924}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001925EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001926
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001927const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301928 .suspend = snd_soc_suspend,
1929 .resume = snd_soc_resume,
1930 .freeze = snd_soc_suspend,
1931 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001932 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301933 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001934};
Stephen Warrendeb26072011-04-05 19:35:30 -06001935EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001936
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937/* ASoC platform driver */
1938static struct platform_driver soc_driver = {
1939 .driver = {
1940 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001941 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001942 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001943 },
1944 .probe = soc_probe,
1945 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001946};
1947
Mark Brown096e49d2009-07-05 15:12:22 +01001948/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949 * snd_soc_new_ac97_codec - initailise AC97 device
1950 * @codec: audio codec
1951 * @ops: AC97 bus operations
1952 * @num: AC97 codec number
1953 *
1954 * Initialises AC97 codec resources for use by ad-hoc devices only.
1955 */
1956int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1957 struct snd_ac97_bus_ops *ops, int num)
1958{
1959 mutex_lock(&codec->mutex);
1960
1961 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1962 if (codec->ac97 == NULL) {
1963 mutex_unlock(&codec->mutex);
1964 return -ENOMEM;
1965 }
1966
1967 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1968 if (codec->ac97->bus == NULL) {
1969 kfree(codec->ac97);
1970 codec->ac97 = NULL;
1971 mutex_unlock(&codec->mutex);
1972 return -ENOMEM;
1973 }
1974
1975 codec->ac97->bus->ops = ops;
1976 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001977
1978 /*
1979 * Mark the AC97 device to be created by us. This way we ensure that the
1980 * device will be registered with the device subsystem later on.
1981 */
1982 codec->ac97_created = 1;
1983
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984 mutex_unlock(&codec->mutex);
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1988
Markus Pargmann741a5092013-08-19 17:05:55 +02001989static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
1990
1991static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
1992{
1993 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
1994
1995 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
1996
1997 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
1998
1999 udelay(10);
2000
2001 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2002
2003 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2004 msleep(2);
2005}
2006
2007static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2008{
2009 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2010
2011 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2012
2013 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2014 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2015 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2016
2017 udelay(10);
2018
2019 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2020
2021 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2022 msleep(2);
2023}
2024
2025static int snd_soc_ac97_parse_pinctl(struct device *dev,
2026 struct snd_ac97_reset_cfg *cfg)
2027{
2028 struct pinctrl *p;
2029 struct pinctrl_state *state;
2030 int gpio;
2031 int ret;
2032
2033 p = devm_pinctrl_get(dev);
2034 if (IS_ERR(p)) {
2035 dev_err(dev, "Failed to get pinctrl\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002036 return PTR_ERR(p);
Markus Pargmann741a5092013-08-19 17:05:55 +02002037 }
2038 cfg->pctl = p;
2039
2040 state = pinctrl_lookup_state(p, "ac97-reset");
2041 if (IS_ERR(state)) {
2042 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002043 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002044 }
2045 cfg->pstate_reset = state;
2046
2047 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2048 if (IS_ERR(state)) {
2049 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002050 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002051 }
2052 cfg->pstate_warm_reset = state;
2053
2054 state = pinctrl_lookup_state(p, "ac97-running");
2055 if (IS_ERR(state)) {
2056 dev_err(dev, "Can't find pinctrl state ac97-running\n");
Christoph Jaegerb7580cd2014-04-15 22:39:01 +02002057 return PTR_ERR(state);
Markus Pargmann741a5092013-08-19 17:05:55 +02002058 }
2059 cfg->pstate_run = state;
2060
2061 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2062 if (gpio < 0) {
2063 dev_err(dev, "Can't find ac97-sync gpio\n");
2064 return gpio;
2065 }
2066 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2067 if (ret) {
2068 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2069 return ret;
2070 }
2071 cfg->gpio_sync = gpio;
2072
2073 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2074 if (gpio < 0) {
2075 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2076 return gpio;
2077 }
2078 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2079 if (ret) {
2080 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2081 return ret;
2082 }
2083 cfg->gpio_sdata = gpio;
2084
2085 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2086 if (gpio < 0) {
2087 dev_err(dev, "Can't find ac97-reset gpio\n");
2088 return gpio;
2089 }
2090 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2091 if (ret) {
2092 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2093 return ret;
2094 }
2095 cfg->gpio_reset = gpio;
2096
2097 return 0;
2098}
2099
Mark Brownb047e1c2013-06-26 12:45:59 +01002100struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002101EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002102
2103int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2104{
2105 if (ops == soc_ac97_ops)
2106 return 0;
2107
2108 if (soc_ac97_ops && ops)
2109 return -EBUSY;
2110
2111 soc_ac97_ops = ops;
2112
2113 return 0;
2114}
2115EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2116
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002117/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002118 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2119 *
2120 * This function sets the reset and warm_reset properties of ops and parses
2121 * the device node of pdev to get pinctrl states and gpio numbers to use.
2122 */
2123int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2124 struct platform_device *pdev)
2125{
2126 struct device *dev = &pdev->dev;
2127 struct snd_ac97_reset_cfg cfg;
2128 int ret;
2129
2130 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2131 if (ret)
2132 return ret;
2133
2134 ret = snd_soc_set_ac97_ops(ops);
2135 if (ret)
2136 return ret;
2137
2138 ops->warm_reset = snd_soc_ac97_warm_reset;
2139 ops->reset = snd_soc_ac97_reset;
2140
2141 snd_ac97_rst_cfg = cfg;
2142 return 0;
2143}
2144EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2145
2146/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002147 * snd_soc_free_ac97_codec - free AC97 codec device
2148 * @codec: audio codec
2149 *
2150 * Frees AC97 codec device resources.
2151 */
2152void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2153{
2154 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002155#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002156 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002157#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002158 kfree(codec->ac97->bus);
2159 kfree(codec->ac97);
2160 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002161 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002162 mutex_unlock(&codec->mutex);
2163}
2164EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2165
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002166/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002167 * snd_soc_cnew - create new control
2168 * @_template: control template
2169 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002170 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002171 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002172 *
2173 * Create a new mixer control from a template control.
2174 *
2175 * Returns 0 for success, else error.
2176 */
2177struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002178 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002179 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002180{
2181 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002182 struct snd_kcontrol *kcontrol;
2183 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002184
2185 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002186 template.index = 0;
2187
Mark Brownefb7ac32011-03-08 17:23:24 +00002188 if (!long_name)
2189 long_name = template.name;
2190
2191 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002192 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002193 if (!name)
2194 return NULL;
2195
Mark Brownefb7ac32011-03-08 17:23:24 +00002196 template.name = name;
2197 } else {
2198 template.name = long_name;
2199 }
2200
2201 kcontrol = snd_ctl_new1(&template, data);
2202
2203 kfree(name);
2204
2205 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002206}
2207EXPORT_SYMBOL_GPL(snd_soc_cnew);
2208
Liam Girdwood022658b2012-02-03 17:43:09 +00002209static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2210 const struct snd_kcontrol_new *controls, int num_controls,
2211 const char *prefix, void *data)
2212{
2213 int err, i;
2214
2215 for (i = 0; i < num_controls; i++) {
2216 const struct snd_kcontrol_new *control = &controls[i];
2217 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2218 control->name, prefix));
2219 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002220 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2221 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002222 return err;
2223 }
2224 }
2225
2226 return 0;
2227}
2228
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002229struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2230 const char *name)
2231{
2232 struct snd_card *card = soc_card->snd_card;
2233 struct snd_kcontrol *kctl;
2234
2235 if (unlikely(!name))
2236 return NULL;
2237
2238 list_for_each_entry(kctl, &card->controls, list)
2239 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2240 return kctl;
2241 return NULL;
2242}
2243EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2244
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002245/**
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002246 * snd_soc_add_component_controls - Add an array of controls to a component.
2247 *
2248 * @component: Component to add controls to
2249 * @controls: Array of controls to add
2250 * @num_controls: Number of elements in the array
2251 *
2252 * Return: 0 for success, else error.
2253 */
2254int snd_soc_add_component_controls(struct snd_soc_component *component,
2255 const struct snd_kcontrol_new *controls, unsigned int num_controls)
2256{
2257 struct snd_card *card = component->card->snd_card;
2258
2259 return snd_soc_add_controls(card, component->dev, controls,
2260 num_controls, component->name_prefix, component);
2261}
2262EXPORT_SYMBOL_GPL(snd_soc_add_component_controls);
2263
2264/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002265 * snd_soc_add_codec_controls - add an array of controls to a codec.
2266 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002267 * duplicating this code.
2268 *
2269 * @codec: codec to add controls to
2270 * @controls: array of controls to add
2271 * @num_controls: number of elements in the array
2272 *
2273 * Return 0 for success, else error.
2274 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002275int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002276 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Ian Molton3e8e1952009-01-09 00:23:21 +00002277{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002278 return snd_soc_add_component_controls(&codec->component, controls,
2279 num_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002280}
Liam Girdwood022658b2012-02-03 17:43:09 +00002281EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002282
2283/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002284 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002285 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002286 *
2287 * @platform: platform to add controls to
2288 * @controls: array of controls to add
2289 * @num_controls: number of elements in the array
2290 *
2291 * Return 0 for success, else error.
2292 */
2293int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002294 const struct snd_kcontrol_new *controls, unsigned int num_controls)
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002295{
Lars-Peter Clausen0f2780a2014-07-17 22:01:08 +02002296 return snd_soc_add_component_controls(&platform->component, controls,
2297 num_controls);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002298}
2299EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2300
2301/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002302 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2303 * Convenience function to add a list of controls.
2304 *
2305 * @soc_card: SoC card to add controls to
2306 * @controls: array of controls to add
2307 * @num_controls: number of elements in the array
2308 *
2309 * Return 0 for success, else error.
2310 */
2311int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2312 const struct snd_kcontrol_new *controls, int num_controls)
2313{
2314 struct snd_card *card = soc_card->snd_card;
2315
2316 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2317 NULL, soc_card);
2318}
2319EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2320
2321/**
2322 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2323 * Convienience function to add a list of controls.
2324 *
2325 * @dai: DAI to add controls to
2326 * @controls: array of controls to add
2327 * @num_controls: number of elements in the array
2328 *
2329 * Return 0 for success, else error.
2330 */
2331int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2332 const struct snd_kcontrol_new *controls, int num_controls)
2333{
2334 struct snd_card *card = dai->card->snd_card;
2335
2336 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2337 NULL, dai);
2338}
2339EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2340
2341/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002342 * snd_soc_info_enum_double - enumerated double mixer info callback
2343 * @kcontrol: mixer control
2344 * @uinfo: control element information
2345 *
2346 * Callback to provide information about a double enumerated
2347 * mixer control.
2348 *
2349 * Returns 0 for success.
2350 */
2351int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2352 struct snd_ctl_elem_info *uinfo)
2353{
2354 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2355
2356 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2357 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002358 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002359
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002360 if (uinfo->value.enumerated.item >= e->items)
2361 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002362 strlcpy(uinfo->value.enumerated.name,
2363 e->texts[uinfo->value.enumerated.item],
2364 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002365 return 0;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2368
2369/**
2370 * snd_soc_get_enum_double - enumerated double mixer get callback
2371 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002372 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002373 *
2374 * Callback to get the value of a double enumerated mixer.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_value *ucontrol)
2380{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002381 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002382 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002383 unsigned int val, item;
2384 unsigned int reg_val;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002385 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002387 ret = snd_soc_component_read(component, e->reg, &reg_val);
2388 if (ret)
2389 return ret;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002390 val = (reg_val >> e->shift_l) & e->mask;
2391 item = snd_soc_enum_val_to_item(e, val);
2392 ucontrol->value.enumerated.item[0] = item;
2393 if (e->shift_l != e->shift_r) {
2394 val = (reg_val >> e->shift_l) & e->mask;
2395 item = snd_soc_enum_val_to_item(e, val);
2396 ucontrol->value.enumerated.item[1] = item;
2397 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002398
2399 return 0;
2400}
2401EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2402
2403/**
2404 * snd_soc_put_enum_double - enumerated double mixer put callback
2405 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002406 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407 *
2408 * Callback to set the value of a double enumerated mixer.
2409 *
2410 * Returns 0 for success.
2411 */
2412int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2413 struct snd_ctl_elem_value *ucontrol)
2414{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002415 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002417 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002418 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002419 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002421 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002423 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002424 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002425 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002426 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002427 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002428 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002429 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 }
2431
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002432 return snd_soc_component_update_bits(component, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433}
2434EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2435
2436/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002437 * snd_soc_read_signed - Read a codec register and interprete as signed value
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002438 * @component: component
Markus Pargmannf227b882014-01-16 16:02:10 +01002439 * @reg: Register to read
2440 * @mask: Mask to use after shifting the register value
2441 * @shift: Right shift of register value
2442 * @sign_bit: Bit that describes if a number is negative or not.
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002443 * @signed_val: Pointer to where the read value should be stored
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002444 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002445 * This functions reads a codec register. The register value is shifted right
2446 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2447 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002448 *
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002449 * Returns 0 on sucess, otherwise an error value
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002450 */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002451static int snd_soc_read_signed(struct snd_soc_component *component,
2452 unsigned int reg, unsigned int mask, unsigned int shift,
2453 unsigned int sign_bit, int *signed_val)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002454{
Markus Pargmannf227b882014-01-16 16:02:10 +01002455 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002456 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002457
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002458 ret = snd_soc_component_read(component, reg, &val);
2459 if (ret < 0)
2460 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002461
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002462 val = (val >> shift) & mask;
2463
2464 if (!sign_bit) {
2465 *signed_val = val;
2466 return 0;
2467 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002468
2469 /* non-negative number */
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002470 if (!(val & BIT(sign_bit))) {
2471 *signed_val = val;
2472 return 0;
2473 }
Markus Pargmannf227b882014-01-16 16:02:10 +01002474
2475 ret = val;
2476
2477 /*
2478 * The register most probably does not contain a full-sized int.
2479 * Instead we have an arbitrary number of bits in a signed
2480 * representation which has to be translated into a full-sized int.
2481 * This is done by filling up all bits above the sign-bit.
2482 */
2483 ret |= ~((int)(BIT(sign_bit) - 1));
2484
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002485 *signed_val = ret;
2486
2487 return 0;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002488}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002489
2490/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002491 * snd_soc_info_volsw - single mixer info callback
2492 * @kcontrol: mixer control
2493 * @uinfo: control element information
2494 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002495 * Callback to provide information about a single mixer control, or a double
2496 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002497 *
2498 * Returns 0 for success.
2499 */
2500int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2501 struct snd_ctl_elem_info *uinfo)
2502{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002503 struct soc_mixer_control *mc =
2504 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002505 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002507 if (!mc->platform_max)
2508 mc->platform_max = mc->max;
2509 platform_max = mc->platform_max;
2510
2511 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002512 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2513 else
2514 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2515
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002516 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002517 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002518 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002519 return 0;
2520}
2521EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2522
2523/**
2524 * snd_soc_get_volsw - single mixer get callback
2525 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002526 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002528 * Callback to get the value of a single mixer control, or a double mixer
2529 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002530 *
2531 * Returns 0 for success.
2532 */
2533int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2534 struct snd_ctl_elem_value *ucontrol)
2535{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002536 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002537 struct soc_mixer_control *mc =
2538 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002539 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002540 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002541 unsigned int shift = mc->shift;
2542 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002543 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002544 int min = mc->min;
2545 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002546 unsigned int mask = (1 << fls(max)) - 1;
2547 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002548 int val;
2549 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002550
Markus Pargmannf227b882014-01-16 16:02:10 +01002551 if (sign_bit)
2552 mask = BIT(sign_bit + 1) - 1;
2553
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002554 ret = snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
2555 if (ret)
2556 return ret;
2557
2558 ucontrol->value.integer.value[0] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002559 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002561 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002562
2563 if (snd_soc_volsw_is_stereo(mc)) {
2564 if (reg == reg2)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002565 ret = snd_soc_read_signed(component, reg, mask, rshift,
2566 sign_bit, &val);
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002567 else
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002568 ret = snd_soc_read_signed(component, reg2, mask, shift,
2569 sign_bit, &val);
2570 if (ret)
2571 return ret;
2572
2573 ucontrol->value.integer.value[1] = val - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002574 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002575 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002576 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577 }
2578
2579 return 0;
2580}
2581EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2582
2583/**
2584 * snd_soc_put_volsw - single mixer put callback
2585 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002586 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002587 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002588 * Callback to set the value of a single mixer control, or a double mixer
2589 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590 *
2591 * Returns 0 for success.
2592 */
2593int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2594 struct snd_ctl_elem_value *ucontrol)
2595{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002596 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl4eaa9812008-07-29 11:42:26 +01002597 struct soc_mixer_control *mc =
2598 (struct soc_mixer_control *)kcontrol->private_value;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002599 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002600 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002601 unsigned int shift = mc->shift;
2602 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002603 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002604 int min = mc->min;
2605 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002606 unsigned int mask = (1 << fls(max)) - 1;
2607 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002608 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002609 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002610 unsigned int val2 = 0;
2611 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002612
Markus Pargmannf227b882014-01-16 16:02:10 +01002613 if (sign_bit)
2614 mask = BIT(sign_bit + 1) - 1;
2615
2616 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002618 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619 val_mask = mask << shift;
2620 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002621 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002622 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002623 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002624 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002625 if (reg == reg2) {
2626 val_mask |= mask << rshift;
2627 val |= val2 << rshift;
2628 } else {
2629 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002630 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002631 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002632 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002633 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002634 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002635 return err;
2636
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002637 if (type_2r)
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002638 err = snd_soc_component_update_bits(component, reg2, val_mask,
2639 val2);
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002640
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641 return err;
2642}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002643EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002644
Mark Browne13ac2e2008-05-28 17:58:05 +01002645/**
Brian Austin1d99f242012-03-30 10:43:55 -05002646 * snd_soc_get_volsw_sx - single mixer get callback
2647 * @kcontrol: mixer control
2648 * @ucontrol: control element information
2649 *
2650 * Callback to get the value of a single mixer control, or a double mixer
2651 * control that spans 2 registers.
2652 *
2653 * Returns 0 for success.
2654 */
2655int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2656 struct snd_ctl_elem_value *ucontrol)
2657{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002658 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002659 struct soc_mixer_control *mc =
2660 (struct soc_mixer_control *)kcontrol->private_value;
Brian Austin1d99f242012-03-30 10:43:55 -05002661 unsigned int reg = mc->reg;
2662 unsigned int reg2 = mc->rreg;
2663 unsigned int shift = mc->shift;
2664 unsigned int rshift = mc->rshift;
2665 int max = mc->max;
2666 int min = mc->min;
2667 int mask = (1 << (fls(min + max) - 1)) - 1;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002668 unsigned int val;
2669 int ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002670
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002671 ret = snd_soc_component_read(component, reg, &val);
2672 if (ret < 0)
2673 return ret;
Brian Austin1d99f242012-03-30 10:43:55 -05002674
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002675 ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
2676
2677 if (snd_soc_volsw_is_stereo(mc)) {
2678 ret = snd_soc_component_read(component, reg2, &val);
2679 if (ret < 0)
2680 return ret;
2681
2682 val = ((val >> rshift) - min) & mask;
2683 ucontrol->value.integer.value[1] = val;
2684 }
Brian Austin1d99f242012-03-30 10:43:55 -05002685
2686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2689
2690/**
2691 * snd_soc_put_volsw_sx - double mixer set callback
2692 * @kcontrol: mixer control
2693 * @uinfo: control element information
2694 *
2695 * Callback to set the value of a double mixer control that spans 2 registers.
2696 *
2697 * Returns 0 for success.
2698 */
2699int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2700 struct snd_ctl_elem_value *ucontrol)
2701{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002702 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Brian Austin1d99f242012-03-30 10:43:55 -05002703 struct soc_mixer_control *mc =
2704 (struct soc_mixer_control *)kcontrol->private_value;
2705
2706 unsigned int reg = mc->reg;
2707 unsigned int reg2 = mc->rreg;
2708 unsigned int shift = mc->shift;
2709 unsigned int rshift = mc->rshift;
2710 int max = mc->max;
2711 int min = mc->min;
2712 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002713 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002714 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002715
2716 val_mask = mask << shift;
2717 val = (ucontrol->value.integer.value[0] + min) & mask;
2718 val = val << shift;
2719
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002720 err = snd_soc_component_update_bits(component, reg, val_mask, val);
Mukund Navadad0558522012-11-09 11:53:40 +05302721 if (err < 0)
2722 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002723
2724 if (snd_soc_volsw_is_stereo(mc)) {
2725 val_mask = mask << rshift;
2726 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2727 val2 = val2 << rshift;
2728
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002729 err = snd_soc_component_update_bits(component, reg2, val_mask,
2730 val2);
Brian Austin1d99f242012-03-30 10:43:55 -05002731 }
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002732 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002733}
2734EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2735
2736/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002737 * snd_soc_info_volsw_s8 - signed mixer info callback
2738 * @kcontrol: mixer control
2739 * @uinfo: control element information
2740 *
2741 * Callback to provide information about a signed mixer control.
2742 *
2743 * Returns 0 for success.
2744 */
2745int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2746 struct snd_ctl_elem_info *uinfo)
2747{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002748 struct soc_mixer_control *mc =
2749 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002750 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002751 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002752
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002753 if (!mc->platform_max)
2754 mc->platform_max = mc->max;
2755 platform_max = mc->platform_max;
2756
Mark Browne13ac2e2008-05-28 17:58:05 +01002757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2758 uinfo->count = 2;
2759 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002760 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002761 return 0;
2762}
2763EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2764
2765/**
2766 * snd_soc_get_volsw_s8 - signed mixer get callback
2767 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002768 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002769 *
2770 * Callback to get the value of a signed mixer control.
2771 *
2772 * Returns 0 for success.
2773 */
2774int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2775 struct snd_ctl_elem_value *ucontrol)
2776{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002777 struct soc_mixer_control *mc =
2778 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002779 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002780 unsigned int reg = mc->reg;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002781 unsigned int val;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002782 int min = mc->min;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002783 int ret;
2784
2785 ret = snd_soc_component_read(component, reg, &val);
2786 if (ret)
2787 return ret;
Mark Browne13ac2e2008-05-28 17:58:05 +01002788
2789 ucontrol->value.integer.value[0] =
2790 ((signed char)(val & 0xff))-min;
2791 ucontrol->value.integer.value[1] =
2792 ((signed char)((val >> 8) & 0xff))-min;
2793 return 0;
2794}
2795EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2796
2797/**
2798 * snd_soc_put_volsw_sgn - signed mixer put callback
2799 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002800 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002801 *
2802 * Callback to set the value of a signed mixer control.
2803 *
2804 * Returns 0 for success.
2805 */
2806int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2807 struct snd_ctl_elem_value *ucontrol)
2808{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002809 struct soc_mixer_control *mc =
2810 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002811 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002812 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002813 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002814 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002815
2816 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2817 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2818
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002819 return snd_soc_component_update_bits(component, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002820}
2821EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2822
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002823/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002824 * snd_soc_info_volsw_range - single mixer info callback with range.
2825 * @kcontrol: mixer control
2826 * @uinfo: control element information
2827 *
2828 * Callback to provide information, within a range, about a single
2829 * mixer control.
2830 *
2831 * returns 0 for success.
2832 */
2833int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2834 struct snd_ctl_elem_info *uinfo)
2835{
2836 struct soc_mixer_control *mc =
2837 (struct soc_mixer_control *)kcontrol->private_value;
2838 int platform_max;
2839 int min = mc->min;
2840
2841 if (!mc->platform_max)
2842 mc->platform_max = mc->max;
2843 platform_max = mc->platform_max;
2844
2845 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002846 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002847 uinfo->value.integer.min = 0;
2848 uinfo->value.integer.max = platform_max - min;
2849
2850 return 0;
2851}
2852EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2853
2854/**
2855 * snd_soc_put_volsw_range - single mixer put value callback with range.
2856 * @kcontrol: mixer control
2857 * @ucontrol: control element information
2858 *
2859 * Callback to set the value, within a range, for a single mixer control.
2860 *
2861 * Returns 0 for success.
2862 */
2863int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2864 struct snd_ctl_elem_value *ucontrol)
2865{
2866 struct soc_mixer_control *mc =
2867 (struct soc_mixer_control *)kcontrol->private_value;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002868 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002869 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002870 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002871 unsigned int shift = mc->shift;
2872 int min = mc->min;
2873 int max = mc->max;
2874 unsigned int mask = (1 << fls(max)) - 1;
2875 unsigned int invert = mc->invert;
2876 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002877 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002878
2879 val = ((ucontrol->value.integer.value[0] + min) & mask);
2880 if (invert)
2881 val = max - val;
2882 val_mask = mask << shift;
2883 val = val << shift;
2884
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002885 ret = snd_soc_component_update_bits(component, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002886 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002887 return ret;
2888
2889 if (snd_soc_volsw_is_stereo(mc)) {
2890 val = ((ucontrol->value.integer.value[1] + min) & mask);
2891 if (invert)
2892 val = max - val;
2893 val_mask = mask << shift;
2894 val = val << shift;
2895
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002896 ret = snd_soc_component_update_bits(component, rreg, val_mask,
2897 val);
Mark Brown9bde4f02012-12-19 16:05:00 +00002898 }
2899
2900 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002901}
2902EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2903
2904/**
2905 * snd_soc_get_volsw_range - single mixer get callback with range
2906 * @kcontrol: mixer control
2907 * @ucontrol: control element information
2908 *
2909 * Callback to get the value, within a range, of a single mixer control.
2910 *
2911 * Returns 0 for success.
2912 */
2913int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2914 struct snd_ctl_elem_value *ucontrol)
2915{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002916 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002917 struct soc_mixer_control *mc =
2918 (struct soc_mixer_control *)kcontrol->private_value;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002919 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002920 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002921 unsigned int shift = mc->shift;
2922 int min = mc->min;
2923 int max = mc->max;
2924 unsigned int mask = (1 << fls(max)) - 1;
2925 unsigned int invert = mc->invert;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002926 unsigned int val;
2927 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002928
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002929 ret = snd_soc_component_read(component, reg, &val);
2930 if (ret)
2931 return ret;
2932
2933 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002934 if (invert)
2935 ucontrol->value.integer.value[0] =
2936 max - ucontrol->value.integer.value[0];
2937 ucontrol->value.integer.value[0] =
2938 ucontrol->value.integer.value[0] - min;
2939
Mark Brown9bde4f02012-12-19 16:05:00 +00002940 if (snd_soc_volsw_is_stereo(mc)) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002941 ret = snd_soc_component_read(component, rreg, &val);
2942 if (ret)
2943 return ret;
2944
2945 ucontrol->value.integer.value[1] = (val >> shift) & mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002946 if (invert)
2947 ucontrol->value.integer.value[1] =
2948 max - ucontrol->value.integer.value[1];
2949 ucontrol->value.integer.value[1] =
2950 ucontrol->value.integer.value[1] - min;
2951 }
2952
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002953 return 0;
2954}
2955EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2956
2957/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002958 * snd_soc_limit_volume - Set new limit to an existing volume control.
2959 *
2960 * @codec: where to look for the control
2961 * @name: Name of the control
2962 * @max: new maximum limit
2963 *
2964 * Return 0 for success, else error.
2965 */
2966int snd_soc_limit_volume(struct snd_soc_codec *codec,
2967 const char *name, int max)
2968{
Lars-Peter Clausen00200102014-07-17 22:01:07 +02002969 struct snd_card *card = codec->component.card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002970 struct snd_kcontrol *kctl;
2971 struct soc_mixer_control *mc;
2972 int found = 0;
2973 int ret = -EINVAL;
2974
2975 /* Sanity check for name and max */
2976 if (unlikely(!name || max <= 0))
2977 return -EINVAL;
2978
2979 list_for_each_entry(kctl, &card->controls, list) {
2980 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2981 found = 1;
2982 break;
2983 }
2984 }
2985 if (found) {
2986 mc = (struct soc_mixer_control *)kctl->private_value;
2987 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002988 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002989 ret = 0;
2990 }
2991 }
2992 return ret;
2993}
2994EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2995
Mark Brown71d08512011-10-10 18:31:26 +01002996int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2997 struct snd_ctl_elem_info *uinfo)
2998{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02002999 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003000 struct soc_bytes *params = (void *)kcontrol->private_value;
3001
3002 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003003 uinfo->count = params->num_regs * component->val_bytes;
Mark Brown71d08512011-10-10 18:31:26 +01003004
3005 return 0;
3006}
3007EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3008
3009int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3010 struct snd_ctl_elem_value *ucontrol)
3011{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003012 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003013 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brown71d08512011-10-10 18:31:26 +01003014 int ret;
3015
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003016 if (component->regmap)
3017 ret = regmap_raw_read(component->regmap, params->base,
Mark Brown71d08512011-10-10 18:31:26 +01003018 ucontrol->value.bytes.data,
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003019 params->num_regs * component->val_bytes);
Mark Brown71d08512011-10-10 18:31:26 +01003020 else
3021 ret = -EINVAL;
3022
Mark Brownf831b052012-02-17 16:20:33 -08003023 /* Hide any masked bytes to ensure consistent data reporting */
3024 if (ret == 0 && params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003025 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003026 case 1:
3027 ucontrol->value.bytes.data[0] &= ~params->mask;
3028 break;
3029 case 2:
3030 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003031 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003032 break;
3033 case 4:
3034 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003035 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003036 break;
3037 default:
3038 return -EINVAL;
3039 }
3040 }
3041
Mark Brown71d08512011-10-10 18:31:26 +01003042 return ret;
3043}
3044EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3045
3046int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3047 struct snd_ctl_elem_value *ucontrol)
3048{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003049 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Mark Brown71d08512011-10-10 18:31:26 +01003050 struct soc_bytes *params = (void *)kcontrol->private_value;
Mark Brownf831b052012-02-17 16:20:33 -08003051 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003052 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003053 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003054
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003055 if (!component->regmap)
Mark Brownf831b052012-02-17 16:20:33 -08003056 return -EINVAL;
3057
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003058 len = params->num_regs * component->val_bytes;
Mark Brownf831b052012-02-17 16:20:33 -08003059
Mark Brownb5a8fe42013-01-20 21:42:22 +09003060 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3061 if (!data)
3062 return -ENOMEM;
3063
Mark Brownf831b052012-02-17 16:20:33 -08003064 /*
3065 * If we've got a mask then we need to preserve the register
3066 * bits. We shouldn't modify the incoming data so take a
3067 * copy.
3068 */
3069 if (params->mask) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003070 ret = regmap_read(component->regmap, params->base, &val);
Mark Brownf831b052012-02-17 16:20:33 -08003071 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003072 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003073
3074 val &= params->mask;
3075
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003076 switch (component->val_bytes) {
Mark Brownf831b052012-02-17 16:20:33 -08003077 case 1:
3078 ((u8 *)data)[0] &= ~params->mask;
3079 ((u8 *)data)[0] |= val;
3080 break;
3081 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003082 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003083 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003084 &mask, &mask);
3085 if (ret != 0)
3086 goto out;
3087
3088 ((u16 *)data)[0] &= mask;
3089
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003090 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003091 &val, &val);
3092 if (ret != 0)
3093 goto out;
3094
3095 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003096 break;
3097 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003098 mask = ~params->mask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003099 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003100 &mask, &mask);
3101 if (ret != 0)
3102 goto out;
3103
3104 ((u32 *)data)[0] &= mask;
3105
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003106 ret = regmap_parse_val(component->regmap,
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003107 &val, &val);
3108 if (ret != 0)
3109 goto out;
3110
3111 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003112 break;
3113 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003114 ret = -EINVAL;
3115 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003116 }
3117 }
3118
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003119 ret = regmap_raw_write(component->regmap, params->base,
Mark Brownf831b052012-02-17 16:20:33 -08003120 data, len);
3121
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003122out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003123 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003124
3125 return ret;
3126}
3127EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3128
Vinod Kould9881202014-05-02 10:58:11 +05303129int snd_soc_bytes_info_ext(struct snd_kcontrol *kcontrol,
3130 struct snd_ctl_elem_info *ucontrol)
3131{
3132 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3133
3134 ucontrol->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3135 ucontrol->count = params->max;
3136
3137 return 0;
3138}
3139EXPORT_SYMBOL_GPL(snd_soc_bytes_info_ext);
3140
Omair Mohammed Abdullah7523a272014-07-15 21:34:48 +05303141int snd_soc_bytes_tlv_callback(struct snd_kcontrol *kcontrol, int op_flag,
3142 unsigned int size, unsigned int __user *tlv)
3143{
3144 struct soc_bytes_ext *params = (void *)kcontrol->private_value;
3145 unsigned int count = size < params->max ? size : params->max;
3146 int ret = -ENXIO;
3147
3148 switch (op_flag) {
3149 case SNDRV_CTL_TLV_OP_READ:
3150 if (params->get)
3151 ret = params->get(tlv, count);
3152 break;
3153 case SNDRV_CTL_TLV_OP_WRITE:
3154 if (params->put)
3155 ret = params->put(tlv, count);
3156 break;
3157 }
3158 return ret;
3159}
3160EXPORT_SYMBOL_GPL(snd_soc_bytes_tlv_callback);
3161
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003162/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003163 * snd_soc_info_xr_sx - signed multi register info callback
3164 * @kcontrol: mreg control
3165 * @uinfo: control element information
3166 *
3167 * Callback to provide information of a control that can
3168 * span multiple codec registers which together
3169 * forms a single signed value in a MSB/LSB manner.
3170 *
3171 * Returns 0 for success.
3172 */
3173int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3174 struct snd_ctl_elem_info *uinfo)
3175{
3176 struct soc_mreg_control *mc =
3177 (struct soc_mreg_control *)kcontrol->private_value;
3178 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3179 uinfo->count = 1;
3180 uinfo->value.integer.min = mc->min;
3181 uinfo->value.integer.max = mc->max;
3182
3183 return 0;
3184}
3185EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3186
3187/**
3188 * snd_soc_get_xr_sx - signed multi register get callback
3189 * @kcontrol: mreg control
3190 * @ucontrol: control element information
3191 *
3192 * Callback to get the value of a control that can span
3193 * multiple codec registers which together forms a single
3194 * signed value in a MSB/LSB manner. The control supports
3195 * specifying total no of bits used to allow for bitfields
3196 * across the multiple codec registers.
3197 *
3198 * Returns 0 for success.
3199 */
3200int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3201 struct snd_ctl_elem_value *ucontrol)
3202{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003203 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003204 struct soc_mreg_control *mc =
3205 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003206 unsigned int regbase = mc->regbase;
3207 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003208 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003209 unsigned int regwmask = (1<<regwshift)-1;
3210 unsigned int invert = mc->invert;
3211 unsigned long mask = (1UL<<mc->nbits)-1;
3212 long min = mc->min;
3213 long max = mc->max;
3214 long val = 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003215 unsigned int regval;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003216 unsigned int i;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003217 int ret;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003218
3219 for (i = 0; i < regcount; i++) {
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003220 ret = snd_soc_component_read(component, regbase+i, &regval);
3221 if (ret)
3222 return ret;
3223 val |= (regval & regwmask) << (regwshift*(regcount-i-1));
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003224 }
3225 val &= mask;
3226 if (min < 0 && val > max)
3227 val |= ~mask;
3228 if (invert)
3229 val = max - val;
3230 ucontrol->value.integer.value[0] = val;
3231
3232 return 0;
3233}
3234EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3235
3236/**
3237 * snd_soc_put_xr_sx - signed multi register get callback
3238 * @kcontrol: mreg control
3239 * @ucontrol: control element information
3240 *
3241 * Callback to set the value of a control that can span
3242 * multiple codec registers which together forms a single
3243 * signed value in a MSB/LSB manner. The control supports
3244 * specifying total no of bits used to allow for bitfields
3245 * across the multiple codec registers.
3246 *
3247 * Returns 0 for success.
3248 */
3249int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3250 struct snd_ctl_elem_value *ucontrol)
3251{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003252 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003253 struct soc_mreg_control *mc =
3254 (struct soc_mreg_control *)kcontrol->private_value;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003255 unsigned int regbase = mc->regbase;
3256 unsigned int regcount = mc->regcount;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003257 unsigned int regwshift = component->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003258 unsigned int regwmask = (1<<regwshift)-1;
3259 unsigned int invert = mc->invert;
3260 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003261 long max = mc->max;
3262 long val = ucontrol->value.integer.value[0];
3263 unsigned int i, regval, regmask;
3264 int err;
3265
3266 if (invert)
3267 val = max - val;
3268 val &= mask;
3269 for (i = 0; i < regcount; i++) {
3270 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3271 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003272 err = snd_soc_component_update_bits(component, regbase+i,
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003273 regmask, regval);
3274 if (err < 0)
3275 return err;
3276 }
3277
3278 return 0;
3279}
3280EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3281
3282/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003283 * snd_soc_get_strobe - strobe get callback
3284 * @kcontrol: mixer control
3285 * @ucontrol: control element information
3286 *
3287 * Callback get the value of a strobe mixer control.
3288 *
3289 * Returns 0 for success.
3290 */
3291int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3292 struct snd_ctl_elem_value *ucontrol)
3293{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003294 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003295 struct soc_mixer_control *mc =
3296 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003297 unsigned int reg = mc->reg;
3298 unsigned int shift = mc->shift;
3299 unsigned int mask = 1 << shift;
3300 unsigned int invert = mc->invert != 0;
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003301 unsigned int val;
3302 int ret;
3303
3304 ret = snd_soc_component_read(component, reg, &val);
3305 if (ret)
3306 return ret;
3307
3308 val &= mask;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003309
3310 if (shift != 0 && val != 0)
3311 val = val >> shift;
3312 ucontrol->value.enumerated.item[0] = val ^ invert;
3313
3314 return 0;
3315}
3316EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3317
3318/**
3319 * snd_soc_put_strobe - strobe put callback
3320 * @kcontrol: mixer control
3321 * @ucontrol: control element information
3322 *
3323 * Callback strobe a register bit to high then low (or the inverse)
3324 * in one pass of a single mixer enum control.
3325 *
3326 * Returns 1 for success.
3327 */
3328int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3329 struct snd_ctl_elem_value *ucontrol)
3330{
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003331 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003332 struct soc_mixer_control *mc =
3333 (struct soc_mixer_control *)kcontrol->private_value;
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003334 unsigned int reg = mc->reg;
3335 unsigned int shift = mc->shift;
3336 unsigned int mask = 1 << shift;
3337 unsigned int invert = mc->invert != 0;
3338 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3339 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3340 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3341 int err;
3342
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003343 err = snd_soc_component_update_bits(component, reg, mask, val1);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003344 if (err < 0)
3345 return err;
3346
Lars-Peter Clausen907fe362014-04-22 13:23:14 +02003347 return snd_soc_component_update_bits(component, reg, mask, val2);
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003348}
3349EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3350
3351/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003352 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3353 * @dai: DAI
3354 * @clk_id: DAI specific clock ID
3355 * @freq: new clock frequency in Hz
3356 * @dir: new clock direction - input/output.
3357 *
3358 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3359 */
3360int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3361 unsigned int freq, int dir)
3362{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003363 if (dai->driver && dai->driver->ops->set_sysclk)
3364 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003365 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003366 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003367 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003368 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003369 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003370}
3371EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3372
3373/**
Mark Brownec4ee522011-03-07 20:58:11 +00003374 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3375 * @codec: CODEC
3376 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003377 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003378 * @freq: new clock frequency in Hz
3379 * @dir: new clock direction - input/output.
3380 *
3381 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3382 */
3383int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003384 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003385{
3386 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003387 return codec->driver->set_sysclk(codec, clk_id, source,
3388 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003389 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003390 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003391}
3392EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3393
3394/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003395 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3396 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003397 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003398 * @div: new clock divisor.
3399 *
3400 * Configures the clock dividers. This is used to derive the best DAI bit and
3401 * frame clocks from the system or master clock. It's best to set the DAI bit
3402 * and frame clocks as low as possible to save system power.
3403 */
3404int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3405 int div_id, int div)
3406{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003407 if (dai->driver && dai->driver->ops->set_clkdiv)
3408 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409 else
3410 return -EINVAL;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3413
3414/**
3415 * snd_soc_dai_set_pll - configure DAI PLL.
3416 * @dai: DAI
3417 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003418 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003419 * @freq_in: PLL input clock frequency in Hz
3420 * @freq_out: requested PLL output clock frequency in Hz
3421 *
3422 * Configures and enables PLL to generate output clock based on input clock.
3423 */
Mark Brown85488032009-09-05 18:52:16 +01003424int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3425 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003426{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003427 if (dai->driver && dai->driver->ops->set_pll)
3428 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003429 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003430 else if (dai->codec && dai->codec->driver->set_pll)
3431 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3432 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003433 else
3434 return -EINVAL;
3435}
3436EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3437
Mark Brownec4ee522011-03-07 20:58:11 +00003438/*
3439 * snd_soc_codec_set_pll - configure codec PLL.
3440 * @codec: CODEC
3441 * @pll_id: DAI specific PLL ID
3442 * @source: DAI specific source for the PLL
3443 * @freq_in: PLL input clock frequency in Hz
3444 * @freq_out: requested PLL output clock frequency in Hz
3445 *
3446 * Configures and enables PLL to generate output clock based on input clock.
3447 */
3448int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3449 unsigned int freq_in, unsigned int freq_out)
3450{
3451 if (codec->driver->set_pll)
3452 return codec->driver->set_pll(codec, pll_id, source,
3453 freq_in, freq_out);
3454 else
3455 return -EINVAL;
3456}
3457EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3458
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003459/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003460 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3461 * @dai: DAI
3462 * @ratio Ratio of BCLK to Sample rate.
3463 *
3464 * Configures the DAI for a preset BCLK to sample rate ratio.
3465 */
3466int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3467{
3468 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3469 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3470 else
3471 return -EINVAL;
3472}
3473EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3474
3475/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003476 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3477 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003478 * @fmt: SND_SOC_DAIFMT_ format value.
3479 *
3480 * Configures the DAI hardware format and clocking.
3481 */
3482int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3483{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003484 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003485 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003486 if (dai->driver->ops->set_fmt == NULL)
3487 return -ENOTSUPP;
3488 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003489}
3490EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3491
3492/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003493 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003494 * @slots: Number of slots in use.
3495 * @tx_mask: bitmask representing active TX slots.
3496 * @rx_mask: bitmask representing active RX slots.
3497 *
3498 * Generates the TDM tx and rx slot default masks for DAI.
3499 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003500static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003501 unsigned int *tx_mask,
3502 unsigned int *rx_mask)
3503{
3504 if (*tx_mask || *rx_mask)
3505 return 0;
3506
3507 if (!slots)
3508 return -EINVAL;
3509
3510 *tx_mask = (1 << slots) - 1;
3511 *rx_mask = (1 << slots) - 1;
3512
3513 return 0;
3514}
3515
3516/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003517 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3518 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003519 * @tx_mask: bitmask representing active TX slots.
3520 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003521 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003522 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003523 *
3524 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3525 * specific.
3526 */
3527int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003528 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003529{
Xiubo Lie5c21512014-03-21 14:17:12 +08003530 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3531 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003532 &tx_mask, &rx_mask);
3533 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003534 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003535
Benoit Cousson88bd8702014-07-08 23:19:34 +02003536 dai->tx_mask = tx_mask;
3537 dai->rx_mask = rx_mask;
3538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003539 if (dai->driver && dai->driver->ops->set_tdm_slot)
3540 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003541 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003542 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003543 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003544}
3545EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3546
3547/**
Barry Song472df3c2009-09-12 01:16:29 +08003548 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3549 * @dai: DAI
3550 * @tx_num: how many TX channels
3551 * @tx_slot: pointer to an array which imply the TX slot number channel
3552 * 0~num-1 uses
3553 * @rx_num: how many RX channels
3554 * @rx_slot: pointer to an array which imply the RX slot number channel
3555 * 0~num-1 uses
3556 *
3557 * configure the relationship between channel number and TDM slot number.
3558 */
3559int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3560 unsigned int tx_num, unsigned int *tx_slot,
3561 unsigned int rx_num, unsigned int *rx_slot)
3562{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003563 if (dai->driver && dai->driver->ops->set_channel_map)
3564 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003565 rx_num, rx_slot);
3566 else
3567 return -EINVAL;
3568}
3569EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3570
3571/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003572 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3573 * @dai: DAI
3574 * @tristate: tristate enable
3575 *
3576 * Tristates the DAI so that others can use it.
3577 */
3578int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3579{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003580 if (dai->driver && dai->driver->ops->set_tristate)
3581 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003582 else
3583 return -EINVAL;
3584}
3585EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3586
3587/**
3588 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3589 * @dai: DAI
3590 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003591 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003592 *
3593 * Mutes the DAI DAC.
3594 */
Mark Brownda183962013-02-06 15:44:07 +00003595int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3596 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003597{
Mark Brownda183962013-02-06 15:44:07 +00003598 if (!dai->driver)
3599 return -ENOTSUPP;
3600
3601 if (dai->driver->ops->mute_stream)
3602 return dai->driver->ops->mute_stream(dai, mute, direction);
3603 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3604 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003605 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003606 else
Mark Brown04570c62012-04-13 19:16:03 +01003607 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003608}
3609EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3610
Benoit Cousson88bd8702014-07-08 23:19:34 +02003611static int snd_soc_init_multicodec(struct snd_soc_card *card,
3612 struct snd_soc_dai_link *dai_link)
3613{
3614 /* Legacy codec/codec_dai link is a single entry in multicodec */
3615 if (dai_link->codec_name || dai_link->codec_of_node ||
3616 dai_link->codec_dai_name) {
3617 dai_link->num_codecs = 1;
3618
3619 dai_link->codecs = devm_kzalloc(card->dev,
3620 sizeof(struct snd_soc_dai_link_component),
3621 GFP_KERNEL);
3622 if (!dai_link->codecs)
3623 return -ENOMEM;
3624
3625 dai_link->codecs[0].name = dai_link->codec_name;
3626 dai_link->codecs[0].of_node = dai_link->codec_of_node;
3627 dai_link->codecs[0].dai_name = dai_link->codec_dai_name;
3628 }
3629
3630 if (!dai_link->codecs) {
3631 dev_err(card->dev, "ASoC: DAI link has no CODECs\n");
3632 return -EINVAL;
3633 }
3634
3635 return 0;
3636}
3637
Mark Brownc5af3a22008-11-28 13:29:45 +00003638/**
3639 * snd_soc_register_card - Register a card with the ASoC core
3640 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003641 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003642 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003643 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303644int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003645{
Benoit Cousson88bd8702014-07-08 23:19:34 +02003646 int i, j, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003647
Mark Brownc5af3a22008-11-28 13:29:45 +00003648 if (!card->name || !card->dev)
3649 return -EINVAL;
3650
Stephen Warren5a504962011-12-21 10:40:59 -07003651 for (i = 0; i < card->num_links; i++) {
3652 struct snd_soc_dai_link *link = &card->dai_link[i];
3653
Benoit Cousson88bd8702014-07-08 23:19:34 +02003654 ret = snd_soc_init_multicodec(card, link);
3655 if (ret) {
3656 dev_err(card->dev, "ASoC: failed to init multicodec\n");
3657 return ret;
Stephen Warren5a504962011-12-21 10:40:59 -07003658 }
Benoit Cousson88bd8702014-07-08 23:19:34 +02003659
3660 for (j = 0; j < link->num_codecs; j++) {
3661 /*
3662 * Codec must be specified by 1 of name or OF node,
3663 * not both or neither.
3664 */
3665 if (!!link->codecs[j].name ==
3666 !!link->codecs[j].of_node) {
3667 dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n",
3668 link->name);
3669 return -EINVAL;
3670 }
3671 /* Codec DAI name must be specified */
3672 if (!link->codecs[j].dai_name) {
3673 dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n",
3674 link->name);
3675 return -EINVAL;
3676 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003677 }
Stephen Warren5a504962011-12-21 10:40:59 -07003678
3679 /*
3680 * Platform may be specified by either name or OF node, but
3681 * can be left unspecified, and a dummy platform will be used.
3682 */
3683 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003684 dev_err(card->dev,
3685 "ASoC: Both platform name/of_node are set for %s\n",
3686 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003687 return -EINVAL;
3688 }
3689
3690 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003691 * CPU device may be specified by either name or OF node, but
3692 * can be left unspecified, and will be matched based on DAI
3693 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003694 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003695 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003696 dev_err(card->dev,
3697 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3698 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003699 return -EINVAL;
3700 }
3701 /*
3702 * At least one of CPU DAI name or CPU device name/node must be
3703 * specified
3704 */
3705 if (!link->cpu_dai_name &&
3706 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003707 dev_err(card->dev,
3708 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3709 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003710 return -EINVAL;
3711 }
3712 }
3713
Mark Browned77cc12011-05-03 18:25:34 +01003714 dev_set_drvdata(card->dev, card);
3715
Stephen Warren111c6412011-01-28 14:26:35 -07003716 snd_soc_initialize_card_lists(card);
3717
Vinod Koul150dd2f2011-01-13 22:48:32 +05303718 soc_init_card_debugfs(card);
3719
Mark Brown181a6892012-03-14 20:18:49 +00003720 card->rtd = devm_kzalloc(card->dev,
3721 sizeof(struct snd_soc_pcm_runtime) *
3722 (card->num_links + card->num_aux_devs),
3723 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003724 if (card->rtd == NULL)
3725 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003726 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003727 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003728
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003729 for (i = 0; i < card->num_links; i++) {
3730 card->rtd[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003731 card->rtd[i].dai_link = &card->dai_link[i];
Benoit Cousson88bd8702014-07-08 23:19:34 +02003732 card->rtd[i].codec_dais = devm_kzalloc(card->dev,
3733 sizeof(struct snd_soc_dai *) *
3734 (card->rtd[i].dai_link->num_codecs),
3735 GFP_KERNEL);
3736 if (card->rtd[i].codec_dais == NULL)
3737 return -ENOMEM;
Lars-Peter Clausen5f3484a2014-07-01 22:13:48 +02003738 }
3739
3740 for (i = 0; i < card->num_aux_devs; i++)
3741 card->rtd_aux[i].card = card;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003742
Mark Browndb432b42011-10-03 21:06:40 +01003743 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003744 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003745 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003746 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003747
Mark Brownb19e6e72012-03-14 21:18:39 +00003748 ret = snd_soc_instantiate_card(card);
3749 if (ret != 0)
3750 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003751
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003752 /* deactivate pins to sleep state */
3753 for (i = 0; i < card->num_rtd; i++) {
Benoit Cousson88bd8702014-07-08 23:19:34 +02003754 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
3755 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
3756 int j;
3757
3758 for (j = 0; j < rtd->num_codecs; j++) {
3759 struct snd_soc_dai *codec_dai = rtd->codec_dais[j];
3760 if (!codec_dai->active)
3761 pinctrl_pm_select_sleep_state(codec_dai->dev);
3762 }
3763
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003764 if (!cpu_dai->active)
3765 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3766 }
3767
Mark Brownb19e6e72012-03-14 21:18:39 +00003768 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003769}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303770EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003771
3772/**
3773 * snd_soc_unregister_card - Unregister a card with the ASoC core
3774 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003775 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003776 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003777 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303778int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003779{
Vinod Koulb0e26482011-01-13 22:48:02 +05303780 if (card->instantiated)
3781 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003782 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003783
3784 return 0;
3785}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303786EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003787
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788/*
3789 * Simplify DAI link configuration by removing ".-1" from device names
3790 * and sanitizing names.
3791 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003792static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793{
3794 char *found, name[NAME_SIZE];
3795 int id1, id2;
3796
3797 if (dev_name(dev) == NULL)
3798 return NULL;
3799
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003800 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003801
3802 /* are we a "%s.%d" name (platform and SPI components) */
3803 found = strstr(name, dev->driver->name);
3804 if (found) {
3805 /* get ID */
3806 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3807
3808 /* discard ID from name if ID == -1 */
3809 if (*id == -1)
3810 found[strlen(dev->driver->name)] = '\0';
3811 }
3812
3813 } else {
3814 /* I2C component devices are named "bus-addr" */
3815 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3816 char tmp[NAME_SIZE];
3817
3818 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003819 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003820
3821 /* sanitize component name for DAI link creation */
3822 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003823 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003824 } else
3825 *id = 0;
3826 }
3827
3828 return kstrdup(name, GFP_KERNEL);
3829}
3830
3831/*
3832 * Simplify DAI link naming for single devices with multiple DAIs by removing
3833 * any ".-1" and using the DAI name (instead of device name).
3834 */
3835static inline char *fmt_multiple_name(struct device *dev,
3836 struct snd_soc_dai_driver *dai_drv)
3837{
3838 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003839 dev_err(dev,
3840 "ASoC: error - multiple DAI %s registered with no name\n",
3841 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003842 return NULL;
3843 }
3844
3845 return kstrdup(dai_drv->name, GFP_KERNEL);
3846}
3847
Mark Brown91151712008-11-30 23:31:24 +00003848/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003849 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003850 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003851 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003852 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003853static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003854{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003855 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003856
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003857 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003858 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3859 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003860 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003861 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003862 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003863 }
Mark Brown91151712008-11-30 23:31:24 +00003864}
Mark Brown91151712008-11-30 23:31:24 +00003865
3866/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003867 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003868 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003869 * @component: The component the DAIs are registered for
3870 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003871 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003872 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3873 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003874 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003875static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003876 struct snd_soc_dai_driver *dai_drv, size_t count,
3877 bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003878{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003879 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003880 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003881 unsigned int i;
3882 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003883
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003884 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003885
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003886 component->dai_drv = dai_drv;
3887 component->num_dai = count;
3888
Mark Brown91151712008-11-30 23:31:24 +00003889 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003890
3891 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003892 if (dai == NULL) {
3893 ret = -ENOMEM;
3894 goto err;
3895 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003896
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003897 /*
3898 * Back in the old days when we still had component-less DAIs,
3899 * instead of having a static name, component-less DAIs would
3900 * inherit the name of the parent device so it is possible to
3901 * register multiple instances of the DAI. We still need to keep
3902 * the same naming style even though those DAIs are not
3903 * component-less anymore.
3904 */
3905 if (count == 1 && legacy_dai_naming) {
3906 dai->name = fmt_single_name(dev, &dai->id);
3907 } else {
3908 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3909 if (dai_drv[i].id)
3910 dai->id = dai_drv[i].id;
3911 else
3912 dai->id = i;
3913 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003914 if (dai->name == NULL) {
3915 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003916 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003917 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003918 }
3919
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003920 dai->component = component;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003921 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003922 dai->driver = &dai_drv[i];
3923 if (!dai->driver->ops)
3924 dai->driver->ops = &null_dai_ops;
3925
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003926 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003927
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003928 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003929 }
3930
3931 return 0;
3932
3933err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003934 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003935
3936 return ret;
3937}
Mark Brown91151712008-11-30 23:31:24 +00003938
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003939static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm,
3940 enum snd_soc_dapm_type type, int subseq)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003941{
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003942 struct snd_soc_component *component = dapm->component;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003943
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003944 component->driver->seq_notifier(component, type, subseq);
3945}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003946
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003947static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm,
3948 int event)
3949{
3950 struct snd_soc_component *component = dapm->component;
3951
3952 return component->driver->stream_event(component, event);
3953}
3954
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003955static int snd_soc_component_initialize(struct snd_soc_component *component,
3956 const struct snd_soc_component_driver *driver, struct device *dev)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003957{
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003958 struct snd_soc_dapm_context *dapm;
3959
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003960 component->name = fmt_single_name(dev, &component->id);
3961 if (!component->name) {
3962 dev_err(dev, "ASoC: Failed to allocate name\n");
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003963 return -ENOMEM;
3964 }
3965
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003966 component->dev = dev;
3967 component->driver = driver;
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003968 component->probe = component->driver->probe;
3969 component->remove = component->driver->remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02003970
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003971 if (!component->dapm_ptr)
3972 component->dapm_ptr = &component->dapm;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003973
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02003974 dapm = component->dapm_ptr;
3975 dapm->dev = dev;
3976 dapm->component = component;
3977 dapm->bias_level = SND_SOC_BIAS_OFF;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02003978 dapm->idle_bias_off = true;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02003979 if (driver->seq_notifier)
3980 dapm->seq_notifier = snd_soc_component_seq_notifier;
3981 if (driver->stream_event)
3982 dapm->stream_event = snd_soc_component_stream_event;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003983
Lars-Peter Clausen61aca562014-08-19 15:51:21 +02003984 component->controls = driver->controls;
3985 component->num_controls = driver->num_controls;
3986 component->dapm_widgets = driver->dapm_widgets;
3987 component->num_dapm_widgets = driver->num_dapm_widgets;
3988 component->dapm_routes = driver->dapm_routes;
3989 component->num_dapm_routes = driver->num_dapm_routes;
3990
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003991 INIT_LIST_HEAD(&component->dai_list);
3992 mutex_init(&component->io_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003993
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02003994 return 0;
3995}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003996
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02003997static void snd_soc_component_init_regmap(struct snd_soc_component *component)
3998{
3999 if (!component->regmap)
4000 component->regmap = dev_get_regmap(component->dev, NULL);
4001 if (component->regmap) {
4002 int val_bytes = regmap_get_val_bytes(component->regmap);
4003 /* Errors are legitimate for non-integer byte multiples */
4004 if (val_bytes > 0)
4005 component->val_bytes = val_bytes;
4006 }
4007}
4008
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004009static void snd_soc_component_add_unlocked(struct snd_soc_component *component)
4010{
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004011 if (!component->write && !component->read)
4012 snd_soc_component_init_regmap(component);
4013
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004014 list_add(&component->list, &component_list);
4015}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004016
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004017static void snd_soc_component_add(struct snd_soc_component *component)
4018{
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004019 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004020 snd_soc_component_add_unlocked(component);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004021 mutex_unlock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004022}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004023
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004024static void snd_soc_component_cleanup(struct snd_soc_component *component)
4025{
4026 snd_soc_unregister_dais(component);
4027 kfree(component->name);
4028}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004029
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004030static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
4031{
4032 list_del(&component->list);
4033}
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004034
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004035static void snd_soc_component_del(struct snd_soc_component *component)
4036{
4037 mutex_lock(&client_mutex);
4038 snd_soc_component_del_unlocked(component);
4039 mutex_unlock(&client_mutex);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004040}
4041
4042int snd_soc_register_component(struct device *dev,
4043 const struct snd_soc_component_driver *cmpnt_drv,
4044 struct snd_soc_dai_driver *dai_drv,
4045 int num_dai)
4046{
4047 struct snd_soc_component *cmpnt;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004048 int ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004049
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004050 cmpnt = kzalloc(sizeof(*cmpnt), GFP_KERNEL);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004051 if (!cmpnt) {
4052 dev_err(dev, "ASoC: Failed to allocate memory\n");
4053 return -ENOMEM;
4054 }
4055
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004056 ret = snd_soc_component_initialize(cmpnt, cmpnt_drv, dev);
4057 if (ret)
4058 goto err_free;
4059
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004060 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004061 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004062
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004063 ret = snd_soc_register_dais(cmpnt, dai_drv, num_dai, true);
4064 if (ret < 0) {
4065 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4066 goto err_cleanup;
4067 }
4068
4069 snd_soc_component_add(cmpnt);
4070
4071 return 0;
4072
4073err_cleanup:
4074 snd_soc_component_cleanup(cmpnt);
4075err_free:
4076 kfree(cmpnt);
4077 return ret;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004078}
4079EXPORT_SYMBOL_GPL(snd_soc_register_component);
4080
4081/**
4082 * snd_soc_unregister_component - Unregister a component from the ASoC core
4083 *
4084 */
4085void snd_soc_unregister_component(struct device *dev)
4086{
4087 struct snd_soc_component *cmpnt;
4088
4089 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01004090 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004091 goto found;
4092 }
4093 return;
4094
4095found:
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004096 snd_soc_component_del(cmpnt);
4097 snd_soc_component_cleanup(cmpnt);
4098 kfree(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004099}
4100EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4101
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004102static int snd_soc_platform_drv_probe(struct snd_soc_component *component)
4103{
4104 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4105
4106 return platform->driver->probe(platform);
4107}
4108
4109static void snd_soc_platform_drv_remove(struct snd_soc_component *component)
4110{
4111 struct snd_soc_platform *platform = snd_soc_component_to_platform(component);
4112
4113 platform->driver->remove(platform);
4114}
4115
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004116/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004117 * snd_soc_add_platform - Add a platform to the ASoC core
4118 * @dev: The parent device for the platform
4119 * @platform: The platform to add
4120 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004121 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004122int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004123 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004124{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004125 int ret;
4126
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004127 ret = snd_soc_component_initialize(&platform->component,
4128 &platform_drv->component_driver, dev);
4129 if (ret)
4130 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004131
4132 platform->dev = dev;
4133 platform->driver = platform_drv;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004134
4135 if (platform_drv->probe)
4136 platform->component.probe = snd_soc_platform_drv_probe;
4137 if (platform_drv->remove)
4138 platform->component.remove = snd_soc_platform_drv_remove;
Mark Brown12a48a8c2008-12-03 19:40:30 +00004139
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004140#ifdef CONFIG_DEBUG_FS
4141 platform->component.debugfs_prefix = "platform";
4142#endif
4143
Mark Brown12a48a8c2008-12-03 19:40:30 +00004144 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004145 snd_soc_component_add_unlocked(&platform->component);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004146 list_add(&platform->list, &platform_list);
4147 mutex_unlock(&client_mutex);
4148
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004149 dev_dbg(dev, "ASoC: Registered platform '%s'\n",
4150 platform->component.name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004151
4152 return 0;
4153}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004154EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4155
4156/**
4157 * snd_soc_register_platform - Register a platform with the ASoC core
4158 *
4159 * @platform: platform to register
4160 */
4161int snd_soc_register_platform(struct device *dev,
4162 const struct snd_soc_platform_driver *platform_drv)
4163{
4164 struct snd_soc_platform *platform;
4165 int ret;
4166
4167 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4168
4169 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4170 if (platform == NULL)
4171 return -ENOMEM;
4172
4173 ret = snd_soc_add_platform(dev, platform, platform_drv);
4174 if (ret)
4175 kfree(platform);
4176
4177 return ret;
4178}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004179EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4180
4181/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004182 * snd_soc_remove_platform - Remove a platform from the ASoC core
4183 * @platform: the platform to remove
4184 */
4185void snd_soc_remove_platform(struct snd_soc_platform *platform)
4186{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004187
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004188 mutex_lock(&client_mutex);
4189 list_del(&platform->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004190 snd_soc_component_del_unlocked(&platform->component);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004191 mutex_unlock(&client_mutex);
4192
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004193 snd_soc_component_cleanup(&platform->component);
4194
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004195 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004196 platform->component.name);
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004197}
4198EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4199
4200struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4201{
4202 struct snd_soc_platform *platform;
4203
4204 list_for_each_entry(platform, &platform_list, list) {
4205 if (dev == platform->dev)
4206 return platform;
4207 }
4208
4209 return NULL;
4210}
4211EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4212
4213/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004214 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4215 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004216 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004217 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004218void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004219{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004220 struct snd_soc_platform *platform;
4221
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004222 platform = snd_soc_lookup_platform(dev);
4223 if (!platform)
4224 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004225
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004226 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004227 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004228}
4229EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4230
Mark Brown151ab222009-05-09 16:22:58 +01004231static u64 codec_format_map[] = {
4232 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4233 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4234 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4235 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4236 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4237 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4238 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4239 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4240 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4241 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4242 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4243 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4244 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4245 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4246 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4247 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4248};
4249
4250/* Fix up the DAI formats for endianness: codecs don't actually see
4251 * the endianness of the data but we're using the CPU format
4252 * definitions which do need to include endianness so we ensure that
4253 * codec DAIs always have both big and little endian variants set.
4254 */
4255static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4256{
4257 int i;
4258
4259 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4260 if (stream->formats & codec_format_map[i])
4261 stream->formats |= codec_format_map[i];
4262}
4263
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004264static int snd_soc_codec_drv_probe(struct snd_soc_component *component)
4265{
4266 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4267
4268 return codec->driver->probe(codec);
4269}
4270
4271static void snd_soc_codec_drv_remove(struct snd_soc_component *component)
4272{
4273 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4274
4275 codec->driver->remove(codec);
4276}
4277
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004278static int snd_soc_codec_drv_write(struct snd_soc_component *component,
4279 unsigned int reg, unsigned int val)
4280{
4281 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4282
4283 return codec->driver->write(codec, reg, val);
4284}
4285
4286static int snd_soc_codec_drv_read(struct snd_soc_component *component,
4287 unsigned int reg, unsigned int *val)
4288{
4289 struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
4290
4291 *val = codec->driver->read(codec, reg);
4292
4293 return 0;
4294}
4295
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004296static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm,
4297 enum snd_soc_bias_level level)
4298{
4299 struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm);
4300
4301 return codec->driver->set_bias_level(codec, level);
4302}
4303
Mark Brown0d0cf002008-12-10 14:32:45 +00004304/**
4305 * snd_soc_register_codec - Register a codec with the ASoC core
4306 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004307 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004308 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004309int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004310 const struct snd_soc_codec_driver *codec_drv,
4311 struct snd_soc_dai_driver *dai_drv,
4312 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004313{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004314 struct snd_soc_codec *codec;
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004315 struct snd_soc_dai *dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004316 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004317
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004318 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004319
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004320 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4321 if (codec == NULL)
4322 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004323
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004324 codec->component.dapm_ptr = &codec->dapm;
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004325 codec->component.codec = codec;
Lars-Peter Clausence0fc932014-06-16 18:13:06 +02004326
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004327 ret = snd_soc_component_initialize(&codec->component,
4328 &codec_drv->component_driver, dev);
4329 if (ret)
4330 goto err_free;
Mark Brown151ab222009-05-09 16:22:58 +01004331
Lars-Peter Clausenf1d45cc32014-08-19 15:51:19 +02004332 if (codec_drv->controls) {
4333 codec->component.controls = codec_drv->controls;
4334 codec->component.num_controls = codec_drv->num_controls;
4335 }
4336 if (codec_drv->dapm_widgets) {
4337 codec->component.dapm_widgets = codec_drv->dapm_widgets;
4338 codec->component.num_dapm_widgets = codec_drv->num_dapm_widgets;
4339 }
4340 if (codec_drv->dapm_routes) {
4341 codec->component.dapm_routes = codec_drv->dapm_routes;
4342 codec->component.num_dapm_routes = codec_drv->num_dapm_routes;
4343 }
4344
4345 if (codec_drv->probe)
4346 codec->component.probe = snd_soc_codec_drv_probe;
4347 if (codec_drv->remove)
4348 codec->component.remove = snd_soc_codec_drv_remove;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004349 if (codec_drv->write)
4350 codec->component.write = snd_soc_codec_drv_write;
4351 if (codec_drv->read)
4352 codec->component.read = snd_soc_codec_drv_read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004353 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004354 codec->dapm.codec = codec;
Lars-Peter Clausen5819c2f2014-08-24 15:36:55 +02004355 codec->dapm.idle_bias_off = codec_drv->idle_bias_off;
Lars-Peter Clausen14e8bde2014-06-16 18:13:08 +02004356 if (codec_drv->seq_notifier)
4357 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Lars-Peter Clausen68f831c2014-06-16 18:13:05 +02004358 if (codec_drv->set_bias_level)
4359 codec->dapm.set_bias_level = snd_soc_codec_set_bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004360 codec->dev = dev;
4361 codec->driver = codec_drv;
Lars-Peter Clausene2c330b2014-04-22 13:23:13 +02004362 codec->component.val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004363 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004364
Lars-Peter Clausen81c7cfd2014-08-19 15:51:18 +02004365#ifdef CONFIG_DEBUG_FS
4366 codec->component.init_debugfs = soc_init_codec_debugfs;
4367 codec->component.debugfs_prefix = "codec";
4368#endif
4369
Lars-Peter Clausen886f5692014-08-19 15:51:28 +02004370 if (codec_drv->get_regmap)
4371 codec->component.regmap = codec_drv->get_regmap(dev);
Xiubo Lia39f75f2014-03-26 13:40:23 +08004372
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004373 for (i = 0; i < num_dai; i++) {
4374 fixup_codec_formats(&dai_drv[i].playback);
4375 fixup_codec_formats(&dai_drv[i].capture);
4376 }
4377
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004378 ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false);
4379 if (ret < 0) {
4380 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4381 goto err_cleanup;
4382 }
4383
4384 list_for_each_entry(dai, &codec->component.dai_list, list)
4385 dai->codec = codec;
4386
Mark Brown054880f2012-04-09 17:29:19 +01004387 mutex_lock(&client_mutex);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004388 snd_soc_component_add_unlocked(&codec->component);
Mark Brown054880f2012-04-09 17:29:19 +01004389 list_add(&codec->list, &codec_list);
4390 mutex_unlock(&client_mutex);
4391
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004392 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n",
4393 codec->component.name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004394 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004395
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004396err_cleanup:
4397 snd_soc_component_cleanup(&codec->component);
4398err_free:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004399 kfree(codec);
4400 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004401}
4402EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4403
4404/**
4405 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4406 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004407 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004408 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004409void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004410{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004411 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004412
4413 list_for_each_entry(codec, &codec_list, list) {
4414 if (dev == codec->dev)
4415 goto found;
4416 }
4417 return;
4418
4419found:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004420
Mark Brown0d0cf002008-12-10 14:32:45 +00004421 mutex_lock(&client_mutex);
4422 list_del(&codec->list);
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004423 snd_soc_component_del_unlocked(&codec->component);
Mark Brown0d0cf002008-12-10 14:32:45 +00004424 mutex_unlock(&client_mutex);
4425
Lars-Peter Clausenf4333202014-06-16 18:13:02 +02004426 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n",
4427 codec->component.name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004428
Lars-Peter Clausenbb131092014-06-16 18:13:03 +02004429 snd_soc_component_cleanup(&codec->component);
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004430 snd_soc_cache_exit(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004431 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004432}
4433EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4434
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004435/* Retrieve a card's name from device tree */
4436int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4437 const char *propname)
4438{
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304439 struct device_node *np;
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004440 int ret;
4441
Tushar Behera7e07e7c2014-07-04 14:23:00 +05304442 if (!card->dev) {
4443 pr_err("card->dev is not set before calling %s\n", __func__);
4444 return -EINVAL;
4445 }
4446
4447 np = card->dev->of_node;
4448
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004449 ret = of_property_read_string_index(np, propname, 0, &card->name);
4450 /*
4451 * EINVAL means the property does not exist. This is fine providing
4452 * card->name was previously set, which is checked later in
4453 * snd_soc_register_card.
4454 */
4455 if (ret < 0 && ret != -EINVAL) {
4456 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004457 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004458 propname, ret);
4459 return ret;
4460 }
4461
4462 return 0;
4463}
4464EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4465
Xiubo Li9a6d4862014-02-08 15:59:52 +08004466static const struct snd_soc_dapm_widget simple_widgets[] = {
4467 SND_SOC_DAPM_MIC("Microphone", NULL),
4468 SND_SOC_DAPM_LINE("Line", NULL),
4469 SND_SOC_DAPM_HP("Headphone", NULL),
4470 SND_SOC_DAPM_SPK("Speaker", NULL),
4471};
4472
4473int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4474 const char *propname)
4475{
4476 struct device_node *np = card->dev->of_node;
4477 struct snd_soc_dapm_widget *widgets;
4478 const char *template, *wname;
4479 int i, j, num_widgets, ret;
4480
4481 num_widgets = of_property_count_strings(np, propname);
4482 if (num_widgets < 0) {
4483 dev_err(card->dev,
4484 "ASoC: Property '%s' does not exist\n", propname);
4485 return -EINVAL;
4486 }
4487 if (num_widgets & 1) {
4488 dev_err(card->dev,
4489 "ASoC: Property '%s' length is not even\n", propname);
4490 return -EINVAL;
4491 }
4492
4493 num_widgets /= 2;
4494 if (!num_widgets) {
4495 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4496 propname);
4497 return -EINVAL;
4498 }
4499
4500 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4501 GFP_KERNEL);
4502 if (!widgets) {
4503 dev_err(card->dev,
4504 "ASoC: Could not allocate memory for widgets\n");
4505 return -ENOMEM;
4506 }
4507
4508 for (i = 0; i < num_widgets; i++) {
4509 ret = of_property_read_string_index(np, propname,
4510 2 * i, &template);
4511 if (ret) {
4512 dev_err(card->dev,
4513 "ASoC: Property '%s' index %d read error:%d\n",
4514 propname, 2 * i, ret);
4515 return -EINVAL;
4516 }
4517
4518 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4519 if (!strncmp(template, simple_widgets[j].name,
4520 strlen(simple_widgets[j].name))) {
4521 widgets[i] = simple_widgets[j];
4522 break;
4523 }
4524 }
4525
4526 if (j >= ARRAY_SIZE(simple_widgets)) {
4527 dev_err(card->dev,
4528 "ASoC: DAPM widget '%s' is not supported\n",
4529 template);
4530 return -EINVAL;
4531 }
4532
4533 ret = of_property_read_string_index(np, propname,
4534 (2 * i) + 1,
4535 &wname);
4536 if (ret) {
4537 dev_err(card->dev,
4538 "ASoC: Property '%s' index %d read error:%d\n",
4539 propname, (2 * i) + 1, ret);
4540 return -EINVAL;
4541 }
4542
4543 widgets[i].name = wname;
4544 }
4545
4546 card->dapm_widgets = widgets;
4547 card->num_dapm_widgets = num_widgets;
4548
4549 return 0;
4550}
4551EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4552
Xiubo Li89c67852014-02-14 09:34:35 +08004553int snd_soc_of_parse_tdm_slot(struct device_node *np,
4554 unsigned int *slots,
4555 unsigned int *slot_width)
4556{
4557 u32 val;
4558 int ret;
4559
4560 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4561 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4562 if (ret)
4563 return ret;
4564
4565 if (slots)
4566 *slots = val;
4567 }
4568
4569 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4570 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4571 if (ret)
4572 return ret;
4573
4574 if (slot_width)
4575 *slot_width = val;
4576 }
4577
4578 return 0;
4579}
4580EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4581
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004582int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4583 const char *propname)
4584{
4585 struct device_node *np = card->dev->of_node;
4586 int num_routes;
4587 struct snd_soc_dapm_route *routes;
4588 int i, ret;
4589
4590 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004591 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004592 dev_err(card->dev,
4593 "ASoC: Property '%s' does not exist or its length is not even\n",
4594 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004595 return -EINVAL;
4596 }
4597 num_routes /= 2;
4598 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004599 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004600 propname);
4601 return -EINVAL;
4602 }
4603
4604 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4605 GFP_KERNEL);
4606 if (!routes) {
4607 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004608 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004609 return -EINVAL;
4610 }
4611
4612 for (i = 0; i < num_routes; i++) {
4613 ret = of_property_read_string_index(np, propname,
4614 2 * i, &routes[i].sink);
4615 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004616 dev_err(card->dev,
4617 "ASoC: Property '%s' index %d could not be read: %d\n",
4618 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004619 return -EINVAL;
4620 }
4621 ret = of_property_read_string_index(np, propname,
4622 (2 * i) + 1, &routes[i].source);
4623 if (ret) {
4624 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004625 "ASoC: Property '%s' index %d could not be read: %d\n",
4626 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004627 return -EINVAL;
4628 }
4629 }
4630
4631 card->num_dapm_routes = num_routes;
4632 card->dapm_routes = routes;
4633
4634 return 0;
4635}
4636EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4637
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004638unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
Jyri Sarha389cb832014-03-24 12:15:24 +02004639 const char *prefix,
4640 struct device_node **bitclkmaster,
4641 struct device_node **framemaster)
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004642{
4643 int ret, i;
4644 char prop[128];
4645 unsigned int format = 0;
4646 int bit, frame;
4647 const char *str;
4648 struct {
4649 char *name;
4650 unsigned int val;
4651 } of_fmt_table[] = {
4652 { "i2s", SND_SOC_DAIFMT_I2S },
4653 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4654 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4655 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4656 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4657 { "ac97", SND_SOC_DAIFMT_AC97 },
4658 { "pdm", SND_SOC_DAIFMT_PDM},
4659 { "msb", SND_SOC_DAIFMT_MSB },
4660 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004661 };
4662
4663 if (!prefix)
4664 prefix = "";
4665
4666 /*
4667 * check "[prefix]format = xxx"
4668 * SND_SOC_DAIFMT_FORMAT_MASK area
4669 */
4670 snprintf(prop, sizeof(prop), "%sformat", prefix);
4671 ret = of_property_read_string(np, prop, &str);
4672 if (ret == 0) {
4673 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4674 if (strcmp(str, of_fmt_table[i].name) == 0) {
4675 format |= of_fmt_table[i].val;
4676 break;
4677 }
4678 }
4679 }
4680
4681 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004682 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004683 * SND_SOC_DAIFMT_CLOCK_MASK area
4684 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004685 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4686 if (of_get_property(np, prop, NULL))
4687 format |= SND_SOC_DAIFMT_CONT;
4688 else
4689 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004690
4691 /*
4692 * check "[prefix]bitclock-inversion"
4693 * check "[prefix]frame-inversion"
4694 * SND_SOC_DAIFMT_INV_MASK area
4695 */
4696 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4697 bit = !!of_get_property(np, prop, NULL);
4698
4699 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4700 frame = !!of_get_property(np, prop, NULL);
4701
4702 switch ((bit << 4) + frame) {
4703 case 0x11:
4704 format |= SND_SOC_DAIFMT_IB_IF;
4705 break;
4706 case 0x10:
4707 format |= SND_SOC_DAIFMT_IB_NF;
4708 break;
4709 case 0x01:
4710 format |= SND_SOC_DAIFMT_NB_IF;
4711 break;
4712 default:
4713 /* SND_SOC_DAIFMT_NB_NF is default */
4714 break;
4715 }
4716
4717 /*
4718 * check "[prefix]bitclock-master"
4719 * check "[prefix]frame-master"
4720 * SND_SOC_DAIFMT_MASTER_MASK area
4721 */
4722 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4723 bit = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004724 if (bit && bitclkmaster)
4725 *bitclkmaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004726
4727 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4728 frame = !!of_get_property(np, prop, NULL);
Jyri Sarha389cb832014-03-24 12:15:24 +02004729 if (frame && framemaster)
4730 *framemaster = of_parse_phandle(np, prop, 0);
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004731
4732 switch ((bit << 4) + frame) {
4733 case 0x11:
4734 format |= SND_SOC_DAIFMT_CBM_CFM;
4735 break;
4736 case 0x10:
4737 format |= SND_SOC_DAIFMT_CBM_CFS;
4738 break;
4739 case 0x01:
4740 format |= SND_SOC_DAIFMT_CBS_CFM;
4741 break;
4742 default:
4743 format |= SND_SOC_DAIFMT_CBS_CFS;
4744 break;
4745 }
4746
4747 return format;
4748}
4749EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4750
Kuninori Morimotocb470082013-09-10 17:39:56 -07004751int snd_soc_of_get_dai_name(struct device_node *of_node,
4752 const char **dai_name)
4753{
4754 struct snd_soc_component *pos;
4755 struct of_phandle_args args;
4756 int ret;
4757
4758 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4759 "#sound-dai-cells", 0, &args);
4760 if (ret)
4761 return ret;
4762
4763 ret = -EPROBE_DEFER;
4764
4765 mutex_lock(&client_mutex);
4766 list_for_each_entry(pos, &component_list, list) {
4767 if (pos->dev->of_node != args.np)
4768 continue;
4769
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004770 if (pos->driver->of_xlate_dai_name) {
4771 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4772 } else {
4773 int id = -1;
4774
4775 switch (args.args_count) {
4776 case 0:
4777 id = 0; /* same as dai_drv[0] */
4778 break;
4779 case 1:
4780 id = args.args[0];
4781 break;
4782 default:
4783 /* not supported */
4784 break;
4785 }
4786
4787 if (id < 0 || id >= pos->num_dai) {
4788 ret = -EINVAL;
Nicolin Chen3dcba282014-04-21 19:14:46 +08004789 continue;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004790 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004791
4792 ret = 0;
4793
4794 *dai_name = pos->dai_drv[id].name;
4795 if (!*dai_name)
4796 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004797 }
4798
Kuninori Morimotocb470082013-09-10 17:39:56 -07004799 break;
4800 }
4801 mutex_unlock(&client_mutex);
4802
4803 of_node_put(args.np);
4804
4805 return ret;
4806}
4807EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4808
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004809static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004810{
Mark Brown384c89e2008-12-03 17:34:03 +00004811#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004812 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4813 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004814 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004815 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004816 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004817
Mark Brown8a9dab12011-01-10 22:25:21 +00004818 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004819 &codec_list_fops))
4820 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4821
Mark Brown8a9dab12011-01-10 22:25:21 +00004822 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004823 &dai_list_fops))
4824 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004825
Mark Brown8a9dab12011-01-10 22:25:21 +00004826 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004827 &platform_list_fops))
4828 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004829#endif
4830
Mark Brownfb257892011-04-28 10:57:54 +01004831 snd_soc_util_init();
4832
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004833 return platform_driver_register(&soc_driver);
4834}
Mark Brown4abe8e12010-10-12 17:41:03 +01004835module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004836
Mark Brown7d8c16a2008-11-30 22:11:24 +00004837static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004838{
Mark Brownfb257892011-04-28 10:57:54 +01004839 snd_soc_util_exit();
4840
Mark Brown384c89e2008-12-03 17:34:03 +00004841#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004842 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004843#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004844 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004845}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004846module_exit(snd_soc_exit);
4847
4848/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004849MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004850MODULE_DESCRIPTION("ALSA SoC Core");
4851MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004852MODULE_ALIAS("platform:soc-audio");