blob: 41bd243485205cff0042a4026b73208fe51b154f [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
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200280 dev_warn(codec->dev,
281 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000282 return;
283 }
284
Mark Brownaaee8ef2011-01-26 20:53:50 +0000285 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
286 &codec->cache_sync);
287 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
288 &codec->cache_only);
289
Mark Brown2624d5f2009-11-03 21:56:13 +0000290 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
291 codec->debugfs_codec_root,
292 codec, &codec_reg_fops);
293 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200294 dev_warn(codec->dev,
295 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000296
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200297 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000298}
299
300static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
301{
302 debugfs_remove_recursive(codec->debugfs_codec_root);
303}
304
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000305static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
306{
307 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
308
309 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
310 debugfs_card_root);
311 if (!platform->debugfs_platform_root) {
312 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000313 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000314 return;
315 }
316
317 snd_soc_dapm_debugfs_init(&platform->dapm,
318 platform->debugfs_platform_root);
319}
320
321static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
322{
323 debugfs_remove_recursive(platform->debugfs_platform_root);
324}
325
Mark Brownc3c5a192010-09-15 18:15:14 +0100326static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
327 size_t count, loff_t *ppos)
328{
329 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100330 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100331 struct snd_soc_codec *codec;
332
333 if (!buf)
334 return -ENOMEM;
335
Mark Brown2b194f9d2010-10-13 10:52:16 +0100336 list_for_each_entry(codec, &codec_list, list) {
337 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
338 codec->name);
339 if (len >= 0)
340 ret += len;
341 if (ret > PAGE_SIZE) {
342 ret = PAGE_SIZE;
343 break;
344 }
345 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100346
347 if (ret >= 0)
348 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
349
350 kfree(buf);
351
352 return ret;
353}
354
355static const struct file_operations codec_list_fops = {
356 .read = codec_list_read_file,
357 .llseek = default_llseek,/* read accesses f_pos */
358};
359
Mark Brownf3208782010-09-15 18:19:07 +0100360static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
361 size_t count, loff_t *ppos)
362{
363 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100364 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100365 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100366 struct snd_soc_dai *dai;
367
368 if (!buf)
369 return -ENOMEM;
370
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100371 list_for_each_entry(component, &component_list, list) {
372 list_for_each_entry(dai, &component->dai_list, list) {
373 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
374 dai->name);
375 if (len >= 0)
376 ret += len;
377 if (ret > PAGE_SIZE) {
378 ret = PAGE_SIZE;
379 break;
380 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100381 }
382 }
Mark Brownf3208782010-09-15 18:19:07 +0100383
Mark Brown2b194f9d2010-10-13 10:52:16 +0100384 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100385
386 kfree(buf);
387
388 return ret;
389}
390
391static const struct file_operations dai_list_fops = {
392 .read = dai_list_read_file,
393 .llseek = default_llseek,/* read accesses f_pos */
394};
395
Mark Brown19c7ac22010-09-15 18:22:40 +0100396static ssize_t platform_list_read_file(struct file *file,
397 char __user *user_buf,
398 size_t count, loff_t *ppos)
399{
400 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100401 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100402 struct snd_soc_platform *platform;
403
404 if (!buf)
405 return -ENOMEM;
406
Mark Brown2b194f9d2010-10-13 10:52:16 +0100407 list_for_each_entry(platform, &platform_list, list) {
408 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
409 platform->name);
410 if (len >= 0)
411 ret += len;
412 if (ret > PAGE_SIZE) {
413 ret = PAGE_SIZE;
414 break;
415 }
416 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100417
Mark Brown2b194f9d2010-10-13 10:52:16 +0100418 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100419
420 kfree(buf);
421
422 return ret;
423}
424
425static const struct file_operations platform_list_fops = {
426 .read = platform_list_read_file,
427 .llseek = default_llseek,/* read accesses f_pos */
428};
429
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200430static void soc_init_card_debugfs(struct snd_soc_card *card)
431{
432 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000433 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200434 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200435 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100436 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200437 return;
438 }
439
440 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
441 card->debugfs_card_root,
442 &card->pop_time);
443 if (!card->debugfs_pop_time)
444 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000445 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200446}
447
448static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
449{
450 debugfs_remove_recursive(card->debugfs_card_root);
451}
452
Mark Brown2624d5f2009-11-03 21:56:13 +0000453#else
454
455static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
456{
457}
458
459static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
460{
461}
Axel Linb95fccb2010-11-09 17:06:44 +0800462
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000463static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
464{
465}
466
467static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
468{
469}
470
Axel Linb95fccb2010-11-09 17:06:44 +0800471static inline void soc_init_card_debugfs(struct snd_soc_card *card)
472{
473}
474
475static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
476{
477}
Mark Brown2624d5f2009-11-03 21:56:13 +0000478#endif
479
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100480struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
481 const char *dai_link, int stream)
482{
483 int i;
484
485 for (i = 0; i < card->num_links; i++) {
486 if (card->rtd[i].dai_link->no_pcm &&
487 !strcmp(card->rtd[i].dai_link->name, dai_link))
488 return card->rtd[i].pcm->streams[stream].substream;
489 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000490 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100491 return NULL;
492}
493EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
494
495struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
496 const char *dai_link)
497{
498 int i;
499
500 for (i = 0; i < card->num_links; i++) {
501 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
502 return &card->rtd[i];
503 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000504 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100505 return NULL;
506}
507EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
508
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200509#ifdef CONFIG_SND_SOC_AC97_BUS
510/* unregister ac97 codec */
511static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
512{
513 if (codec->ac97->dev.bus)
514 device_unregister(&codec->ac97->dev);
515 return 0;
516}
517
518/* stop no dev release warning */
519static void soc_ac97_device_release(struct device *dev){}
520
521/* register ac97 codec to bus */
522static int soc_ac97_dev_register(struct snd_soc_codec *codec)
523{
524 int err;
525
526 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100527 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200528 codec->ac97->dev.release = soc_ac97_device_release;
529
Kay Sieversbb072bf2008-11-02 03:50:35 +0100530 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000531 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200532 err = device_register(&codec->ac97->dev);
533 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000534 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 codec->ac97->dev.bus = NULL;
536 return err;
537 }
538 return 0;
539}
540#endif
541
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100542static void codec2codec_close_delayed_work(struct work_struct *work)
543{
544 /* Currently nothing to do for c2c links
545 * Since c2c links are internal nodes in the DAPM graph and
546 * don't interface with the outside world or application layer
547 * we don't have to do any special handling on close.
548 */
549}
550
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000551#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200552/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000553int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200554{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000555 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200556 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200557 int i;
558
Daniel Macke3509ff2009-06-03 17:44:49 +0200559 /* If the initialization of this soc device failed, there is no codec
560 * associated with it. Just bail out in this case.
561 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000562 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200563 return 0;
564
Andy Green6ed25972008-06-13 16:24:05 +0100565 /* Due to the resume being scheduled into a workqueue we could
566 * suspend before that's finished - wait for it to complete.
567 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000568 snd_power_lock(card->snd_card);
569 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
570 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100571
572 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100574
Mark Browna00f90f2010-12-02 16:24:24 +0000575 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000576 for (i = 0; i < card->num_rtd; i++) {
577 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
578 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100579
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100581 continue;
582
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 if (drv->ops->digital_mute && dai->playback_active)
584 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200585 }
586
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100587 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000588 for (i = 0; i < card->num_rtd; i++) {
589 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100590 continue;
591
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000592 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100593 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100594
Mark Brown87506542008-11-18 20:50:34 +0000595 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000596 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200597
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 for (i = 0; i < card->num_rtd; i++) {
599 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
600 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100601
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000602 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100603 continue;
604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
606 cpu_dai->driver->suspend(cpu_dai);
607 if (platform->driver->suspend && !platform->suspended) {
608 platform->driver->suspend(cpu_dai);
609 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100610 }
611 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200612
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000613 /* close any waiting streams and save state */
614 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700615 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200616 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000617 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620
621 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100622 continue;
623
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800624 snd_soc_dapm_stream_event(&card->rtd[i],
625 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800626 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800628 snd_soc_dapm_stream_event(&card->rtd[i],
629 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800630 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000631 }
632
Mark Browne2d32ff2012-08-31 17:38:32 -0700633 /* Recheck all analogue paths too */
634 dapm_mark_io_dirty(&card->dapm);
635 snd_soc_dapm_sync(&card->dapm);
636
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000637 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200638 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000639 /* If there are paths active then the CODEC will be held with
640 * bias _ON and should not be suspended. */
641 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200642 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000643 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000644 /*
645 * If the CODEC is capable of idle
646 * bias off then being in STANDBY
647 * means it's doing something,
648 * otherwise fall through.
649 */
650 if (codec->dapm.idle_bias_off) {
651 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200652 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000653 break;
654 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000655 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100656 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000657 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900658 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800659 if (codec->using_regmap)
660 regcache_mark_dirty(codec->control_data);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800661 /* deactivate pins to sleep state */
662 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000663 break;
664 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200665 dev_dbg(codec->dev,
666 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000667 break;
668 }
669 }
670 }
671
672 for (i = 0; i < card->num_rtd; i++) {
673 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
674
675 if (card->rtd[i].dai_link->ignore_suspend)
676 continue;
677
678 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
679 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800680
681 /* deactivate pins to sleep state */
682 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200683 }
684
Mark Brown87506542008-11-18 20:50:34 +0000685 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000686 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200687
688 return 0;
689}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000690EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200691
Andy Green6ed25972008-06-13 16:24:05 +0100692/* deferred resume work, so resume can complete before we finished
693 * setting our codec back up, which can be very slow on I2C
694 */
695static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200696{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000697 struct snd_soc_card *card =
698 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200699 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200700 int i;
701
Andy Green6ed25972008-06-13 16:24:05 +0100702 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
703 * so userspace apps are blocked from touching us
704 */
705
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000706 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100707
Mark Brown99497882010-05-07 20:24:05 +0100708 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000709 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100710
Mark Brown87506542008-11-18 20:50:34 +0000711 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000712 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 /* resume AC97 DAIs */
715 for (i = 0; i < card->num_rtd; i++) {
716 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100719 continue;
720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
722 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200723 }
724
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200725 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000726 /* If the CODEC was idle over suspend then it will have been
727 * left with bias OFF or STANDBY and suspended so we must now
728 * resume. Otherwise the suspend was suppressed.
729 */
730 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200731 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000732 case SND_SOC_BIAS_STANDBY:
733 case SND_SOC_BIAS_OFF:
734 codec->driver->resume(codec);
735 codec->suspended = 0;
736 break;
737 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200738 dev_dbg(codec->dev,
739 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000740 break;
741 }
Mark Brown1547aba2010-05-07 21:11:40 +0100742 }
743 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200744
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000745 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100746
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100748 continue;
749
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800750 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000751 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800752 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000753
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800754 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000755 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800756 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200757 }
758
Mark Brown3ff3f642008-05-19 12:32:25 +0200759 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760 for (i = 0; i < card->num_rtd; i++) {
761 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
762 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100763
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000764 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100765 continue;
766
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 if (drv->ops->digital_mute && dai->playback_active)
768 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200769 }
770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 for (i = 0; i < card->num_rtd; i++) {
772 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
773 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100774
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000775 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100776 continue;
777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
779 cpu_dai->driver->resume(cpu_dai);
780 if (platform->driver->resume && platform->suspended) {
781 platform->driver->resume(cpu_dai);
782 platform->suspended = 0;
783 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784 }
785
Mark Brown87506542008-11-18 20:50:34 +0000786 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000787 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200788
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000789 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100790
791 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700793
794 /* Recheck all analogue paths too */
795 dapm_mark_io_dirty(&card->dapm);
796 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100797}
798
799/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000800int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100801{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000802 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600803 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200804
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800805 /* If the initialization of this soc device failed, there is no codec
806 * associated with it. Just bail out in this case.
807 */
808 if (list_empty(&card->codec_dev_list))
809 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++) {
813 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
814 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
815 if (cpu_dai->active)
816 pinctrl_pm_select_default_state(cpu_dai->dev);
817 if (codec_dai->active)
818 pinctrl_pm_select_default_state(codec_dai->dev);
819 }
820
Mark Brown64ab9ba2009-03-31 11:27:03 +0100821 /* AC97 devices might have other drivers hanging off them so
822 * need to resume immediately. Other drivers don't have that
823 * problem and may take a substantial amount of time to resume
824 * due to I/O costs and anti-pop so handle them out of line.
825 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000826 for (i = 0; i < card->num_rtd; i++) {
827 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600828 ac97_control |= cpu_dai->driver->ac97_control;
829 }
830 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000831 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600832 soc_resume_deferred(&card->deferred_resume_work);
833 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000834 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000836 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100837 }
Andy Green6ed25972008-06-13 16:24:05 +0100838
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200839 return 0;
840}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000841EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200842#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000843#define snd_soc_suspend NULL
844#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200845#endif
846
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100847static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800848};
849
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000850static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200851{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000852 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
853 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100854 struct snd_soc_component *component;
Mark Brownfe3e78e2009-11-03 22:13:13 +0000855 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000856 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100858 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200859
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000860 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000861
Mark Brownb19e6e72012-03-14 21:18:39 +0000862 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100863 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600864 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100865 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600866 continue;
867 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100868 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600869 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100870 list_for_each_entry(cpu_dai, &component->dai_list, list) {
871 if (dai_link->cpu_dai_name &&
872 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
873 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700874
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100875 rtd->cpu_dai = cpu_dai;
876 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000877 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000878
879 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000880 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000881 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000882 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000883 }
884
Mark Brownb19e6e72012-03-14 21:18:39 +0000885 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000886 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700887 if (dai_link->codec_of_node) {
888 if (codec->dev->of_node != dai_link->codec_of_node)
889 continue;
890 } else {
891 if (strcmp(codec->name, dai_link->codec_name))
892 continue;
893 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000894
Stephen Warren2610ab72011-12-07 13:58:27 -0700895 rtd->codec = codec;
896
897 /*
898 * CODEC found, so find CODEC DAI from registered DAIs from
899 * this CODEC
900 */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100901 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
902 if (!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700903 rtd->codec_dai = codec_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100904 break;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000905 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000906 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000907
908 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000909 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700910 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000911 return -EPROBE_DEFER;
912 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000914
Mark Brownb19e6e72012-03-14 21:18:39 +0000915 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000916 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000917 dai_link->codec_name);
918 return -EPROBE_DEFER;
919 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100920
921 /* if there's no platform we match on the empty platform */
922 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700923 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100924 platform_name = "snd-soc-dummy";
925
Mark Brownb19e6e72012-03-14 21:18:39 +0000926 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700928 if (dai_link->platform_of_node) {
929 if (platform->dev->of_node !=
930 dai_link->platform_of_node)
931 continue;
932 } else {
933 if (strcmp(platform->name, platform_name))
934 continue;
935 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700936
937 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000939 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000940 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000941 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000942 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000943 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000944
945 card->num_rtd++;
946
947 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948}
949
Stephen Warrend12cd192012-06-08 12:34:22 -0600950static int soc_remove_platform(struct snd_soc_platform *platform)
951{
952 int ret;
953
954 if (platform->driver->remove) {
955 ret = platform->driver->remove(platform);
956 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000957 dev_err(platform->dev, "ASoC: failed to remove %d\n",
958 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600959 }
960
961 /* Make sure all DAPM widgets are freed */
962 snd_soc_dapm_free(&platform->dapm);
963
964 soc_cleanup_platform_debugfs(platform);
965 platform->probed = 0;
966 list_del(&platform->card_list);
967 module_put(platform->dev->driver->owner);
968
969 return 0;
970}
971
Jarkko Nikula589c3562010-12-06 16:27:07 +0200972static void soc_remove_codec(struct snd_soc_codec *codec)
973{
974 int err;
975
976 if (codec->driver->remove) {
977 err = codec->driver->remove(codec);
978 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000979 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200980 }
981
982 /* Make sure all DAPM widgets are freed */
983 snd_soc_dapm_free(&codec->dapm);
984
985 soc_cleanup_codec_debugfs(codec);
986 codec->probed = 0;
987 list_del(&codec->card_list);
988 module_put(codec->dev->driver->owner);
989}
990
Stephen Warren62ae68f2012-06-08 12:34:23 -0600991static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000992{
993 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000994 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
995 int err;
996
997 /* unregister the rtd device */
998 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800999 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1000 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1001 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001002 rtd->dev_registered = 0;
1003 }
1004
1005 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001006 if (codec_dai && codec_dai->probed &&
1007 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001008 if (codec_dai->driver->remove) {
1009 err = codec_dai->driver->remove(codec_dai);
1010 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001011 dev_err(codec_dai->dev,
1012 "ASoC: failed to remove %s: %d\n",
1013 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001014 }
1015 codec_dai->probed = 0;
1016 list_del(&codec_dai->card_list);
1017 }
1018
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001019 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001020 if (cpu_dai && cpu_dai->probed &&
1021 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001022 if (cpu_dai->driver->remove) {
1023 err = cpu_dai->driver->remove(cpu_dai);
1024 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001025 dev_err(cpu_dai->dev,
1026 "ASoC: failed to remove %s: %d\n",
1027 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001028 }
1029 cpu_dai->probed = 0;
1030 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001031
Stephen Warren18d75642012-06-08 12:34:21 -06001032 if (!cpu_dai->codec) {
1033 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001034 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001035 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001036 }
1037}
1038
Stephen Warren62ae68f2012-06-08 12:34:23 -06001039static void soc_remove_link_components(struct snd_soc_card *card, int num,
1040 int order)
1041{
1042 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1043 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1044 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1045 struct snd_soc_platform *platform = rtd->platform;
1046 struct snd_soc_codec *codec;
1047
1048 /* remove the platform */
1049 if (platform && platform->probed &&
1050 platform->driver->remove_order == order) {
1051 soc_remove_platform(platform);
1052 }
1053
1054 /* remove the CODEC-side CODEC */
1055 if (codec_dai) {
1056 codec = codec_dai->codec;
1057 if (codec && codec->probed &&
1058 codec->driver->remove_order == order)
1059 soc_remove_codec(codec);
1060 }
1061
1062 /* remove any CPU-side CODEC */
1063 if (cpu_dai) {
1064 codec = cpu_dai->codec;
1065 if (codec && codec->probed &&
1066 codec->driver->remove_order == order)
1067 soc_remove_codec(codec);
1068 }
1069}
1070
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001071static void soc_remove_dai_links(struct snd_soc_card *card)
1072{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001073 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001074
Liam Girdwood0168bf02011-06-07 16:08:05 +01001075 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1076 order++) {
1077 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001078 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001079 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001080
1081 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1082 order++) {
1083 for (dai = 0; dai < card->num_rtd; dai++)
1084 soc_remove_link_components(card, dai, order);
1085 }
1086
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001087 card->num_rtd = 0;
1088}
1089
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001090static void soc_set_name_prefix(struct snd_soc_card *card,
1091 struct snd_soc_codec *codec)
1092{
1093 int i;
1094
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001095 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001096 return;
1097
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001098 for (i = 0; i < card->num_configs; i++) {
1099 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001100 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1101 codec->name_prefix = map->name_prefix;
1102 break;
1103 }
1104 }
1105}
1106
Jarkko Nikula589c3562010-12-06 16:27:07 +02001107static int soc_probe_codec(struct snd_soc_card *card,
1108 struct snd_soc_codec *codec)
1109{
1110 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001111 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001112 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001113
1114 codec->card = card;
1115 codec->dapm.card = card;
1116 soc_set_name_prefix(card, codec);
1117
Jarkko Nikula70d29332011-01-27 16:24:22 +02001118 if (!try_module_get(codec->dev->driver->owner))
1119 return -ENODEV;
1120
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001121 soc_init_codec_debugfs(codec);
1122
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001123 if (driver->dapm_widgets)
1124 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1125 driver->num_dapm_widgets);
1126
Mark Brown888df392012-02-16 19:37:51 -08001127 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001128 list_for_each_entry(dai, &codec->component.dai_list, list)
Mark Brown888df392012-02-16 19:37:51 -08001129 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
Mark Brown888df392012-02-16 19:37:51 -08001130
Mark Brown33c5f962011-08-22 18:40:30 +01001131 codec->dapm.idle_bias_off = driver->idle_bias_off;
1132
Xiubo Lia32c17b2014-03-11 12:43:22 +08001133 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1134 /* Set the default I/O up try regmap */
1135 ret = snd_soc_codec_set_cache_io(codec, NULL);
1136 if (ret < 0) {
1137 dev_err(codec->dev,
1138 "Failed to set cache I/O: %d\n", ret);
1139 goto err_probe;
1140 }
1141 }
Xiubo Life2265e2014-02-27 17:49:52 +08001142
Mark Brown89b95ac02011-03-07 16:38:44 +00001143 if (driver->probe) {
1144 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001145 if (ret < 0) {
1146 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001147 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001148 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001149 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001150 WARN(codec->dapm.idle_bias_off &&
1151 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001152 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1153 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001154 }
1155
Mark Brownb7af1da2011-04-07 19:18:44 +09001156 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001157 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001158 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001159 if (driver->dapm_routes)
1160 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1161 driver->num_dapm_routes);
1162
Jarkko Nikula589c3562010-12-06 16:27:07 +02001163 /* mark codec as probed and add to card codec list */
1164 codec->probed = 1;
1165 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001166 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001167
Jarkko Nikula70d29332011-01-27 16:24:22 +02001168 return 0;
1169
1170err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001171 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001172 module_put(codec->dev->driver->owner);
1173
Jarkko Nikula589c3562010-12-06 16:27:07 +02001174 return ret;
1175}
1176
Liam Girdwood956245e2011-07-01 16:54:08 +01001177static int soc_probe_platform(struct snd_soc_card *card,
1178 struct snd_soc_platform *platform)
1179{
1180 int ret = 0;
1181 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001182 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001183 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001184
1185 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001186 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001187
1188 if (!try_module_get(platform->dev->driver->owner))
1189 return -ENODEV;
1190
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001191 soc_init_platform_debugfs(platform);
1192
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001193 if (driver->dapm_widgets)
1194 snd_soc_dapm_new_controls(&platform->dapm,
1195 driver->dapm_widgets, driver->num_dapm_widgets);
1196
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001197 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001198 list_for_each_entry(component, &component_list, list) {
1199 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001200 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001201 list_for_each_entry(dai, &component->dai_list, list)
1202 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001203 }
1204
Stephen Warren3fec6b62012-04-05 12:28:01 -06001205 platform->dapm.idle_bias_off = 1;
1206
Liam Girdwood956245e2011-07-01 16:54:08 +01001207 if (driver->probe) {
1208 ret = driver->probe(platform);
1209 if (ret < 0) {
1210 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001211 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001212 goto err_probe;
1213 }
1214 }
1215
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001216 if (driver->controls)
1217 snd_soc_add_platform_controls(platform, driver->controls,
1218 driver->num_controls);
1219 if (driver->dapm_routes)
1220 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1221 driver->num_dapm_routes);
1222
Liam Girdwood956245e2011-07-01 16:54:08 +01001223 /* mark platform as probed and add to card platform list */
1224 platform->probed = 1;
1225 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001226 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001227
1228 return 0;
1229
1230err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001231 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001232 module_put(platform->dev->driver->owner);
1233
1234 return ret;
1235}
1236
Mark Brown36ae1a92012-01-06 17:12:45 -08001237static void rtd_release(struct device *dev)
1238{
1239 kfree(dev);
1240}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001241
Jarkko Nikula589c3562010-12-06 16:27:07 +02001242static int soc_post_component_init(struct snd_soc_card *card,
1243 struct snd_soc_codec *codec,
1244 int num, int dailess)
1245{
1246 struct snd_soc_dai_link *dai_link = NULL;
1247 struct snd_soc_aux_dev *aux_dev = NULL;
1248 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001249 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001250 int ret = 0;
1251
1252 if (!dailess) {
1253 dai_link = &card->dai_link[num];
1254 rtd = &card->rtd[num];
1255 name = dai_link->name;
1256 } else {
1257 aux_dev = &card->aux_dev[num];
1258 rtd = &card->rtd_aux[num];
1259 name = aux_dev->name;
1260 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001261 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001262
Jarkko Nikula589c3562010-12-06 16:27:07 +02001263 /* do machine specific initialization */
1264 if (!dailess && dai_link->init)
1265 ret = dai_link->init(rtd);
1266 else if (dailess && aux_dev->init)
1267 ret = aux_dev->init(&codec->dapm);
1268 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001269 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001270 return ret;
1271 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001272
Jarkko Nikula589c3562010-12-06 16:27:07 +02001273 /* register the rtd device */
1274 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001275
1276 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1277 if (!rtd->dev)
1278 return -ENOMEM;
1279 device_initialize(rtd->dev);
1280 rtd->dev->parent = card->dev;
1281 rtd->dev->release = rtd_release;
1282 rtd->dev->init_name = name;
1283 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001284 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001285 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1286 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1287 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1288 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001289 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001290 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001291 /* calling put_device() here to free the rtd->dev */
1292 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001293 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001294 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001295 return ret;
1296 }
1297 rtd->dev_registered = 1;
1298
1299 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001300 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001301 if (ret < 0)
1302 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001303 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001304
1305 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001306 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307 if (ret < 0)
1308 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001309 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001310
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001311#ifdef CONFIG_DEBUG_FS
1312 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001313 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001314 goto out;
1315
1316 ret = soc_dpcm_debugfs_add(rtd);
1317 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001318 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001319
1320out:
1321#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 return 0;
1323}
1324
Stephen Warren62ae68f2012-06-08 12:34:23 -06001325static int soc_probe_link_components(struct snd_soc_card *card, int num,
1326 int order)
1327{
1328 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1329 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1330 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1331 struct snd_soc_platform *platform = rtd->platform;
1332 int ret;
1333
1334 /* probe the CPU-side component, if it is a CODEC */
1335 if (cpu_dai->codec &&
1336 !cpu_dai->codec->probed &&
1337 cpu_dai->codec->driver->probe_order == order) {
1338 ret = soc_probe_codec(card, cpu_dai->codec);
1339 if (ret < 0)
1340 return ret;
1341 }
1342
1343 /* probe the CODEC-side component */
1344 if (!codec_dai->codec->probed &&
1345 codec_dai->codec->driver->probe_order == order) {
1346 ret = soc_probe_codec(card, codec_dai->codec);
1347 if (ret < 0)
1348 return ret;
1349 }
1350
1351 /* probe the platform */
1352 if (!platform->probed &&
1353 platform->driver->probe_order == order) {
1354 ret = soc_probe_platform(card, platform);
1355 if (ret < 0)
1356 return ret;
1357 }
1358
1359 return 0;
1360}
1361
1362static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001363{
1364 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1365 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1366 struct snd_soc_codec *codec = rtd->codec;
1367 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001368 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1369 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1370 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001371 int ret;
1372
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001373 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001374 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001375
1376 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378 codec_dai->card = card;
1379 cpu_dai->card = card;
1380
1381 /* set default power off timeout */
1382 rtd->pmdown_time = pmdown_time;
1383
1384 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001385 if (!cpu_dai->probed &&
1386 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001387 if (!cpu_dai->codec) {
1388 cpu_dai->dapm.card = card;
1389 if (!try_module_get(cpu_dai->dev->driver->owner))
1390 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001391
Stephen Warren18d75642012-06-08 12:34:21 -06001392 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001393 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001394
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001395 if (cpu_dai->driver->probe) {
1396 ret = cpu_dai->driver->probe(cpu_dai);
1397 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001398 dev_err(cpu_dai->dev,
1399 "ASoC: failed to probe CPU DAI %s: %d\n",
1400 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001401 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001402 return ret;
1403 }
1404 }
1405 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001406 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001407 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1408 }
1409
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001410 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001411 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001412 if (codec_dai->driver->probe) {
1413 ret = codec_dai->driver->probe(codec_dai);
1414 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001415 dev_err(codec_dai->dev,
1416 "ASoC: failed to probe CODEC DAI %s: %d\n",
1417 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001418 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001419 }
1420 }
1421
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001422 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001423 codec_dai->probed = 1;
1424 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001425 }
1426
Liam Girdwood0168bf02011-06-07 16:08:05 +01001427 /* complete DAI probe during last probe */
1428 if (order != SND_SOC_COMP_ORDER_LAST)
1429 return 0;
1430
Jarkko Nikula589c3562010-12-06 16:27:07 +02001431 ret = soc_post_component_init(card, codec, num, 0);
1432 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001434
Mark Brown36ae1a92012-01-06 17:12:45 -08001435 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001436 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001437 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1438 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001439
Namarta Kohli1245b702012-08-16 17:10:41 +05301440 if (cpu_dai->driver->compress_dai) {
1441 /*create compress_device"*/
1442 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001443 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001444 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301445 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001446 return ret;
1447 }
1448 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301449
1450 if (!dai_link->params) {
1451 /* create the pcm */
1452 ret = soc_new_pcm(rtd, num);
1453 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001454 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301455 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001456 return ret;
1457 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301458 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001459 INIT_DELAYED_WORK(&rtd->delayed_work,
1460 codec2codec_close_delayed_work);
1461
Namarta Kohli1245b702012-08-16 17:10:41 +05301462 /* link the DAI widgets */
1463 play_w = codec_dai->playback_widget;
1464 capture_w = cpu_dai->capture_widget;
1465 if (play_w && capture_w) {
1466 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001467 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301468 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001469 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301470 play_w->name, capture_w->name, ret);
1471 return ret;
1472 }
1473 }
1474
1475 play_w = cpu_dai->playback_widget;
1476 capture_w = codec_dai->capture_widget;
1477 if (play_w && capture_w) {
1478 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1479 capture_w, play_w);
1480 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001481 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301482 play_w->name, capture_w->name, ret);
1483 return ret;
1484 }
Mark Brownc74184e2012-04-04 22:12:09 +01001485 }
1486 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001487 }
1488
1489 /* add platform data for AC97 devices */
1490 if (rtd->codec_dai->driver->ac97_control)
1491 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1492
1493 return 0;
1494}
1495
1496#ifdef CONFIG_SND_SOC_AC97_BUS
1497static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1498{
1499 int ret;
1500
1501 /* Only instantiate AC97 if not already done by the adaptor
1502 * for the generic AC97 subsystem.
1503 */
1504 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001505 /*
1506 * It is possible that the AC97 device is already registered to
1507 * the device subsystem. This happens when the device is created
1508 * via snd_ac97_mixer(). Currently only SoC codec that does so
1509 * is the generic AC97 glue but others migh emerge.
1510 *
1511 * In those cases we don't try to register the device again.
1512 */
1513 if (!rtd->codec->ac97_created)
1514 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515
1516 ret = soc_ac97_dev_register(rtd->codec);
1517 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001518 dev_err(rtd->codec->dev,
1519 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001520 return ret;
1521 }
1522
1523 rtd->codec->ac97_registered = 1;
1524 }
1525 return 0;
1526}
1527
1528static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1529{
1530 if (codec->ac97_registered) {
1531 soc_ac97_dev_unregister(codec);
1532 codec->ac97_registered = 0;
1533 }
1534}
1535#endif
1536
Mark Brownb19e6e72012-03-14 21:18:39 +00001537static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1538{
1539 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1540 struct snd_soc_codec *codec;
1541
1542 /* find CODEC from registered CODECs*/
1543 list_for_each_entry(codec, &codec_list, list) {
1544 if (!strcmp(codec->name, aux_dev->codec_name))
1545 return 0;
1546 }
1547
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001548 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001549
Mark Brownb19e6e72012-03-14 21:18:39 +00001550 return -EPROBE_DEFER;
1551}
1552
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001553static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1554{
1555 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001556 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001557 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001558
1559 /* find CODEC from registered CODECs*/
1560 list_for_each_entry(codec, &codec_list, list) {
1561 if (!strcmp(codec->name, aux_dev->codec_name)) {
1562 if (codec->probed) {
1563 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001564 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001565 ret = -EBUSY;
1566 goto out;
1567 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001568 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001569 }
1570 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001571 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001572 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001573 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001574
Jarkko Nikula676ad982010-12-03 09:18:22 +02001575found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001576 ret = soc_probe_codec(card, codec);
1577 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001578 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001579
Jarkko Nikula589c3562010-12-06 16:27:07 +02001580 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001581
1582out:
1583 return ret;
1584}
1585
1586static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1587{
1588 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1589 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001590
1591 /* unregister the rtd device */
1592 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001593 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001594 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001595 rtd->dev_registered = 0;
1596 }
1597
Jarkko Nikula589c3562010-12-06 16:27:07 +02001598 if (codec && codec->probed)
1599 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001600}
1601
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001602static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001603{
1604 int ret;
1605
1606 if (codec->cache_init)
1607 return 0;
1608
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001609 ret = snd_soc_cache_init(codec);
1610 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001611 dev_err(codec->dev,
1612 "ASoC: Failed to set cache compression type: %d\n",
1613 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001614 return ret;
1615 }
1616 codec->cache_init = 1;
1617 return 0;
1618}
1619
Mark Brownb19e6e72012-03-14 21:18:39 +00001620static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001621{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001622 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001623 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001624 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001625
Liam Girdwood01b9d992012-03-07 10:38:25 +00001626 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627
Mark Brownb19e6e72012-03-14 21:18:39 +00001628 /* bind DAIs */
1629 for (i = 0; i < card->num_links; i++) {
1630 ret = soc_bind_dai_link(card, i);
1631 if (ret != 0)
1632 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001633 }
1634
Mark Brownb19e6e72012-03-14 21:18:39 +00001635 /* check aux_devs too */
1636 for (i = 0; i < card->num_aux_devs; i++) {
1637 ret = soc_check_aux_dev(card, i);
1638 if (ret != 0)
1639 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640 }
1641
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001642 /* initialize the register cache for each available codec */
1643 list_for_each_entry(codec, &codec_list, list) {
1644 if (codec->cache_init)
1645 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001646 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001647 if (ret < 0)
1648 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001649 }
1650
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001651 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001652 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001653 card->owner, 0, &card->snd_card);
1654 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001655 dev_err(card->dev,
1656 "ASoC: can't create sound card for card %s: %d\n",
1657 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001658 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001659 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001660
Mark Browne37a4972011-03-02 18:21:57 +00001661 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1662 card->dapm.dev = card->dev;
1663 card->dapm.card = card;
1664 list_add(&card->dapm.list, &card->dapm_list);
1665
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001666#ifdef CONFIG_DEBUG_FS
1667 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1668#endif
1669
Mark Brown88ee1c62011-02-02 10:43:26 +00001670#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001671 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001672 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001673#endif
Andy Green6ed25972008-06-13 16:24:05 +01001674
Mark Brown9a841eb2011-04-12 17:51:37 -07001675 if (card->dapm_widgets)
1676 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1677 card->num_dapm_widgets);
1678
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001679 /* initialise the sound card only once */
1680 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001681 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001682 if (ret < 0)
1683 goto card_probe_error;
1684 }
1685
Stephen Warren62ae68f2012-06-08 12:34:23 -06001686 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001687 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1688 order++) {
1689 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001690 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001691 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001692 dev_err(card->dev,
1693 "ASoC: failed to instantiate card %d\n",
1694 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001695 goto probe_dai_err;
1696 }
1697 }
1698 }
1699
1700 /* probe all DAI links on this card */
1701 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1702 order++) {
1703 for (i = 0; i < card->num_links; i++) {
1704 ret = soc_probe_link_dais(card, i, order);
1705 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001706 dev_err(card->dev,
1707 "ASoC: failed to instantiate card %d\n",
1708 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001709 goto probe_dai_err;
1710 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001711 }
1712 }
1713
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001714 for (i = 0; i < card->num_aux_devs; i++) {
1715 ret = soc_probe_aux_dev(card, i);
1716 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001717 dev_err(card->dev,
1718 "ASoC: failed to add auxiliary devices %d\n",
1719 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001720 goto probe_aux_dev_err;
1721 }
1722 }
1723
Mark Brown888df392012-02-16 19:37:51 -08001724 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001725 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001726
Mark Brownb7af1da2011-04-07 19:18:44 +09001727 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001728 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001729
Mark Brownb8ad29d2011-03-02 18:35:51 +00001730 if (card->dapm_routes)
1731 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1732 card->num_dapm_routes);
1733
Mark Brown75d9ac42011-09-27 16:41:01 +01001734 for (i = 0; i < card->num_links; i++) {
1735 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001736 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001737
Mark Brownf04209a2012-04-09 16:29:21 +01001738 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001739 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001740 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001741 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001742 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001743 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001744 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001745 }
1746
1747 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001748 if (dai_fmt &&
1749 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001750 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1751 dai_fmt);
1752 if (ret != 0 && ret != -ENOTSUPP)
1753 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001754 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001755 ret);
1756 } else if (dai_fmt) {
1757 /* Flip the polarity for the "CPU" end */
1758 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1759 switch (dai_link->dai_fmt &
1760 SND_SOC_DAIFMT_MASTER_MASK) {
1761 case SND_SOC_DAIFMT_CBM_CFM:
1762 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1763 break;
1764 case SND_SOC_DAIFMT_CBM_CFS:
1765 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1766 break;
1767 case SND_SOC_DAIFMT_CBS_CFM:
1768 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1769 break;
1770 case SND_SOC_DAIFMT_CBS_CFS:
1771 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1772 break;
1773 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001774
1775 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001776 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001777 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001778 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001779 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001780 ret);
1781 }
1782 }
1783
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001784 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001785 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001786 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1787 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001788 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1789 "%s", card->driver_name ? card->driver_name : card->name);
1790 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1791 switch (card->snd_card->driver[i]) {
1792 case '_':
1793 case '-':
1794 case '\0':
1795 break;
1796 default:
1797 if (!isalnum(card->snd_card->driver[i]))
1798 card->snd_card->driver[i] = '_';
1799 break;
1800 }
1801 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001802
Mark Brown28e9ad92011-03-02 18:36:34 +00001803 if (card->late_probe) {
1804 ret = card->late_probe(card);
1805 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001806 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001807 card->name, ret);
1808 goto probe_aux_dev_err;
1809 }
1810 }
1811
Stephen Warren16332812011-11-23 12:42:04 -07001812 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001813 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001814 snd_soc_dapm_auto_nc_codec_pins(codec);
1815
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001816 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001817
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001818 ret = snd_card_register(card->snd_card);
1819 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001820 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1821 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001822 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001823 }
1824
1825#ifdef CONFIG_SND_SOC_AC97_BUS
1826 /* register any AC97 codecs */
1827 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001828 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1829 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001830 dev_err(card->dev,
1831 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001832 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001833 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001834 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001835 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001836 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001837#endif
1838
Mark Brown435c5e22008-12-04 15:32:53 +00001839 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001840 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001841 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001842
1843 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001844
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001845probe_aux_dev_err:
1846 for (i = 0; i < card->num_aux_devs; i++)
1847 soc_remove_aux_dev(card, i);
1848
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001849probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001850 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001851
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001852card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001853 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001854 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001855
1856 snd_card_free(card->snd_card);
1857
Mark Brownb19e6e72012-03-14 21:18:39 +00001858base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001859 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001860
Mark Brownb19e6e72012-03-14 21:18:39 +00001861 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001862}
1863
1864/* probes a new socdev */
1865static int soc_probe(struct platform_device *pdev)
1866{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001867 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001868
Vinod Koul70a7ca32011-01-14 19:22:48 +05301869 /*
1870 * no card, so machine driver should be registering card
1871 * we should not be here in that case so ret error
1872 */
1873 if (!card)
1874 return -EINVAL;
1875
Mark Brownfe4085e2012-03-02 13:07:41 +00001876 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001877 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001878 card->name);
1879
Mark Brown435c5e22008-12-04 15:32:53 +00001880 /* Bodge while we unpick instantiation */
1881 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001882
Mark Brown28d528c2012-08-09 18:45:23 +01001883 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001884}
1885
Vinod Koulb0e26482011-01-13 22:48:02 +05301886static int soc_cleanup_card_resources(struct snd_soc_card *card)
1887{
Vinod Koulb0e26482011-01-13 22:48:02 +05301888 int i;
1889
1890 /* make sure any delayed work runs */
1891 for (i = 0; i < card->num_rtd; i++) {
1892 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001893 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301894 }
1895
1896 /* remove auxiliary devices */
1897 for (i = 0; i < card->num_aux_devs; i++)
1898 soc_remove_aux_dev(card, i);
1899
1900 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001901 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301902
1903 soc_cleanup_card_debugfs(card);
1904
1905 /* remove the card */
1906 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001907 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301908
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001909 snd_soc_dapm_free(&card->dapm);
1910
Vinod Koulb0e26482011-01-13 22:48:02 +05301911 snd_card_free(card->snd_card);
1912 return 0;
1913
1914}
1915
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001916/* removes a socdev */
1917static int soc_remove(struct platform_device *pdev)
1918{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001919 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001920
Mark Brownc5af3a22008-11-28 13:29:45 +00001921 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001922 return 0;
1923}
1924
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001925int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001926{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001927 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001928 int i;
Mark Brown51737472009-06-22 13:16:51 +01001929
1930 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001931 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001932
1933 /* Flush out pmdown_time work - we actually do want to run it
1934 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001935 for (i = 0; i < card->num_rtd; i++) {
1936 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001937 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001938 }
Mark Brown51737472009-06-22 13:16:51 +01001939
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001940 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001941
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001942 /* deactivate pins to sleep state */
1943 for (i = 0; i < card->num_rtd; i++) {
1944 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1945 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1946 pinctrl_pm_select_sleep_state(codec_dai->dev);
1947 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1948 }
1949
Mark Brown416356f2009-06-30 19:05:15 +01001950 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001951}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001952EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001953
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001954const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301955 .suspend = snd_soc_suspend,
1956 .resume = snd_soc_resume,
1957 .freeze = snd_soc_suspend,
1958 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001959 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301960 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001961};
Stephen Warrendeb26072011-04-05 19:35:30 -06001962EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001963
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964/* ASoC platform driver */
1965static struct platform_driver soc_driver = {
1966 .driver = {
1967 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001968 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001969 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001970 },
1971 .probe = soc_probe,
1972 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001973};
1974
Mark Brown096e49d2009-07-05 15:12:22 +01001975/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001976 * snd_soc_new_ac97_codec - initailise AC97 device
1977 * @codec: audio codec
1978 * @ops: AC97 bus operations
1979 * @num: AC97 codec number
1980 *
1981 * Initialises AC97 codec resources for use by ad-hoc devices only.
1982 */
1983int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1984 struct snd_ac97_bus_ops *ops, int num)
1985{
1986 mutex_lock(&codec->mutex);
1987
1988 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1989 if (codec->ac97 == NULL) {
1990 mutex_unlock(&codec->mutex);
1991 return -ENOMEM;
1992 }
1993
1994 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1995 if (codec->ac97->bus == NULL) {
1996 kfree(codec->ac97);
1997 codec->ac97 = NULL;
1998 mutex_unlock(&codec->mutex);
1999 return -ENOMEM;
2000 }
2001
2002 codec->ac97->bus->ops = ops;
2003 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002004
2005 /*
2006 * Mark the AC97 device to be created by us. This way we ensure that the
2007 * device will be registered with the device subsystem later on.
2008 */
2009 codec->ac97_created = 1;
2010
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002011 mutex_unlock(&codec->mutex);
2012 return 0;
2013}
2014EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2015
Markus Pargmann741a5092013-08-19 17:05:55 +02002016static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2017
2018static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2019{
2020 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2021
2022 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2023
2024 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2025
2026 udelay(10);
2027
2028 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2029
2030 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2031 msleep(2);
2032}
2033
2034static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2035{
2036 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2037
2038 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2039
2040 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2041 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2042 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2043
2044 udelay(10);
2045
2046 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2047
2048 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2049 msleep(2);
2050}
2051
2052static int snd_soc_ac97_parse_pinctl(struct device *dev,
2053 struct snd_ac97_reset_cfg *cfg)
2054{
2055 struct pinctrl *p;
2056 struct pinctrl_state *state;
2057 int gpio;
2058 int ret;
2059
2060 p = devm_pinctrl_get(dev);
2061 if (IS_ERR(p)) {
2062 dev_err(dev, "Failed to get pinctrl\n");
2063 return PTR_RET(p);
2064 }
2065 cfg->pctl = p;
2066
2067 state = pinctrl_lookup_state(p, "ac97-reset");
2068 if (IS_ERR(state)) {
2069 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2070 return PTR_RET(state);
2071 }
2072 cfg->pstate_reset = state;
2073
2074 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2075 if (IS_ERR(state)) {
2076 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2077 return PTR_RET(state);
2078 }
2079 cfg->pstate_warm_reset = state;
2080
2081 state = pinctrl_lookup_state(p, "ac97-running");
2082 if (IS_ERR(state)) {
2083 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2084 return PTR_RET(state);
2085 }
2086 cfg->pstate_run = state;
2087
2088 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2089 if (gpio < 0) {
2090 dev_err(dev, "Can't find ac97-sync gpio\n");
2091 return gpio;
2092 }
2093 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2094 if (ret) {
2095 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2096 return ret;
2097 }
2098 cfg->gpio_sync = gpio;
2099
2100 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2101 if (gpio < 0) {
2102 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2103 return gpio;
2104 }
2105 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2106 if (ret) {
2107 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2108 return ret;
2109 }
2110 cfg->gpio_sdata = gpio;
2111
2112 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2113 if (gpio < 0) {
2114 dev_err(dev, "Can't find ac97-reset gpio\n");
2115 return gpio;
2116 }
2117 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2118 if (ret) {
2119 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2120 return ret;
2121 }
2122 cfg->gpio_reset = gpio;
2123
2124 return 0;
2125}
2126
Mark Brownb047e1c2013-06-26 12:45:59 +01002127struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002128EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002129
2130int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2131{
2132 if (ops == soc_ac97_ops)
2133 return 0;
2134
2135 if (soc_ac97_ops && ops)
2136 return -EBUSY;
2137
2138 soc_ac97_ops = ops;
2139
2140 return 0;
2141}
2142EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2143
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002144/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002145 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2146 *
2147 * This function sets the reset and warm_reset properties of ops and parses
2148 * the device node of pdev to get pinctrl states and gpio numbers to use.
2149 */
2150int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2151 struct platform_device *pdev)
2152{
2153 struct device *dev = &pdev->dev;
2154 struct snd_ac97_reset_cfg cfg;
2155 int ret;
2156
2157 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2158 if (ret)
2159 return ret;
2160
2161 ret = snd_soc_set_ac97_ops(ops);
2162 if (ret)
2163 return ret;
2164
2165 ops->warm_reset = snd_soc_ac97_warm_reset;
2166 ops->reset = snd_soc_ac97_reset;
2167
2168 snd_ac97_rst_cfg = cfg;
2169 return 0;
2170}
2171EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2172
2173/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002174 * snd_soc_free_ac97_codec - free AC97 codec device
2175 * @codec: audio codec
2176 *
2177 * Frees AC97 codec device resources.
2178 */
2179void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2180{
2181 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002182#ifdef CONFIG_SND_SOC_AC97_BUS
2183 soc_unregister_ac97_dai_link(codec);
2184#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002185 kfree(codec->ac97->bus);
2186 kfree(codec->ac97);
2187 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002188 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 mutex_unlock(&codec->mutex);
2190}
2191EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2192
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002193/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002194 * snd_soc_cnew - create new control
2195 * @_template: control template
2196 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002197 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002198 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 *
2200 * Create a new mixer control from a template control.
2201 *
2202 * Returns 0 for success, else error.
2203 */
2204struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002205 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002206 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002207{
2208 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002209 struct snd_kcontrol *kcontrol;
2210 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002211
2212 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002213 template.index = 0;
2214
Mark Brownefb7ac32011-03-08 17:23:24 +00002215 if (!long_name)
2216 long_name = template.name;
2217
2218 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002219 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002220 if (!name)
2221 return NULL;
2222
Mark Brownefb7ac32011-03-08 17:23:24 +00002223 template.name = name;
2224 } else {
2225 template.name = long_name;
2226 }
2227
2228 kcontrol = snd_ctl_new1(&template, data);
2229
2230 kfree(name);
2231
2232 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002233}
2234EXPORT_SYMBOL_GPL(snd_soc_cnew);
2235
Liam Girdwood022658b2012-02-03 17:43:09 +00002236static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2237 const struct snd_kcontrol_new *controls, int num_controls,
2238 const char *prefix, void *data)
2239{
2240 int err, i;
2241
2242 for (i = 0; i < num_controls; i++) {
2243 const struct snd_kcontrol_new *control = &controls[i];
2244 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2245 control->name, prefix));
2246 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002247 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2248 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002249 return err;
2250 }
2251 }
2252
2253 return 0;
2254}
2255
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002256struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2257 const char *name)
2258{
2259 struct snd_card *card = soc_card->snd_card;
2260 struct snd_kcontrol *kctl;
2261
2262 if (unlikely(!name))
2263 return NULL;
2264
2265 list_for_each_entry(kctl, &card->controls, list)
2266 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2267 return kctl;
2268 return NULL;
2269}
2270EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2271
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002272/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002273 * snd_soc_add_codec_controls - add an array of controls to a codec.
2274 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002275 * duplicating this code.
2276 *
2277 * @codec: codec to add controls to
2278 * @controls: array of controls to add
2279 * @num_controls: number of elements in the array
2280 *
2281 * Return 0 for success, else error.
2282 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002283int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002284 const struct snd_kcontrol_new *controls, int num_controls)
2285{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002286 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002287
Liam Girdwood022658b2012-02-03 17:43:09 +00002288 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2289 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002290}
Liam Girdwood022658b2012-02-03 17:43:09 +00002291EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002292
2293/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002294 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002295 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002296 *
2297 * @platform: platform to add controls to
2298 * @controls: array of controls to add
2299 * @num_controls: number of elements in the array
2300 *
2301 * Return 0 for success, else error.
2302 */
2303int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2304 const struct snd_kcontrol_new *controls, int num_controls)
2305{
2306 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002307
Liam Girdwood022658b2012-02-03 17:43:09 +00002308 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2309 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002310}
2311EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2312
2313/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002314 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2315 * Convenience function to add a list of controls.
2316 *
2317 * @soc_card: SoC card to add controls to
2318 * @controls: array of controls to add
2319 * @num_controls: number of elements in the array
2320 *
2321 * Return 0 for success, else error.
2322 */
2323int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2324 const struct snd_kcontrol_new *controls, int num_controls)
2325{
2326 struct snd_card *card = soc_card->snd_card;
2327
2328 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2329 NULL, soc_card);
2330}
2331EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2332
2333/**
2334 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2335 * Convienience function to add a list of controls.
2336 *
2337 * @dai: DAI to add controls to
2338 * @controls: array of controls to add
2339 * @num_controls: number of elements in the array
2340 *
2341 * Return 0 for success, else error.
2342 */
2343int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2344 const struct snd_kcontrol_new *controls, int num_controls)
2345{
2346 struct snd_card *card = dai->card->snd_card;
2347
2348 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2349 NULL, dai);
2350}
2351EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2352
2353/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002354 * snd_soc_info_enum_double - enumerated double mixer info callback
2355 * @kcontrol: mixer control
2356 * @uinfo: control element information
2357 *
2358 * Callback to provide information about a double enumerated
2359 * mixer control.
2360 *
2361 * Returns 0 for success.
2362 */
2363int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2364 struct snd_ctl_elem_info *uinfo)
2365{
2366 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2367
2368 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2369 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002370 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002371
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002372 if (uinfo->value.enumerated.item >= e->items)
2373 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002374 strlcpy(uinfo->value.enumerated.name,
2375 e->texts[uinfo->value.enumerated.item],
2376 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002377 return 0;
2378}
2379EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2380
2381/**
2382 * snd_soc_get_enum_double - enumerated double mixer get callback
2383 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002384 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002385 *
2386 * Callback to get the value of a double enumerated mixer.
2387 *
2388 * Returns 0 for success.
2389 */
2390int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_value *ucontrol)
2392{
2393 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2394 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002395 unsigned int val, item;
2396 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002398 reg_val = snd_soc_read(codec, e->reg);
2399 val = (reg_val >> e->shift_l) & e->mask;
2400 item = snd_soc_enum_val_to_item(e, val);
2401 ucontrol->value.enumerated.item[0] = item;
2402 if (e->shift_l != e->shift_r) {
2403 val = (reg_val >> e->shift_l) & e->mask;
2404 item = snd_soc_enum_val_to_item(e, val);
2405 ucontrol->value.enumerated.item[1] = item;
2406 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002407
2408 return 0;
2409}
2410EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2411
2412/**
2413 * snd_soc_put_enum_double - enumerated double mixer put callback
2414 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002415 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002416 *
2417 * Callback to set the value of a double enumerated mixer.
2418 *
2419 * Returns 0 for success.
2420 */
2421int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2425 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002426 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002427 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002428 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002429
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002430 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002432 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002433 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002434 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002435 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002436 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002437 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002438 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002439 }
2440
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002441 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002442}
2443EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2444
2445/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002446 * snd_soc_read_signed - Read a codec register and interprete as signed value
2447 * @codec: codec
2448 * @reg: Register to read
2449 * @mask: Mask to use after shifting the register value
2450 * @shift: Right shift of register value
2451 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002452 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002453 * This functions reads a codec register. The register value is shifted right
2454 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2455 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002456 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002457 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002458 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002459static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2460 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002461{
Markus Pargmannf227b882014-01-16 16:02:10 +01002462 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002463 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002464
Markus Pargmannf227b882014-01-16 16:02:10 +01002465 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002466
Markus Pargmannf227b882014-01-16 16:02:10 +01002467 if (!sign_bit)
2468 return val;
2469
2470 /* non-negative number */
2471 if (!(val & BIT(sign_bit)))
2472 return val;
2473
2474 ret = val;
2475
2476 /*
2477 * The register most probably does not contain a full-sized int.
2478 * Instead we have an arbitrary number of bits in a signed
2479 * representation which has to be translated into a full-sized int.
2480 * This is done by filling up all bits above the sign-bit.
2481 */
2482 ret |= ~((int)(BIT(sign_bit) - 1));
2483
2484 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002485}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002486
2487/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002488 * snd_soc_info_volsw - single mixer info callback
2489 * @kcontrol: mixer control
2490 * @uinfo: control element information
2491 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002492 * Callback to provide information about a single mixer control, or a double
2493 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002494 *
2495 * Returns 0 for success.
2496 */
2497int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_info *uinfo)
2499{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002500 struct soc_mixer_control *mc =
2501 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002502 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002504 if (!mc->platform_max)
2505 mc->platform_max = mc->max;
2506 platform_max = mc->platform_max;
2507
2508 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002509 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2510 else
2511 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2512
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002513 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002514 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002515 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002516 return 0;
2517}
2518EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2519
2520/**
2521 * snd_soc_get_volsw - single mixer get callback
2522 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002523 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002524 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002525 * Callback to get the value of a single mixer control, or a double mixer
2526 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002527 *
2528 * Returns 0 for success.
2529 */
2530int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2531 struct snd_ctl_elem_value *ucontrol)
2532{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002533 struct soc_mixer_control *mc =
2534 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002535 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002536 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002537 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002538 unsigned int shift = mc->shift;
2539 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002540 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002541 int min = mc->min;
2542 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002543 unsigned int mask = (1 << fls(max)) - 1;
2544 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002545
Markus Pargmannf227b882014-01-16 16:02:10 +01002546 if (sign_bit)
2547 mask = BIT(sign_bit + 1) - 1;
2548
2549 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2550 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002551 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002552 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002553 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002554
2555 if (snd_soc_volsw_is_stereo(mc)) {
2556 if (reg == reg2)
2557 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002558 snd_soc_read_signed(codec, reg, mask, rshift,
2559 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002560 else
2561 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002562 snd_soc_read_signed(codec, reg2, mask, shift,
2563 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002564 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002565 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002566 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 }
2568
2569 return 0;
2570}
2571EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2572
2573/**
2574 * snd_soc_put_volsw - single mixer put callback
2575 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002576 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002577 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002578 * Callback to set the value of a single mixer control, or a double mixer
2579 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 *
2581 * Returns 0 for success.
2582 */
2583int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2584 struct snd_ctl_elem_value *ucontrol)
2585{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002586 struct soc_mixer_control *mc =
2587 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002588 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002589 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002590 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002591 unsigned int shift = mc->shift;
2592 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002593 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002594 int min = mc->min;
2595 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002596 unsigned int mask = (1 << fls(max)) - 1;
2597 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002598 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002599 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002600 unsigned int val2 = 0;
2601 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002602
Markus Pargmannf227b882014-01-16 16:02:10 +01002603 if (sign_bit)
2604 mask = BIT(sign_bit + 1) - 1;
2605
2606 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002607 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002608 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609 val_mask = mask << shift;
2610 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002611 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002612 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002613 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002614 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002615 if (reg == reg2) {
2616 val_mask |= mask << rshift;
2617 val |= val2 << rshift;
2618 } else {
2619 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002620 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002621 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002623 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002624 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002625 return err;
2626
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002627 if (type_2r)
2628 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2629
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002630 return err;
2631}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002632EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002633
Mark Browne13ac2e2008-05-28 17:58:05 +01002634/**
Brian Austin1d99f242012-03-30 10:43:55 -05002635 * snd_soc_get_volsw_sx - single mixer get callback
2636 * @kcontrol: mixer control
2637 * @ucontrol: control element information
2638 *
2639 * Callback to get the value of a single mixer control, or a double mixer
2640 * control that spans 2 registers.
2641 *
2642 * Returns 0 for success.
2643 */
2644int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2645 struct snd_ctl_elem_value *ucontrol)
2646{
2647 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2648 struct soc_mixer_control *mc =
2649 (struct soc_mixer_control *)kcontrol->private_value;
2650
2651 unsigned int reg = mc->reg;
2652 unsigned int reg2 = mc->rreg;
2653 unsigned int shift = mc->shift;
2654 unsigned int rshift = mc->rshift;
2655 int max = mc->max;
2656 int min = mc->min;
2657 int mask = (1 << (fls(min + max) - 1)) - 1;
2658
2659 ucontrol->value.integer.value[0] =
2660 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2661
2662 if (snd_soc_volsw_is_stereo(mc))
2663 ucontrol->value.integer.value[1] =
2664 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2665
2666 return 0;
2667}
2668EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2669
2670/**
2671 * snd_soc_put_volsw_sx - double mixer set callback
2672 * @kcontrol: mixer control
2673 * @uinfo: control element information
2674 *
2675 * Callback to set the value of a double mixer control that spans 2 registers.
2676 *
2677 * Returns 0 for success.
2678 */
2679int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2680 struct snd_ctl_elem_value *ucontrol)
2681{
2682 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2683 struct soc_mixer_control *mc =
2684 (struct soc_mixer_control *)kcontrol->private_value;
2685
2686 unsigned int reg = mc->reg;
2687 unsigned int reg2 = mc->rreg;
2688 unsigned int shift = mc->shift;
2689 unsigned int rshift = mc->rshift;
2690 int max = mc->max;
2691 int min = mc->min;
2692 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002693 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002694 unsigned short val, val_mask, val2 = 0;
2695
2696 val_mask = mask << shift;
2697 val = (ucontrol->value.integer.value[0] + min) & mask;
2698 val = val << shift;
2699
Mukund Navadad0558522012-11-09 11:53:40 +05302700 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2701 if (err < 0)
2702 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002703
2704 if (snd_soc_volsw_is_stereo(mc)) {
2705 val_mask = mask << rshift;
2706 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2707 val2 = val2 << rshift;
2708
2709 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2710 return err;
2711 }
2712 return 0;
2713}
2714EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2715
2716/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002717 * snd_soc_info_volsw_s8 - signed mixer info callback
2718 * @kcontrol: mixer control
2719 * @uinfo: control element information
2720 *
2721 * Callback to provide information about a signed mixer control.
2722 *
2723 * Returns 0 for success.
2724 */
2725int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2726 struct snd_ctl_elem_info *uinfo)
2727{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002728 struct soc_mixer_control *mc =
2729 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002730 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002731 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002732
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002733 if (!mc->platform_max)
2734 mc->platform_max = mc->max;
2735 platform_max = mc->platform_max;
2736
Mark Browne13ac2e2008-05-28 17:58:05 +01002737 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2738 uinfo->count = 2;
2739 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002740 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002741 return 0;
2742}
2743EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2744
2745/**
2746 * snd_soc_get_volsw_s8 - signed mixer get callback
2747 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002748 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002749 *
2750 * Callback to get the value of a signed mixer control.
2751 *
2752 * Returns 0 for success.
2753 */
2754int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2755 struct snd_ctl_elem_value *ucontrol)
2756{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002757 struct soc_mixer_control *mc =
2758 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002759 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002760 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002761 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002762 int val = snd_soc_read(codec, reg);
2763
2764 ucontrol->value.integer.value[0] =
2765 ((signed char)(val & 0xff))-min;
2766 ucontrol->value.integer.value[1] =
2767 ((signed char)((val >> 8) & 0xff))-min;
2768 return 0;
2769}
2770EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2771
2772/**
2773 * snd_soc_put_volsw_sgn - signed mixer put callback
2774 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002775 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002776 *
2777 * Callback to set the value of a signed mixer control.
2778 *
2779 * Returns 0 for success.
2780 */
2781int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2782 struct snd_ctl_elem_value *ucontrol)
2783{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002784 struct soc_mixer_control *mc =
2785 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002786 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002787 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002788 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002789 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002790
2791 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2792 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2793
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002794 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002795}
2796EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2797
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002798/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002799 * snd_soc_info_volsw_range - single mixer info callback with range.
2800 * @kcontrol: mixer control
2801 * @uinfo: control element information
2802 *
2803 * Callback to provide information, within a range, about a single
2804 * mixer control.
2805 *
2806 * returns 0 for success.
2807 */
2808int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2809 struct snd_ctl_elem_info *uinfo)
2810{
2811 struct soc_mixer_control *mc =
2812 (struct soc_mixer_control *)kcontrol->private_value;
2813 int platform_max;
2814 int min = mc->min;
2815
2816 if (!mc->platform_max)
2817 mc->platform_max = mc->max;
2818 platform_max = mc->platform_max;
2819
2820 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002821 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002822 uinfo->value.integer.min = 0;
2823 uinfo->value.integer.max = platform_max - min;
2824
2825 return 0;
2826}
2827EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2828
2829/**
2830 * snd_soc_put_volsw_range - single mixer put value callback with range.
2831 * @kcontrol: mixer control
2832 * @ucontrol: control element information
2833 *
2834 * Callback to set the value, within a range, for a single mixer control.
2835 *
2836 * Returns 0 for success.
2837 */
2838int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2839 struct snd_ctl_elem_value *ucontrol)
2840{
2841 struct soc_mixer_control *mc =
2842 (struct soc_mixer_control *)kcontrol->private_value;
2843 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2844 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002845 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002846 unsigned int shift = mc->shift;
2847 int min = mc->min;
2848 int max = mc->max;
2849 unsigned int mask = (1 << fls(max)) - 1;
2850 unsigned int invert = mc->invert;
2851 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002852 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002853
2854 val = ((ucontrol->value.integer.value[0] + min) & mask);
2855 if (invert)
2856 val = max - val;
2857 val_mask = mask << shift;
2858 val = val << shift;
2859
Mark Brown9bde4f02012-12-19 16:05:00 +00002860 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002861 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002862 return ret;
2863
2864 if (snd_soc_volsw_is_stereo(mc)) {
2865 val = ((ucontrol->value.integer.value[1] + min) & mask);
2866 if (invert)
2867 val = max - val;
2868 val_mask = mask << shift;
2869 val = val << shift;
2870
2871 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2872 }
2873
2874 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002875}
2876EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2877
2878/**
2879 * snd_soc_get_volsw_range - single mixer get callback with range
2880 * @kcontrol: mixer control
2881 * @ucontrol: control element information
2882 *
2883 * Callback to get the value, within a range, of a single mixer control.
2884 *
2885 * Returns 0 for success.
2886 */
2887int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2888 struct snd_ctl_elem_value *ucontrol)
2889{
2890 struct soc_mixer_control *mc =
2891 (struct soc_mixer_control *)kcontrol->private_value;
2892 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2893 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002894 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002895 unsigned int shift = mc->shift;
2896 int min = mc->min;
2897 int max = mc->max;
2898 unsigned int mask = (1 << fls(max)) - 1;
2899 unsigned int invert = mc->invert;
2900
2901 ucontrol->value.integer.value[0] =
2902 (snd_soc_read(codec, reg) >> shift) & mask;
2903 if (invert)
2904 ucontrol->value.integer.value[0] =
2905 max - ucontrol->value.integer.value[0];
2906 ucontrol->value.integer.value[0] =
2907 ucontrol->value.integer.value[0] - min;
2908
Mark Brown9bde4f02012-12-19 16:05:00 +00002909 if (snd_soc_volsw_is_stereo(mc)) {
2910 ucontrol->value.integer.value[1] =
2911 (snd_soc_read(codec, rreg) >> shift) & mask;
2912 if (invert)
2913 ucontrol->value.integer.value[1] =
2914 max - ucontrol->value.integer.value[1];
2915 ucontrol->value.integer.value[1] =
2916 ucontrol->value.integer.value[1] - min;
2917 }
2918
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002919 return 0;
2920}
2921EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2922
2923/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002924 * snd_soc_limit_volume - Set new limit to an existing volume control.
2925 *
2926 * @codec: where to look for the control
2927 * @name: Name of the control
2928 * @max: new maximum limit
2929 *
2930 * Return 0 for success, else error.
2931 */
2932int snd_soc_limit_volume(struct snd_soc_codec *codec,
2933 const char *name, int max)
2934{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002935 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002936 struct snd_kcontrol *kctl;
2937 struct soc_mixer_control *mc;
2938 int found = 0;
2939 int ret = -EINVAL;
2940
2941 /* Sanity check for name and max */
2942 if (unlikely(!name || max <= 0))
2943 return -EINVAL;
2944
2945 list_for_each_entry(kctl, &card->controls, list) {
2946 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2947 found = 1;
2948 break;
2949 }
2950 }
2951 if (found) {
2952 mc = (struct soc_mixer_control *)kctl->private_value;
2953 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002954 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002955 ret = 0;
2956 }
2957 }
2958 return ret;
2959}
2960EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2961
Mark Brown71d08512011-10-10 18:31:26 +01002962int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_info *uinfo)
2964{
2965 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2966 struct soc_bytes *params = (void *)kcontrol->private_value;
2967
2968 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2969 uinfo->count = params->num_regs * codec->val_bytes;
2970
2971 return 0;
2972}
2973EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2974
2975int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2976 struct snd_ctl_elem_value *ucontrol)
2977{
2978 struct soc_bytes *params = (void *)kcontrol->private_value;
2979 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2980 int ret;
2981
2982 if (codec->using_regmap)
2983 ret = regmap_raw_read(codec->control_data, params->base,
2984 ucontrol->value.bytes.data,
2985 params->num_regs * codec->val_bytes);
2986 else
2987 ret = -EINVAL;
2988
Mark Brownf831b052012-02-17 16:20:33 -08002989 /* Hide any masked bytes to ensure consistent data reporting */
2990 if (ret == 0 && params->mask) {
2991 switch (codec->val_bytes) {
2992 case 1:
2993 ucontrol->value.bytes.data[0] &= ~params->mask;
2994 break;
2995 case 2:
2996 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00002997 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08002998 break;
2999 case 4:
3000 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003001 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003002 break;
3003 default:
3004 return -EINVAL;
3005 }
3006 }
3007
Mark Brown71d08512011-10-10 18:31:26 +01003008 return ret;
3009}
3010EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3011
3012int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3013 struct snd_ctl_elem_value *ucontrol)
3014{
3015 struct soc_bytes *params = (void *)kcontrol->private_value;
3016 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003017 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003018 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003019 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003020
Mark Brownf831b052012-02-17 16:20:33 -08003021 if (!codec->using_regmap)
3022 return -EINVAL;
3023
Mark Brownf831b052012-02-17 16:20:33 -08003024 len = params->num_regs * codec->val_bytes;
3025
Mark Brownb5a8fe42013-01-20 21:42:22 +09003026 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3027 if (!data)
3028 return -ENOMEM;
3029
Mark Brownf831b052012-02-17 16:20:33 -08003030 /*
3031 * If we've got a mask then we need to preserve the register
3032 * bits. We shouldn't modify the incoming data so take a
3033 * copy.
3034 */
3035 if (params->mask) {
3036 ret = regmap_read(codec->control_data, params->base, &val);
3037 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003038 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003039
3040 val &= params->mask;
3041
Mark Brownf831b052012-02-17 16:20:33 -08003042 switch (codec->val_bytes) {
3043 case 1:
3044 ((u8 *)data)[0] &= ~params->mask;
3045 ((u8 *)data)[0] |= val;
3046 break;
3047 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003048 mask = ~params->mask;
3049 ret = regmap_parse_val(codec->control_data,
3050 &mask, &mask);
3051 if (ret != 0)
3052 goto out;
3053
3054 ((u16 *)data)[0] &= mask;
3055
3056 ret = regmap_parse_val(codec->control_data,
3057 &val, &val);
3058 if (ret != 0)
3059 goto out;
3060
3061 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003062 break;
3063 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003064 mask = ~params->mask;
3065 ret = regmap_parse_val(codec->control_data,
3066 &mask, &mask);
3067 if (ret != 0)
3068 goto out;
3069
3070 ((u32 *)data)[0] &= mask;
3071
3072 ret = regmap_parse_val(codec->control_data,
3073 &val, &val);
3074 if (ret != 0)
3075 goto out;
3076
3077 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003078 break;
3079 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003080 ret = -EINVAL;
3081 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003082 }
3083 }
3084
3085 ret = regmap_raw_write(codec->control_data, params->base,
3086 data, len);
3087
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003088out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003089 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003090
3091 return ret;
3092}
3093EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3094
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003095/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003096 * snd_soc_info_xr_sx - signed multi register info callback
3097 * @kcontrol: mreg control
3098 * @uinfo: control element information
3099 *
3100 * Callback to provide information of a control that can
3101 * span multiple codec registers which together
3102 * forms a single signed value in a MSB/LSB manner.
3103 *
3104 * Returns 0 for success.
3105 */
3106int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3107 struct snd_ctl_elem_info *uinfo)
3108{
3109 struct soc_mreg_control *mc =
3110 (struct soc_mreg_control *)kcontrol->private_value;
3111 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3112 uinfo->count = 1;
3113 uinfo->value.integer.min = mc->min;
3114 uinfo->value.integer.max = mc->max;
3115
3116 return 0;
3117}
3118EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3119
3120/**
3121 * snd_soc_get_xr_sx - signed multi register get callback
3122 * @kcontrol: mreg control
3123 * @ucontrol: control element information
3124 *
3125 * Callback to get the value of a control that can span
3126 * multiple codec registers which together forms a single
3127 * signed value in a MSB/LSB manner. The control supports
3128 * specifying total no of bits used to allow for bitfields
3129 * across the multiple codec registers.
3130 *
3131 * Returns 0 for success.
3132 */
3133int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3134 struct snd_ctl_elem_value *ucontrol)
3135{
3136 struct soc_mreg_control *mc =
3137 (struct soc_mreg_control *)kcontrol->private_value;
3138 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3139 unsigned int regbase = mc->regbase;
3140 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003141 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003142 unsigned int regwmask = (1<<regwshift)-1;
3143 unsigned int invert = mc->invert;
3144 unsigned long mask = (1UL<<mc->nbits)-1;
3145 long min = mc->min;
3146 long max = mc->max;
3147 long val = 0;
3148 unsigned long regval;
3149 unsigned int i;
3150
3151 for (i = 0; i < regcount; i++) {
3152 regval = snd_soc_read(codec, regbase+i) & regwmask;
3153 val |= regval << (regwshift*(regcount-i-1));
3154 }
3155 val &= mask;
3156 if (min < 0 && val > max)
3157 val |= ~mask;
3158 if (invert)
3159 val = max - val;
3160 ucontrol->value.integer.value[0] = val;
3161
3162 return 0;
3163}
3164EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3165
3166/**
3167 * snd_soc_put_xr_sx - signed multi register get callback
3168 * @kcontrol: mreg control
3169 * @ucontrol: control element information
3170 *
3171 * Callback to set the value of a control that can span
3172 * multiple codec registers which together forms a single
3173 * signed value in a MSB/LSB manner. The control supports
3174 * specifying total no of bits used to allow for bitfields
3175 * across the multiple codec registers.
3176 *
3177 * Returns 0 for success.
3178 */
3179int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3180 struct snd_ctl_elem_value *ucontrol)
3181{
3182 struct soc_mreg_control *mc =
3183 (struct soc_mreg_control *)kcontrol->private_value;
3184 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3185 unsigned int regbase = mc->regbase;
3186 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003187 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003188 unsigned int regwmask = (1<<regwshift)-1;
3189 unsigned int invert = mc->invert;
3190 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003191 long max = mc->max;
3192 long val = ucontrol->value.integer.value[0];
3193 unsigned int i, regval, regmask;
3194 int err;
3195
3196 if (invert)
3197 val = max - val;
3198 val &= mask;
3199 for (i = 0; i < regcount; i++) {
3200 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3201 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3202 err = snd_soc_update_bits_locked(codec, regbase+i,
3203 regmask, regval);
3204 if (err < 0)
3205 return err;
3206 }
3207
3208 return 0;
3209}
3210EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3211
3212/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003213 * snd_soc_get_strobe - strobe get callback
3214 * @kcontrol: mixer control
3215 * @ucontrol: control element information
3216 *
3217 * Callback get the value of a strobe mixer control.
3218 *
3219 * Returns 0 for success.
3220 */
3221int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3222 struct snd_ctl_elem_value *ucontrol)
3223{
3224 struct soc_mixer_control *mc =
3225 (struct soc_mixer_control *)kcontrol->private_value;
3226 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3227 unsigned int reg = mc->reg;
3228 unsigned int shift = mc->shift;
3229 unsigned int mask = 1 << shift;
3230 unsigned int invert = mc->invert != 0;
3231 unsigned int val = snd_soc_read(codec, reg) & mask;
3232
3233 if (shift != 0 && val != 0)
3234 val = val >> shift;
3235 ucontrol->value.enumerated.item[0] = val ^ invert;
3236
3237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3240
3241/**
3242 * snd_soc_put_strobe - strobe put callback
3243 * @kcontrol: mixer control
3244 * @ucontrol: control element information
3245 *
3246 * Callback strobe a register bit to high then low (or the inverse)
3247 * in one pass of a single mixer enum control.
3248 *
3249 * Returns 1 for success.
3250 */
3251int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3252 struct snd_ctl_elem_value *ucontrol)
3253{
3254 struct soc_mixer_control *mc =
3255 (struct soc_mixer_control *)kcontrol->private_value;
3256 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3257 unsigned int reg = mc->reg;
3258 unsigned int shift = mc->shift;
3259 unsigned int mask = 1 << shift;
3260 unsigned int invert = mc->invert != 0;
3261 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3262 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3263 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3264 int err;
3265
3266 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3267 if (err < 0)
3268 return err;
3269
3270 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3271 return err;
3272}
3273EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3274
3275/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003276 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3277 * @dai: DAI
3278 * @clk_id: DAI specific clock ID
3279 * @freq: new clock frequency in Hz
3280 * @dir: new clock direction - input/output.
3281 *
3282 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3283 */
3284int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3285 unsigned int freq, int dir)
3286{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003287 if (dai->driver && dai->driver->ops->set_sysclk)
3288 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003289 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003290 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003291 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003292 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003293 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003294}
3295EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3296
3297/**
Mark Brownec4ee522011-03-07 20:58:11 +00003298 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3299 * @codec: CODEC
3300 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003301 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003302 * @freq: new clock frequency in Hz
3303 * @dir: new clock direction - input/output.
3304 *
3305 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3306 */
3307int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003308 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003309{
3310 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003311 return codec->driver->set_sysclk(codec, clk_id, source,
3312 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003313 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003314 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003315}
3316EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3317
3318/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003319 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3320 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003321 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003322 * @div: new clock divisor.
3323 *
3324 * Configures the clock dividers. This is used to derive the best DAI bit and
3325 * frame clocks from the system or master clock. It's best to set the DAI bit
3326 * and frame clocks as low as possible to save system power.
3327 */
3328int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3329 int div_id, int div)
3330{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003331 if (dai->driver && dai->driver->ops->set_clkdiv)
3332 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003333 else
3334 return -EINVAL;
3335}
3336EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3337
3338/**
3339 * snd_soc_dai_set_pll - configure DAI PLL.
3340 * @dai: DAI
3341 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003342 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003343 * @freq_in: PLL input clock frequency in Hz
3344 * @freq_out: requested PLL output clock frequency in Hz
3345 *
3346 * Configures and enables PLL to generate output clock based on input clock.
3347 */
Mark Brown85488032009-09-05 18:52:16 +01003348int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3349 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003350{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003351 if (dai->driver && dai->driver->ops->set_pll)
3352 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003353 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003354 else if (dai->codec && dai->codec->driver->set_pll)
3355 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3356 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003357 else
3358 return -EINVAL;
3359}
3360EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3361
Mark Brownec4ee522011-03-07 20:58:11 +00003362/*
3363 * snd_soc_codec_set_pll - configure codec PLL.
3364 * @codec: CODEC
3365 * @pll_id: DAI specific PLL ID
3366 * @source: DAI specific source for the PLL
3367 * @freq_in: PLL input clock frequency in Hz
3368 * @freq_out: requested PLL output clock frequency in Hz
3369 *
3370 * Configures and enables PLL to generate output clock based on input clock.
3371 */
3372int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3373 unsigned int freq_in, unsigned int freq_out)
3374{
3375 if (codec->driver->set_pll)
3376 return codec->driver->set_pll(codec, pll_id, source,
3377 freq_in, freq_out);
3378 else
3379 return -EINVAL;
3380}
3381EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3382
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003383/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003384 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3385 * @dai: DAI
3386 * @ratio Ratio of BCLK to Sample rate.
3387 *
3388 * Configures the DAI for a preset BCLK to sample rate ratio.
3389 */
3390int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3391{
3392 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3393 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3394 else
3395 return -EINVAL;
3396}
3397EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3398
3399/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003400 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3401 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003402 * @fmt: SND_SOC_DAIFMT_ format value.
3403 *
3404 * Configures the DAI hardware format and clocking.
3405 */
3406int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3407{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003408 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003409 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003410 if (dai->driver->ops->set_fmt == NULL)
3411 return -ENOTSUPP;
3412 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003413}
3414EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3415
3416/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003417 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003418 * @slots: Number of slots in use.
3419 * @tx_mask: bitmask representing active TX slots.
3420 * @rx_mask: bitmask representing active RX slots.
3421 *
3422 * Generates the TDM tx and rx slot default masks for DAI.
3423 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003424static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003425 unsigned int *tx_mask,
3426 unsigned int *rx_mask)
3427{
3428 if (*tx_mask || *rx_mask)
3429 return 0;
3430
3431 if (!slots)
3432 return -EINVAL;
3433
3434 *tx_mask = (1 << slots) - 1;
3435 *rx_mask = (1 << slots) - 1;
3436
3437 return 0;
3438}
3439
3440/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003441 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3442 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003443 * @tx_mask: bitmask representing active TX slots.
3444 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003445 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003446 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003447 *
3448 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3449 * specific.
3450 */
3451int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003452 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003453{
Xiubo Lie5c21512014-03-21 14:17:12 +08003454 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3455 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003456 &tx_mask, &rx_mask);
3457 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003458 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003459
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003460 if (dai->driver && dai->driver->ops->set_tdm_slot)
3461 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003462 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003463 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003464 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003465}
3466EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3467
3468/**
Barry Song472df3c2009-09-12 01:16:29 +08003469 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3470 * @dai: DAI
3471 * @tx_num: how many TX channels
3472 * @tx_slot: pointer to an array which imply the TX slot number channel
3473 * 0~num-1 uses
3474 * @rx_num: how many RX channels
3475 * @rx_slot: pointer to an array which imply the RX slot number channel
3476 * 0~num-1 uses
3477 *
3478 * configure the relationship between channel number and TDM slot number.
3479 */
3480int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3481 unsigned int tx_num, unsigned int *tx_slot,
3482 unsigned int rx_num, unsigned int *rx_slot)
3483{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003484 if (dai->driver && dai->driver->ops->set_channel_map)
3485 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003486 rx_num, rx_slot);
3487 else
3488 return -EINVAL;
3489}
3490EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3491
3492/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003493 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3494 * @dai: DAI
3495 * @tristate: tristate enable
3496 *
3497 * Tristates the DAI so that others can use it.
3498 */
3499int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3500{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003501 if (dai->driver && dai->driver->ops->set_tristate)
3502 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003503 else
3504 return -EINVAL;
3505}
3506EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3507
3508/**
3509 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3510 * @dai: DAI
3511 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003512 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003513 *
3514 * Mutes the DAI DAC.
3515 */
Mark Brownda183962013-02-06 15:44:07 +00003516int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3517 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003518{
Mark Brownda183962013-02-06 15:44:07 +00003519 if (!dai->driver)
3520 return -ENOTSUPP;
3521
3522 if (dai->driver->ops->mute_stream)
3523 return dai->driver->ops->mute_stream(dai, mute, direction);
3524 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3525 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003526 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003527 else
Mark Brown04570c62012-04-13 19:16:03 +01003528 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003529}
3530EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3531
Mark Brownc5af3a22008-11-28 13:29:45 +00003532/**
3533 * snd_soc_register_card - Register a card with the ASoC core
3534 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003535 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003536 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003537 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303538int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003539{
Mark Brownb19e6e72012-03-14 21:18:39 +00003540 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541
Mark Brownc5af3a22008-11-28 13:29:45 +00003542 if (!card->name || !card->dev)
3543 return -EINVAL;
3544
Stephen Warren5a504962011-12-21 10:40:59 -07003545 for (i = 0; i < card->num_links; i++) {
3546 struct snd_soc_dai_link *link = &card->dai_link[i];
3547
3548 /*
3549 * Codec must be specified by 1 of name or OF node,
3550 * not both or neither.
3551 */
3552 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003553 dev_err(card->dev,
3554 "ASoC: Neither/both codec name/of_node are set for %s\n",
3555 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003556 return -EINVAL;
3557 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003558 /* Codec DAI name must be specified */
3559 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003560 dev_err(card->dev,
3561 "ASoC: codec_dai_name not set for %s\n",
3562 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003563 return -EINVAL;
3564 }
Stephen Warren5a504962011-12-21 10:40:59 -07003565
3566 /*
3567 * Platform may be specified by either name or OF node, but
3568 * can be left unspecified, and a dummy platform will be used.
3569 */
3570 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003571 dev_err(card->dev,
3572 "ASoC: Both platform name/of_node are set for %s\n",
3573 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003574 return -EINVAL;
3575 }
3576
3577 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003578 * CPU device may be specified by either name or OF node, but
3579 * can be left unspecified, and will be matched based on DAI
3580 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003581 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003582 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003583 dev_err(card->dev,
3584 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3585 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003586 return -EINVAL;
3587 }
3588 /*
3589 * At least one of CPU DAI name or CPU device name/node must be
3590 * specified
3591 */
3592 if (!link->cpu_dai_name &&
3593 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003594 dev_err(card->dev,
3595 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3596 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003597 return -EINVAL;
3598 }
3599 }
3600
Mark Browned77cc12011-05-03 18:25:34 +01003601 dev_set_drvdata(card->dev, card);
3602
Stephen Warren111c6412011-01-28 14:26:35 -07003603 snd_soc_initialize_card_lists(card);
3604
Vinod Koul150dd2f2011-01-13 22:48:32 +05303605 soc_init_card_debugfs(card);
3606
Mark Brown181a6892012-03-14 20:18:49 +00003607 card->rtd = devm_kzalloc(card->dev,
3608 sizeof(struct snd_soc_pcm_runtime) *
3609 (card->num_links + card->num_aux_devs),
3610 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003611 if (card->rtd == NULL)
3612 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003613 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003614 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003615
3616 for (i = 0; i < card->num_links; i++)
3617 card->rtd[i].dai_link = &card->dai_link[i];
3618
Mark Brownc5af3a22008-11-28 13:29:45 +00003619 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003620 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003621 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003622 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003623 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003624
Mark Brownb19e6e72012-03-14 21:18:39 +00003625 ret = snd_soc_instantiate_card(card);
3626 if (ret != 0)
3627 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003628
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003629 /* deactivate pins to sleep state */
3630 for (i = 0; i < card->num_rtd; i++) {
3631 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3632 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3633 if (!codec_dai->active)
3634 pinctrl_pm_select_sleep_state(codec_dai->dev);
3635 if (!cpu_dai->active)
3636 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3637 }
3638
Mark Brownb19e6e72012-03-14 21:18:39 +00003639 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003640}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303641EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003642
3643/**
3644 * snd_soc_unregister_card - Unregister a card with the ASoC core
3645 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003646 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003647 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003648 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303649int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003650{
Vinod Koulb0e26482011-01-13 22:48:02 +05303651 if (card->instantiated)
3652 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003653 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003654
3655 return 0;
3656}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303657EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003658
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003659/*
3660 * Simplify DAI link configuration by removing ".-1" from device names
3661 * and sanitizing names.
3662 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003663static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003664{
3665 char *found, name[NAME_SIZE];
3666 int id1, id2;
3667
3668 if (dev_name(dev) == NULL)
3669 return NULL;
3670
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003671 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003672
3673 /* are we a "%s.%d" name (platform and SPI components) */
3674 found = strstr(name, dev->driver->name);
3675 if (found) {
3676 /* get ID */
3677 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3678
3679 /* discard ID from name if ID == -1 */
3680 if (*id == -1)
3681 found[strlen(dev->driver->name)] = '\0';
3682 }
3683
3684 } else {
3685 /* I2C component devices are named "bus-addr" */
3686 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3687 char tmp[NAME_SIZE];
3688
3689 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003690 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003691
3692 /* sanitize component name for DAI link creation */
3693 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003694 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003695 } else
3696 *id = 0;
3697 }
3698
3699 return kstrdup(name, GFP_KERNEL);
3700}
3701
3702/*
3703 * Simplify DAI link naming for single devices with multiple DAIs by removing
3704 * any ".-1" and using the DAI name (instead of device name).
3705 */
3706static inline char *fmt_multiple_name(struct device *dev,
3707 struct snd_soc_dai_driver *dai_drv)
3708{
3709 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003710 dev_err(dev,
3711 "ASoC: error - multiple DAI %s registered with no name\n",
3712 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003713 return NULL;
3714 }
3715
3716 return kstrdup(dai_drv->name, GFP_KERNEL);
3717}
3718
Mark Brown91151712008-11-30 23:31:24 +00003719/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003720 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003721 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003722 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003723 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003724static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003725{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003726 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003727
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003728 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003729 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3730 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003731 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003732 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003733 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003734 }
Mark Brown91151712008-11-30 23:31:24 +00003735}
Mark Brown91151712008-11-30 23:31:24 +00003736
3737/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003738 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003739 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003740 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003741 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3742 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003743 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003744 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003745 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3746 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003747 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003748static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003749 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3750 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003751{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003752 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003753 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003754 unsigned int i;
3755 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003756
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003757 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003758
3759 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003760
3761 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003762 if (dai == NULL) {
3763 ret = -ENOMEM;
3764 goto err;
3765 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003766
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003767 /*
3768 * Back in the old days when we still had component-less DAIs,
3769 * instead of having a static name, component-less DAIs would
3770 * inherit the name of the parent device so it is possible to
3771 * register multiple instances of the DAI. We still need to keep
3772 * the same naming style even though those DAIs are not
3773 * component-less anymore.
3774 */
3775 if (count == 1 && legacy_dai_naming) {
3776 dai->name = fmt_single_name(dev, &dai->id);
3777 } else {
3778 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3779 if (dai_drv[i].id)
3780 dai->id = dai_drv[i].id;
3781 else
3782 dai->id = i;
3783 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003784 if (dai->name == NULL) {
3785 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003786 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003787 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003788 }
3789
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003790 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003791 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003792 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003793 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003794 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003795 if (!dai->driver->ops)
3796 dai->driver->ops = &null_dai_ops;
3797
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003798 if (!dai->codec)
3799 dai->dapm.idle_bias_off = 1;
3800
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003801 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003802
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003803 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003804 }
3805
3806 return 0;
3807
3808err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003809 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003810
3811 return ret;
3812}
Mark Brown91151712008-11-30 23:31:24 +00003813
3814/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003815 * snd_soc_register_component - Register a component with the ASoC core
3816 *
3817 */
3818static int
3819__snd_soc_register_component(struct device *dev,
3820 struct snd_soc_component *cmpnt,
3821 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003822 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003823 struct snd_soc_dai_driver *dai_drv,
3824 int num_dai, bool allow_single_dai)
3825{
3826 int ret;
3827
3828 dev_dbg(dev, "component register %s\n", dev_name(dev));
3829
3830 if (!cmpnt) {
3831 dev_err(dev, "ASoC: Failed to connecting component\n");
3832 return -ENOMEM;
3833 }
3834
3835 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
3836 if (!cmpnt->name) {
3837 dev_err(dev, "ASoC: Failed to simplifying name\n");
3838 return -ENOMEM;
3839 }
3840
3841 cmpnt->dev = dev;
3842 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003843 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003844 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003845 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003846
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003847 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
3848 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003849 if (ret < 0) {
3850 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3851 goto error_component_name;
3852 }
3853
3854 mutex_lock(&client_mutex);
3855 list_add(&cmpnt->list, &component_list);
3856 mutex_unlock(&client_mutex);
3857
3858 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
3859
3860 return ret;
3861
3862error_component_name:
3863 kfree(cmpnt->name);
3864
3865 return ret;
3866}
3867
3868int snd_soc_register_component(struct device *dev,
3869 const struct snd_soc_component_driver *cmpnt_drv,
3870 struct snd_soc_dai_driver *dai_drv,
3871 int num_dai)
3872{
3873 struct snd_soc_component *cmpnt;
3874
3875 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
3876 if (!cmpnt) {
3877 dev_err(dev, "ASoC: Failed to allocate memory\n");
3878 return -ENOMEM;
3879 }
3880
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003881 cmpnt->ignore_pmdown_time = true;
3882
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003883 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003884 dai_drv, num_dai, true);
3885}
3886EXPORT_SYMBOL_GPL(snd_soc_register_component);
3887
3888/**
3889 * snd_soc_unregister_component - Unregister a component from the ASoC core
3890 *
3891 */
3892void snd_soc_unregister_component(struct device *dev)
3893{
3894 struct snd_soc_component *cmpnt;
3895
3896 list_for_each_entry(cmpnt, &component_list, list) {
3897 if (dev == cmpnt->dev)
3898 goto found;
3899 }
3900 return;
3901
3902found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003903 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003904
3905 mutex_lock(&client_mutex);
3906 list_del(&cmpnt->list);
3907 mutex_unlock(&client_mutex);
3908
3909 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
3910 kfree(cmpnt->name);
3911}
3912EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3913
3914/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003915 * snd_soc_add_platform - Add a platform to the ASoC core
3916 * @dev: The parent device for the platform
3917 * @platform: The platform to add
3918 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003919 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003920int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003921 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003922{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003923 /* create platform component name */
3924 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03003925 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003926 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003927
3928 platform->dev = dev;
3929 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003930 platform->dapm.dev = dev;
3931 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003932 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003933 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003934
3935 mutex_lock(&client_mutex);
3936 list_add(&platform->list, &platform_list);
3937 mutex_unlock(&client_mutex);
3938
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003939 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003940
3941 return 0;
3942}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003943EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3944
3945/**
3946 * snd_soc_register_platform - Register a platform with the ASoC core
3947 *
3948 * @platform: platform to register
3949 */
3950int snd_soc_register_platform(struct device *dev,
3951 const struct snd_soc_platform_driver *platform_drv)
3952{
3953 struct snd_soc_platform *platform;
3954 int ret;
3955
3956 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3957
3958 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3959 if (platform == NULL)
3960 return -ENOMEM;
3961
3962 ret = snd_soc_add_platform(dev, platform, platform_drv);
3963 if (ret)
3964 kfree(platform);
3965
3966 return ret;
3967}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003968EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3969
3970/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003971 * snd_soc_remove_platform - Remove a platform from the ASoC core
3972 * @platform: the platform to remove
3973 */
3974void snd_soc_remove_platform(struct snd_soc_platform *platform)
3975{
3976 mutex_lock(&client_mutex);
3977 list_del(&platform->list);
3978 mutex_unlock(&client_mutex);
3979
3980 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
3981 platform->name);
3982 kfree(platform->name);
3983}
3984EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
3985
3986struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
3987{
3988 struct snd_soc_platform *platform;
3989
3990 list_for_each_entry(platform, &platform_list, list) {
3991 if (dev == platform->dev)
3992 return platform;
3993 }
3994
3995 return NULL;
3996}
3997EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
3998
3999/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004000 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4001 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004002 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004003 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004005{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004006 struct snd_soc_platform *platform;
4007
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004008 platform = snd_soc_lookup_platform(dev);
4009 if (!platform)
4010 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004011
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004012 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004013 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004014}
4015EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4016
Mark Brown151ab222009-05-09 16:22:58 +01004017static u64 codec_format_map[] = {
4018 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4019 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4020 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4021 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4022 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4023 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4024 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4025 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4026 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4027 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4028 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4029 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4030 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4031 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4032 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4033 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4034};
4035
4036/* Fix up the DAI formats for endianness: codecs don't actually see
4037 * the endianness of the data but we're using the CPU format
4038 * definitions which do need to include endianness so we ensure that
4039 * codec DAIs always have both big and little endian variants set.
4040 */
4041static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4042{
4043 int i;
4044
4045 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4046 if (stream->formats & codec_format_map[i])
4047 stream->formats |= codec_format_map[i];
4048}
4049
Mark Brown0d0cf002008-12-10 14:32:45 +00004050/**
4051 * snd_soc_register_codec - Register a codec with the ASoC core
4052 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004053 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004054 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004055int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004056 const struct snd_soc_codec_driver *codec_drv,
4057 struct snd_soc_dai_driver *dai_drv,
4058 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004059{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004060 struct snd_soc_codec *codec;
4061 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004062
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004063 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004064
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004065 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4066 if (codec == NULL)
4067 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004068
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004069 /* create CODEC component name */
4070 codec->name = fmt_single_name(dev, &codec->id);
4071 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004072 ret = -ENOMEM;
4073 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004074 }
4075
Mark Brownc3acec22010-12-02 16:15:29 +00004076 codec->write = codec_drv->write;
4077 codec->read = codec_drv->read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004078 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004079 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4080 codec->dapm.dev = dev;
4081 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004082 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004083 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004084 codec->dev = dev;
4085 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004086 codec->num_dai = num_dai;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01004087 codec->val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004088 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004089
4090 for (i = 0; i < num_dai; i++) {
4091 fixup_codec_formats(&dai_drv[i].playback);
4092 fixup_codec_formats(&dai_drv[i].capture);
4093 }
4094
Mark Brown054880f2012-04-09 17:29:19 +01004095 mutex_lock(&client_mutex);
4096 list_add(&codec->list, &codec_list);
4097 mutex_unlock(&client_mutex);
4098
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004099 /* register component */
4100 ret = __snd_soc_register_component(dev, &codec->component,
4101 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004102 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004103 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004104 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004105 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004106 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004107
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004108 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004109 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004110
Kuninori Morimotof790b942013-02-25 00:40:09 -08004111fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004112 mutex_lock(&client_mutex);
4113 list_del(&codec->list);
4114 mutex_unlock(&client_mutex);
4115
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004116 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004117fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004118 kfree(codec);
4119 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004120}
4121EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4122
4123/**
4124 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4125 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004126 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004127 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004128void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004129{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004130 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004131
4132 list_for_each_entry(codec, &codec_list, list) {
4133 if (dev == codec->dev)
4134 goto found;
4135 }
4136 return;
4137
4138found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004139 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004140
Mark Brown0d0cf002008-12-10 14:32:45 +00004141 mutex_lock(&client_mutex);
4142 list_del(&codec->list);
4143 mutex_unlock(&client_mutex);
4144
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004145 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004146
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004147 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004148 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004149 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004150}
4151EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4152
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004153/* Retrieve a card's name from device tree */
4154int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4155 const char *propname)
4156{
4157 struct device_node *np = card->dev->of_node;
4158 int ret;
4159
4160 ret = of_property_read_string_index(np, propname, 0, &card->name);
4161 /*
4162 * EINVAL means the property does not exist. This is fine providing
4163 * card->name was previously set, which is checked later in
4164 * snd_soc_register_card.
4165 */
4166 if (ret < 0 && ret != -EINVAL) {
4167 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004168 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004169 propname, ret);
4170 return ret;
4171 }
4172
4173 return 0;
4174}
4175EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4176
Xiubo Li9a6d4862014-02-08 15:59:52 +08004177static const struct snd_soc_dapm_widget simple_widgets[] = {
4178 SND_SOC_DAPM_MIC("Microphone", NULL),
4179 SND_SOC_DAPM_LINE("Line", NULL),
4180 SND_SOC_DAPM_HP("Headphone", NULL),
4181 SND_SOC_DAPM_SPK("Speaker", NULL),
4182};
4183
4184int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4185 const char *propname)
4186{
4187 struct device_node *np = card->dev->of_node;
4188 struct snd_soc_dapm_widget *widgets;
4189 const char *template, *wname;
4190 int i, j, num_widgets, ret;
4191
4192 num_widgets = of_property_count_strings(np, propname);
4193 if (num_widgets < 0) {
4194 dev_err(card->dev,
4195 "ASoC: Property '%s' does not exist\n", propname);
4196 return -EINVAL;
4197 }
4198 if (num_widgets & 1) {
4199 dev_err(card->dev,
4200 "ASoC: Property '%s' length is not even\n", propname);
4201 return -EINVAL;
4202 }
4203
4204 num_widgets /= 2;
4205 if (!num_widgets) {
4206 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4207 propname);
4208 return -EINVAL;
4209 }
4210
4211 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4212 GFP_KERNEL);
4213 if (!widgets) {
4214 dev_err(card->dev,
4215 "ASoC: Could not allocate memory for widgets\n");
4216 return -ENOMEM;
4217 }
4218
4219 for (i = 0; i < num_widgets; i++) {
4220 ret = of_property_read_string_index(np, propname,
4221 2 * i, &template);
4222 if (ret) {
4223 dev_err(card->dev,
4224 "ASoC: Property '%s' index %d read error:%d\n",
4225 propname, 2 * i, ret);
4226 return -EINVAL;
4227 }
4228
4229 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4230 if (!strncmp(template, simple_widgets[j].name,
4231 strlen(simple_widgets[j].name))) {
4232 widgets[i] = simple_widgets[j];
4233 break;
4234 }
4235 }
4236
4237 if (j >= ARRAY_SIZE(simple_widgets)) {
4238 dev_err(card->dev,
4239 "ASoC: DAPM widget '%s' is not supported\n",
4240 template);
4241 return -EINVAL;
4242 }
4243
4244 ret = of_property_read_string_index(np, propname,
4245 (2 * i) + 1,
4246 &wname);
4247 if (ret) {
4248 dev_err(card->dev,
4249 "ASoC: Property '%s' index %d read error:%d\n",
4250 propname, (2 * i) + 1, ret);
4251 return -EINVAL;
4252 }
4253
4254 widgets[i].name = wname;
4255 }
4256
4257 card->dapm_widgets = widgets;
4258 card->num_dapm_widgets = num_widgets;
4259
4260 return 0;
4261}
4262EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4263
Xiubo Li89c67852014-02-14 09:34:35 +08004264int snd_soc_of_parse_tdm_slot(struct device_node *np,
4265 unsigned int *slots,
4266 unsigned int *slot_width)
4267{
4268 u32 val;
4269 int ret;
4270
4271 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4272 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4273 if (ret)
4274 return ret;
4275
4276 if (slots)
4277 *slots = val;
4278 }
4279
4280 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4281 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4282 if (ret)
4283 return ret;
4284
4285 if (slot_width)
4286 *slot_width = val;
4287 }
4288
4289 return 0;
4290}
4291EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4292
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004293int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4294 const char *propname)
4295{
4296 struct device_node *np = card->dev->of_node;
4297 int num_routes;
4298 struct snd_soc_dapm_route *routes;
4299 int i, ret;
4300
4301 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004302 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004303 dev_err(card->dev,
4304 "ASoC: Property '%s' does not exist or its length is not even\n",
4305 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004306 return -EINVAL;
4307 }
4308 num_routes /= 2;
4309 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004310 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004311 propname);
4312 return -EINVAL;
4313 }
4314
4315 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4316 GFP_KERNEL);
4317 if (!routes) {
4318 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004319 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004320 return -EINVAL;
4321 }
4322
4323 for (i = 0; i < num_routes; i++) {
4324 ret = of_property_read_string_index(np, propname,
4325 2 * i, &routes[i].sink);
4326 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004327 dev_err(card->dev,
4328 "ASoC: Property '%s' index %d could not be read: %d\n",
4329 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004330 return -EINVAL;
4331 }
4332 ret = of_property_read_string_index(np, propname,
4333 (2 * i) + 1, &routes[i].source);
4334 if (ret) {
4335 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004336 "ASoC: Property '%s' index %d could not be read: %d\n",
4337 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004338 return -EINVAL;
4339 }
4340 }
4341
4342 card->num_dapm_routes = num_routes;
4343 card->dapm_routes = routes;
4344
4345 return 0;
4346}
4347EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4348
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004349unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4350 const char *prefix)
4351{
4352 int ret, i;
4353 char prop[128];
4354 unsigned int format = 0;
4355 int bit, frame;
4356 const char *str;
4357 struct {
4358 char *name;
4359 unsigned int val;
4360 } of_fmt_table[] = {
4361 { "i2s", SND_SOC_DAIFMT_I2S },
4362 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4363 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4364 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4365 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4366 { "ac97", SND_SOC_DAIFMT_AC97 },
4367 { "pdm", SND_SOC_DAIFMT_PDM},
4368 { "msb", SND_SOC_DAIFMT_MSB },
4369 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004370 };
4371
4372 if (!prefix)
4373 prefix = "";
4374
4375 /*
4376 * check "[prefix]format = xxx"
4377 * SND_SOC_DAIFMT_FORMAT_MASK area
4378 */
4379 snprintf(prop, sizeof(prop), "%sformat", prefix);
4380 ret = of_property_read_string(np, prop, &str);
4381 if (ret == 0) {
4382 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4383 if (strcmp(str, of_fmt_table[i].name) == 0) {
4384 format |= of_fmt_table[i].val;
4385 break;
4386 }
4387 }
4388 }
4389
4390 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004391 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004392 * SND_SOC_DAIFMT_CLOCK_MASK area
4393 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004394 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4395 if (of_get_property(np, prop, NULL))
4396 format |= SND_SOC_DAIFMT_CONT;
4397 else
4398 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004399
4400 /*
4401 * check "[prefix]bitclock-inversion"
4402 * check "[prefix]frame-inversion"
4403 * SND_SOC_DAIFMT_INV_MASK area
4404 */
4405 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4406 bit = !!of_get_property(np, prop, NULL);
4407
4408 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4409 frame = !!of_get_property(np, prop, NULL);
4410
4411 switch ((bit << 4) + frame) {
4412 case 0x11:
4413 format |= SND_SOC_DAIFMT_IB_IF;
4414 break;
4415 case 0x10:
4416 format |= SND_SOC_DAIFMT_IB_NF;
4417 break;
4418 case 0x01:
4419 format |= SND_SOC_DAIFMT_NB_IF;
4420 break;
4421 default:
4422 /* SND_SOC_DAIFMT_NB_NF is default */
4423 break;
4424 }
4425
4426 /*
4427 * check "[prefix]bitclock-master"
4428 * check "[prefix]frame-master"
4429 * SND_SOC_DAIFMT_MASTER_MASK area
4430 */
4431 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4432 bit = !!of_get_property(np, prop, NULL);
4433
4434 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4435 frame = !!of_get_property(np, prop, NULL);
4436
4437 switch ((bit << 4) + frame) {
4438 case 0x11:
4439 format |= SND_SOC_DAIFMT_CBM_CFM;
4440 break;
4441 case 0x10:
4442 format |= SND_SOC_DAIFMT_CBM_CFS;
4443 break;
4444 case 0x01:
4445 format |= SND_SOC_DAIFMT_CBS_CFM;
4446 break;
4447 default:
4448 format |= SND_SOC_DAIFMT_CBS_CFS;
4449 break;
4450 }
4451
4452 return format;
4453}
4454EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4455
Kuninori Morimotocb470082013-09-10 17:39:56 -07004456int snd_soc_of_get_dai_name(struct device_node *of_node,
4457 const char **dai_name)
4458{
4459 struct snd_soc_component *pos;
4460 struct of_phandle_args args;
4461 int ret;
4462
4463 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4464 "#sound-dai-cells", 0, &args);
4465 if (ret)
4466 return ret;
4467
4468 ret = -EPROBE_DEFER;
4469
4470 mutex_lock(&client_mutex);
4471 list_for_each_entry(pos, &component_list, list) {
4472 if (pos->dev->of_node != args.np)
4473 continue;
4474
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004475 if (pos->driver->of_xlate_dai_name) {
4476 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4477 } else {
4478 int id = -1;
4479
4480 switch (args.args_count) {
4481 case 0:
4482 id = 0; /* same as dai_drv[0] */
4483 break;
4484 case 1:
4485 id = args.args[0];
4486 break;
4487 default:
4488 /* not supported */
4489 break;
4490 }
4491
4492 if (id < 0 || id >= pos->num_dai) {
4493 ret = -EINVAL;
Xiubo Lie41975e2013-12-20 14:39:51 +08004494 break;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004495 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004496
4497 ret = 0;
4498
4499 *dai_name = pos->dai_drv[id].name;
4500 if (!*dai_name)
4501 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004502 }
4503
Kuninori Morimotocb470082013-09-10 17:39:56 -07004504 break;
4505 }
4506 mutex_unlock(&client_mutex);
4507
4508 of_node_put(args.np);
4509
4510 return ret;
4511}
4512EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4513
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004514static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004515{
Mark Brown384c89e2008-12-03 17:34:03 +00004516#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004517 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4518 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004519 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004520 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004521 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004522
Mark Brown8a9dab12011-01-10 22:25:21 +00004523 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004524 &codec_list_fops))
4525 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4526
Mark Brown8a9dab12011-01-10 22:25:21 +00004527 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004528 &dai_list_fops))
4529 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004530
Mark Brown8a9dab12011-01-10 22:25:21 +00004531 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004532 &platform_list_fops))
4533 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004534#endif
4535
Mark Brownfb257892011-04-28 10:57:54 +01004536 snd_soc_util_init();
4537
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004538 return platform_driver_register(&soc_driver);
4539}
Mark Brown4abe8e12010-10-12 17:41:03 +01004540module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004541
Mark Brown7d8c16a2008-11-30 22:11:24 +00004542static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004543{
Mark Brownfb257892011-04-28 10:57:54 +01004544 snd_soc_util_exit();
4545
Mark Brown384c89e2008-12-03 17:34:03 +00004546#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004547 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004548#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004549 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004550}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004551module_exit(snd_soc_exit);
4552
4553/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004554MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004555MODULE_DESCRIPTION("ALSA SoC Core");
4556MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004557MODULE_ALIAS("platform:soc-audio");