blob: 78f0c51c6c833e68c10350081f937d2f7ad75da1 [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
Nariman Poushinb318ad52014-04-01 13:59:33 +01001123 if (driver->dapm_widgets) {
1124 ret = snd_soc_dapm_new_controls(&codec->dapm,
1125 driver->dapm_widgets,
1126 driver->num_dapm_widgets);
1127
1128 if (ret != 0) {
1129 dev_err(codec->dev,
1130 "Failed to create new controls %d\n", ret);
1131 goto err_probe;
1132 }
1133 }
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001134
Mark Brown888df392012-02-16 19:37:51 -08001135 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001136 list_for_each_entry(dai, &codec->component.dai_list, list) {
1137 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1138
1139 if (ret != 0) {
1140 dev_err(codec->dev,
1141 "Failed to create DAI widgets %d\n", ret);
1142 goto err_probe;
1143 }
1144 }
Mark Brown888df392012-02-16 19:37:51 -08001145
Mark Brown33c5f962011-08-22 18:40:30 +01001146 codec->dapm.idle_bias_off = driver->idle_bias_off;
1147
Xiubo Lia32c17b2014-03-11 12:43:22 +08001148 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1149 /* Set the default I/O up try regmap */
1150 ret = snd_soc_codec_set_cache_io(codec, NULL);
1151 if (ret < 0) {
1152 dev_err(codec->dev,
1153 "Failed to set cache I/O: %d\n", ret);
1154 goto err_probe;
1155 }
1156 }
Xiubo Life2265e2014-02-27 17:49:52 +08001157
Mark Brown89b95ac02011-03-07 16:38:44 +00001158 if (driver->probe) {
1159 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001160 if (ret < 0) {
1161 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001162 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001163 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001164 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001165 WARN(codec->dapm.idle_bias_off &&
1166 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001167 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1168 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001169 }
1170
Mark Brownb7af1da2011-04-07 19:18:44 +09001171 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001172 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001173 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001174 if (driver->dapm_routes)
1175 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1176 driver->num_dapm_routes);
1177
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 /* mark codec as probed and add to card codec list */
1179 codec->probed = 1;
1180 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001181 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001182
Jarkko Nikula70d29332011-01-27 16:24:22 +02001183 return 0;
1184
1185err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001186 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001187 module_put(codec->dev->driver->owner);
1188
Jarkko Nikula589c3562010-12-06 16:27:07 +02001189 return ret;
1190}
1191
Liam Girdwood956245e2011-07-01 16:54:08 +01001192static int soc_probe_platform(struct snd_soc_card *card,
1193 struct snd_soc_platform *platform)
1194{
1195 int ret = 0;
1196 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001197 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001198 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001199
1200 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001201 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001202
1203 if (!try_module_get(platform->dev->driver->owner))
1204 return -ENODEV;
1205
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001206 soc_init_platform_debugfs(platform);
1207
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001208 if (driver->dapm_widgets)
1209 snd_soc_dapm_new_controls(&platform->dapm,
1210 driver->dapm_widgets, driver->num_dapm_widgets);
1211
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001212 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001213 list_for_each_entry(component, &component_list, list) {
1214 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001215 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001216 list_for_each_entry(dai, &component->dai_list, list)
1217 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001218 }
1219
Stephen Warren3fec6b62012-04-05 12:28:01 -06001220 platform->dapm.idle_bias_off = 1;
1221
Liam Girdwood956245e2011-07-01 16:54:08 +01001222 if (driver->probe) {
1223 ret = driver->probe(platform);
1224 if (ret < 0) {
1225 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001226 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001227 goto err_probe;
1228 }
1229 }
1230
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001231 if (driver->controls)
1232 snd_soc_add_platform_controls(platform, driver->controls,
1233 driver->num_controls);
1234 if (driver->dapm_routes)
1235 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1236 driver->num_dapm_routes);
1237
Liam Girdwood956245e2011-07-01 16:54:08 +01001238 /* mark platform as probed and add to card platform list */
1239 platform->probed = 1;
1240 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001241 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001242
1243 return 0;
1244
1245err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001246 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001247 module_put(platform->dev->driver->owner);
1248
1249 return ret;
1250}
1251
Mark Brown36ae1a92012-01-06 17:12:45 -08001252static void rtd_release(struct device *dev)
1253{
1254 kfree(dev);
1255}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256
Jarkko Nikula589c3562010-12-06 16:27:07 +02001257static int soc_post_component_init(struct snd_soc_card *card,
1258 struct snd_soc_codec *codec,
1259 int num, int dailess)
1260{
1261 struct snd_soc_dai_link *dai_link = NULL;
1262 struct snd_soc_aux_dev *aux_dev = NULL;
1263 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001264 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001265 int ret = 0;
1266
1267 if (!dailess) {
1268 dai_link = &card->dai_link[num];
1269 rtd = &card->rtd[num];
1270 name = dai_link->name;
1271 } else {
1272 aux_dev = &card->aux_dev[num];
1273 rtd = &card->rtd_aux[num];
1274 name = aux_dev->name;
1275 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001276 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001277
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 /* do machine specific initialization */
1279 if (!dailess && dai_link->init)
1280 ret = dai_link->init(rtd);
1281 else if (dailess && aux_dev->init)
1282 ret = aux_dev->init(&codec->dapm);
1283 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001284 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001285 return ret;
1286 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287
Jarkko Nikula589c3562010-12-06 16:27:07 +02001288 /* register the rtd device */
1289 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001290
1291 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1292 if (!rtd->dev)
1293 return -ENOMEM;
1294 device_initialize(rtd->dev);
1295 rtd->dev->parent = card->dev;
1296 rtd->dev->release = rtd_release;
1297 rtd->dev->init_name = name;
1298 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001299 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001300 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1301 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1302 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1303 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001304 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001305 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001306 /* calling put_device() here to free the rtd->dev */
1307 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001308 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001309 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001310 return ret;
1311 }
1312 rtd->dev_registered = 1;
1313
1314 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001315 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001316 if (ret < 0)
1317 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001318 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001319
1320 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001321 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001322 if (ret < 0)
1323 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001324 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001325
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001326#ifdef CONFIG_DEBUG_FS
1327 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001328 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001329 goto out;
1330
1331 ret = soc_dpcm_debugfs_add(rtd);
1332 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001333 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001334
1335out:
1336#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001337 return 0;
1338}
1339
Stephen Warren62ae68f2012-06-08 12:34:23 -06001340static int soc_probe_link_components(struct snd_soc_card *card, int num,
1341 int order)
1342{
1343 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1344 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1345 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1346 struct snd_soc_platform *platform = rtd->platform;
1347 int ret;
1348
1349 /* probe the CPU-side component, if it is a CODEC */
1350 if (cpu_dai->codec &&
1351 !cpu_dai->codec->probed &&
1352 cpu_dai->codec->driver->probe_order == order) {
1353 ret = soc_probe_codec(card, cpu_dai->codec);
1354 if (ret < 0)
1355 return ret;
1356 }
1357
1358 /* probe the CODEC-side component */
1359 if (!codec_dai->codec->probed &&
1360 codec_dai->codec->driver->probe_order == order) {
1361 ret = soc_probe_codec(card, codec_dai->codec);
1362 if (ret < 0)
1363 return ret;
1364 }
1365
1366 /* probe the platform */
1367 if (!platform->probed &&
1368 platform->driver->probe_order == order) {
1369 ret = soc_probe_platform(card, platform);
1370 if (ret < 0)
1371 return ret;
1372 }
1373
1374 return 0;
1375}
1376
1377static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001378{
1379 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1380 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1381 struct snd_soc_codec *codec = rtd->codec;
1382 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001383 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1384 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1385 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001386 int ret;
1387
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001388 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001389 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001390
1391 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001393 codec_dai->card = card;
1394 cpu_dai->card = card;
1395
1396 /* set default power off timeout */
1397 rtd->pmdown_time = pmdown_time;
1398
1399 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001400 if (!cpu_dai->probed &&
1401 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001402 if (!cpu_dai->codec) {
1403 cpu_dai->dapm.card = card;
1404 if (!try_module_get(cpu_dai->dev->driver->owner))
1405 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001406
Stephen Warren18d75642012-06-08 12:34:21 -06001407 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001408 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001409
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001410 if (cpu_dai->driver->probe) {
1411 ret = cpu_dai->driver->probe(cpu_dai);
1412 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001413 dev_err(cpu_dai->dev,
1414 "ASoC: failed to probe CPU DAI %s: %d\n",
1415 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001416 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001417 return ret;
1418 }
1419 }
1420 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001421 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001422 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1423 }
1424
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001425 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001426 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001427 if (codec_dai->driver->probe) {
1428 ret = codec_dai->driver->probe(codec_dai);
1429 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001430 dev_err(codec_dai->dev,
1431 "ASoC: failed to probe CODEC DAI %s: %d\n",
1432 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001433 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001434 }
1435 }
1436
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001437 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001438 codec_dai->probed = 1;
1439 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001440 }
1441
Liam Girdwood0168bf02011-06-07 16:08:05 +01001442 /* complete DAI probe during last probe */
1443 if (order != SND_SOC_COMP_ORDER_LAST)
1444 return 0;
1445
Jarkko Nikula589c3562010-12-06 16:27:07 +02001446 ret = soc_post_component_init(card, codec, num, 0);
1447 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001448 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001449
Mark Brown36ae1a92012-01-06 17:12:45 -08001450 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001451 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001452 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1453 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001454
Namarta Kohli1245b702012-08-16 17:10:41 +05301455 if (cpu_dai->driver->compress_dai) {
1456 /*create compress_device"*/
1457 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001458 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001459 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301460 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001461 return ret;
1462 }
1463 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301464
1465 if (!dai_link->params) {
1466 /* create the pcm */
1467 ret = soc_new_pcm(rtd, num);
1468 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001469 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301470 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001471 return ret;
1472 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301473 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001474 INIT_DELAYED_WORK(&rtd->delayed_work,
1475 codec2codec_close_delayed_work);
1476
Namarta Kohli1245b702012-08-16 17:10:41 +05301477 /* link the DAI widgets */
1478 play_w = codec_dai->playback_widget;
1479 capture_w = cpu_dai->capture_widget;
1480 if (play_w && capture_w) {
1481 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001482 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301483 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001484 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301485 play_w->name, capture_w->name, ret);
1486 return ret;
1487 }
1488 }
1489
1490 play_w = cpu_dai->playback_widget;
1491 capture_w = codec_dai->capture_widget;
1492 if (play_w && capture_w) {
1493 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1494 capture_w, play_w);
1495 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001496 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301497 play_w->name, capture_w->name, ret);
1498 return ret;
1499 }
Mark Brownc74184e2012-04-04 22:12:09 +01001500 }
1501 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001502 }
1503
1504 /* add platform data for AC97 devices */
1505 if (rtd->codec_dai->driver->ac97_control)
1506 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1507
1508 return 0;
1509}
1510
1511#ifdef CONFIG_SND_SOC_AC97_BUS
1512static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1513{
1514 int ret;
1515
1516 /* Only instantiate AC97 if not already done by the adaptor
1517 * for the generic AC97 subsystem.
1518 */
1519 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001520 /*
1521 * It is possible that the AC97 device is already registered to
1522 * the device subsystem. This happens when the device is created
1523 * via snd_ac97_mixer(). Currently only SoC codec that does so
1524 * is the generic AC97 glue but others migh emerge.
1525 *
1526 * In those cases we don't try to register the device again.
1527 */
1528 if (!rtd->codec->ac97_created)
1529 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001530
1531 ret = soc_ac97_dev_register(rtd->codec);
1532 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001533 dev_err(rtd->codec->dev,
1534 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001535 return ret;
1536 }
1537
1538 rtd->codec->ac97_registered = 1;
1539 }
1540 return 0;
1541}
1542
1543static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1544{
1545 if (codec->ac97_registered) {
1546 soc_ac97_dev_unregister(codec);
1547 codec->ac97_registered = 0;
1548 }
1549}
1550#endif
1551
Mark Brownb19e6e72012-03-14 21:18:39 +00001552static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1553{
1554 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1555 struct snd_soc_codec *codec;
1556
1557 /* find CODEC from registered CODECs*/
1558 list_for_each_entry(codec, &codec_list, list) {
1559 if (!strcmp(codec->name, aux_dev->codec_name))
1560 return 0;
1561 }
1562
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001563 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001564
Mark Brownb19e6e72012-03-14 21:18:39 +00001565 return -EPROBE_DEFER;
1566}
1567
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001568static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1569{
1570 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001571 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001572 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001573
1574 /* find CODEC from registered CODECs*/
1575 list_for_each_entry(codec, &codec_list, list) {
1576 if (!strcmp(codec->name, aux_dev->codec_name)) {
1577 if (codec->probed) {
1578 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001579 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001580 ret = -EBUSY;
1581 goto out;
1582 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001583 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001584 }
1585 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001586 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001587 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001588 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001589
Jarkko Nikula676ad982010-12-03 09:18:22 +02001590found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001591 ret = soc_probe_codec(card, codec);
1592 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001593 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001594
Jarkko Nikula589c3562010-12-06 16:27:07 +02001595 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001596
1597out:
1598 return ret;
1599}
1600
1601static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1602{
1603 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1604 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001605
1606 /* unregister the rtd device */
1607 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001608 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001609 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001610 rtd->dev_registered = 0;
1611 }
1612
Jarkko Nikula589c3562010-12-06 16:27:07 +02001613 if (codec && codec->probed)
1614 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001615}
1616
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001617static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001618{
1619 int ret;
1620
1621 if (codec->cache_init)
1622 return 0;
1623
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001624 ret = snd_soc_cache_init(codec);
1625 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001626 dev_err(codec->dev,
1627 "ASoC: Failed to set cache compression type: %d\n",
1628 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001629 return ret;
1630 }
1631 codec->cache_init = 1;
1632 return 0;
1633}
1634
Mark Brownb19e6e72012-03-14 21:18:39 +00001635static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001636{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001637 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001638 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001639 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001640
Liam Girdwood01b9d992012-03-07 10:38:25 +00001641 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001642
Mark Brownb19e6e72012-03-14 21:18:39 +00001643 /* bind DAIs */
1644 for (i = 0; i < card->num_links; i++) {
1645 ret = soc_bind_dai_link(card, i);
1646 if (ret != 0)
1647 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001648 }
1649
Mark Brownb19e6e72012-03-14 21:18:39 +00001650 /* check aux_devs too */
1651 for (i = 0; i < card->num_aux_devs; i++) {
1652 ret = soc_check_aux_dev(card, i);
1653 if (ret != 0)
1654 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655 }
1656
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001657 /* initialize the register cache for each available codec */
1658 list_for_each_entry(codec, &codec_list, list) {
1659 if (codec->cache_init)
1660 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001661 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001662 if (ret < 0)
1663 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001664 }
1665
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001666 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001667 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001668 card->owner, 0, &card->snd_card);
1669 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001670 dev_err(card->dev,
1671 "ASoC: can't create sound card for card %s: %d\n",
1672 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001673 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001675
Mark Browne37a4972011-03-02 18:21:57 +00001676 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1677 card->dapm.dev = card->dev;
1678 card->dapm.card = card;
1679 list_add(&card->dapm.list, &card->dapm_list);
1680
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001681#ifdef CONFIG_DEBUG_FS
1682 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1683#endif
1684
Mark Brown88ee1c62011-02-02 10:43:26 +00001685#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001686 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001687 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001688#endif
Andy Green6ed25972008-06-13 16:24:05 +01001689
Mark Brown9a841eb2011-04-12 17:51:37 -07001690 if (card->dapm_widgets)
1691 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1692 card->num_dapm_widgets);
1693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 /* initialise the sound card only once */
1695 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001696 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001697 if (ret < 0)
1698 goto card_probe_error;
1699 }
1700
Stephen Warren62ae68f2012-06-08 12:34:23 -06001701 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001702 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1703 order++) {
1704 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001705 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001706 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001707 dev_err(card->dev,
1708 "ASoC: failed to instantiate card %d\n",
1709 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001710 goto probe_dai_err;
1711 }
1712 }
1713 }
1714
1715 /* probe all DAI links on this card */
1716 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1717 order++) {
1718 for (i = 0; i < card->num_links; i++) {
1719 ret = soc_probe_link_dais(card, i, order);
1720 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001721 dev_err(card->dev,
1722 "ASoC: failed to instantiate card %d\n",
1723 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001724 goto probe_dai_err;
1725 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001726 }
1727 }
1728
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001729 for (i = 0; i < card->num_aux_devs; i++) {
1730 ret = soc_probe_aux_dev(card, i);
1731 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001732 dev_err(card->dev,
1733 "ASoC: failed to add auxiliary devices %d\n",
1734 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001735 goto probe_aux_dev_err;
1736 }
1737 }
1738
Mark Brown888df392012-02-16 19:37:51 -08001739 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001740 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001741
Mark Brownb7af1da2011-04-07 19:18:44 +09001742 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001743 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001744
Mark Brownb8ad29d2011-03-02 18:35:51 +00001745 if (card->dapm_routes)
1746 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1747 card->num_dapm_routes);
1748
Mark Brown75d9ac42011-09-27 16:41:01 +01001749 for (i = 0; i < card->num_links; i++) {
1750 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001751 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001752
Mark Brownf04209a2012-04-09 16:29:21 +01001753 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001754 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001755 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001756 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001757 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001758 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001759 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001760 }
1761
1762 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001763 if (dai_fmt &&
1764 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001765 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1766 dai_fmt);
1767 if (ret != 0 && ret != -ENOTSUPP)
1768 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001769 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001770 ret);
1771 } else if (dai_fmt) {
1772 /* Flip the polarity for the "CPU" end */
1773 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1774 switch (dai_link->dai_fmt &
1775 SND_SOC_DAIFMT_MASTER_MASK) {
1776 case SND_SOC_DAIFMT_CBM_CFM:
1777 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1778 break;
1779 case SND_SOC_DAIFMT_CBM_CFS:
1780 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1781 break;
1782 case SND_SOC_DAIFMT_CBS_CFM:
1783 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1784 break;
1785 case SND_SOC_DAIFMT_CBS_CFS:
1786 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1787 break;
1788 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001789
1790 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001791 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001792 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001793 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001794 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001795 ret);
1796 }
1797 }
1798
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001800 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001801 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1802 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001803 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1804 "%s", card->driver_name ? card->driver_name : card->name);
1805 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1806 switch (card->snd_card->driver[i]) {
1807 case '_':
1808 case '-':
1809 case '\0':
1810 break;
1811 default:
1812 if (!isalnum(card->snd_card->driver[i]))
1813 card->snd_card->driver[i] = '_';
1814 break;
1815 }
1816 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001817
Mark Brown28e9ad92011-03-02 18:36:34 +00001818 if (card->late_probe) {
1819 ret = card->late_probe(card);
1820 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001821 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001822 card->name, ret);
1823 goto probe_aux_dev_err;
1824 }
1825 }
1826
Stephen Warren16332812011-11-23 12:42:04 -07001827 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001828 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001829 snd_soc_dapm_auto_nc_codec_pins(codec);
1830
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001831 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001832
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001833 ret = snd_card_register(card->snd_card);
1834 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001835 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1836 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001837 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001838 }
1839
1840#ifdef CONFIG_SND_SOC_AC97_BUS
1841 /* register any AC97 codecs */
1842 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001843 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1844 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001845 dev_err(card->dev,
1846 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001847 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001848 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001849 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001850 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001851 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001852#endif
1853
Mark Brown435c5e22008-12-04 15:32:53 +00001854 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001855 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001856 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001857
1858 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001859
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001860probe_aux_dev_err:
1861 for (i = 0; i < card->num_aux_devs; i++)
1862 soc_remove_aux_dev(card, i);
1863
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001864probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001865 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001866
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001867card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001868 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001869 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001870
1871 snd_card_free(card->snd_card);
1872
Mark Brownb19e6e72012-03-14 21:18:39 +00001873base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001874 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001875
Mark Brownb19e6e72012-03-14 21:18:39 +00001876 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001877}
1878
1879/* probes a new socdev */
1880static int soc_probe(struct platform_device *pdev)
1881{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001882 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001883
Vinod Koul70a7ca32011-01-14 19:22:48 +05301884 /*
1885 * no card, so machine driver should be registering card
1886 * we should not be here in that case so ret error
1887 */
1888 if (!card)
1889 return -EINVAL;
1890
Mark Brownfe4085e2012-03-02 13:07:41 +00001891 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001892 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001893 card->name);
1894
Mark Brown435c5e22008-12-04 15:32:53 +00001895 /* Bodge while we unpick instantiation */
1896 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001897
Mark Brown28d528c2012-08-09 18:45:23 +01001898 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001899}
1900
Vinod Koulb0e26482011-01-13 22:48:02 +05301901static int soc_cleanup_card_resources(struct snd_soc_card *card)
1902{
Vinod Koulb0e26482011-01-13 22:48:02 +05301903 int i;
1904
1905 /* make sure any delayed work runs */
1906 for (i = 0; i < card->num_rtd; i++) {
1907 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001908 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301909 }
1910
1911 /* remove auxiliary devices */
1912 for (i = 0; i < card->num_aux_devs; i++)
1913 soc_remove_aux_dev(card, i);
1914
1915 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001916 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301917
1918 soc_cleanup_card_debugfs(card);
1919
1920 /* remove the card */
1921 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001922 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301923
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001924 snd_soc_dapm_free(&card->dapm);
1925
Vinod Koulb0e26482011-01-13 22:48:02 +05301926 snd_card_free(card->snd_card);
1927 return 0;
1928
1929}
1930
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001931/* removes a socdev */
1932static int soc_remove(struct platform_device *pdev)
1933{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001934 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001935
Mark Brownc5af3a22008-11-28 13:29:45 +00001936 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001937 return 0;
1938}
1939
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001940int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001941{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001942 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001943 int i;
Mark Brown51737472009-06-22 13:16:51 +01001944
1945 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001946 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001947
1948 /* Flush out pmdown_time work - we actually do want to run it
1949 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001950 for (i = 0; i < card->num_rtd; i++) {
1951 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001952 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001953 }
Mark Brown51737472009-06-22 13:16:51 +01001954
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001955 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001956
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001957 /* deactivate pins to sleep state */
1958 for (i = 0; i < card->num_rtd; i++) {
1959 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1960 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1961 pinctrl_pm_select_sleep_state(codec_dai->dev);
1962 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1963 }
1964
Mark Brown416356f2009-06-30 19:05:15 +01001965 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001966}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001967EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001968
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001969const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301970 .suspend = snd_soc_suspend,
1971 .resume = snd_soc_resume,
1972 .freeze = snd_soc_suspend,
1973 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001974 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301975 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001976};
Stephen Warrendeb26072011-04-05 19:35:30 -06001977EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001978
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001979/* ASoC platform driver */
1980static struct platform_driver soc_driver = {
1981 .driver = {
1982 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001983 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001984 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001985 },
1986 .probe = soc_probe,
1987 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001988};
1989
Mark Brown096e49d2009-07-05 15:12:22 +01001990/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001991 * snd_soc_new_ac97_codec - initailise AC97 device
1992 * @codec: audio codec
1993 * @ops: AC97 bus operations
1994 * @num: AC97 codec number
1995 *
1996 * Initialises AC97 codec resources for use by ad-hoc devices only.
1997 */
1998int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1999 struct snd_ac97_bus_ops *ops, int num)
2000{
2001 mutex_lock(&codec->mutex);
2002
2003 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2004 if (codec->ac97 == NULL) {
2005 mutex_unlock(&codec->mutex);
2006 return -ENOMEM;
2007 }
2008
2009 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2010 if (codec->ac97->bus == NULL) {
2011 kfree(codec->ac97);
2012 codec->ac97 = NULL;
2013 mutex_unlock(&codec->mutex);
2014 return -ENOMEM;
2015 }
2016
2017 codec->ac97->bus->ops = ops;
2018 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002019
2020 /*
2021 * Mark the AC97 device to be created by us. This way we ensure that the
2022 * device will be registered with the device subsystem later on.
2023 */
2024 codec->ac97_created = 1;
2025
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002026 mutex_unlock(&codec->mutex);
2027 return 0;
2028}
2029EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2030
Markus Pargmann741a5092013-08-19 17:05:55 +02002031static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2032
2033static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2034{
2035 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2036
2037 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2038
2039 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2040
2041 udelay(10);
2042
2043 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2044
2045 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2046 msleep(2);
2047}
2048
2049static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2050{
2051 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2052
2053 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2054
2055 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2056 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2057 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2058
2059 udelay(10);
2060
2061 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2062
2063 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2064 msleep(2);
2065}
2066
2067static int snd_soc_ac97_parse_pinctl(struct device *dev,
2068 struct snd_ac97_reset_cfg *cfg)
2069{
2070 struct pinctrl *p;
2071 struct pinctrl_state *state;
2072 int gpio;
2073 int ret;
2074
2075 p = devm_pinctrl_get(dev);
2076 if (IS_ERR(p)) {
2077 dev_err(dev, "Failed to get pinctrl\n");
2078 return PTR_RET(p);
2079 }
2080 cfg->pctl = p;
2081
2082 state = pinctrl_lookup_state(p, "ac97-reset");
2083 if (IS_ERR(state)) {
2084 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2085 return PTR_RET(state);
2086 }
2087 cfg->pstate_reset = state;
2088
2089 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2090 if (IS_ERR(state)) {
2091 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2092 return PTR_RET(state);
2093 }
2094 cfg->pstate_warm_reset = state;
2095
2096 state = pinctrl_lookup_state(p, "ac97-running");
2097 if (IS_ERR(state)) {
2098 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2099 return PTR_RET(state);
2100 }
2101 cfg->pstate_run = state;
2102
2103 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2104 if (gpio < 0) {
2105 dev_err(dev, "Can't find ac97-sync gpio\n");
2106 return gpio;
2107 }
2108 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2109 if (ret) {
2110 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2111 return ret;
2112 }
2113 cfg->gpio_sync = gpio;
2114
2115 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2116 if (gpio < 0) {
2117 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2118 return gpio;
2119 }
2120 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2121 if (ret) {
2122 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2123 return ret;
2124 }
2125 cfg->gpio_sdata = gpio;
2126
2127 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2128 if (gpio < 0) {
2129 dev_err(dev, "Can't find ac97-reset gpio\n");
2130 return gpio;
2131 }
2132 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2133 if (ret) {
2134 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2135 return ret;
2136 }
2137 cfg->gpio_reset = gpio;
2138
2139 return 0;
2140}
2141
Mark Brownb047e1c2013-06-26 12:45:59 +01002142struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002143EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002144
2145int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2146{
2147 if (ops == soc_ac97_ops)
2148 return 0;
2149
2150 if (soc_ac97_ops && ops)
2151 return -EBUSY;
2152
2153 soc_ac97_ops = ops;
2154
2155 return 0;
2156}
2157EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2158
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002159/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002160 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2161 *
2162 * This function sets the reset and warm_reset properties of ops and parses
2163 * the device node of pdev to get pinctrl states and gpio numbers to use.
2164 */
2165int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2166 struct platform_device *pdev)
2167{
2168 struct device *dev = &pdev->dev;
2169 struct snd_ac97_reset_cfg cfg;
2170 int ret;
2171
2172 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2173 if (ret)
2174 return ret;
2175
2176 ret = snd_soc_set_ac97_ops(ops);
2177 if (ret)
2178 return ret;
2179
2180 ops->warm_reset = snd_soc_ac97_warm_reset;
2181 ops->reset = snd_soc_ac97_reset;
2182
2183 snd_ac97_rst_cfg = cfg;
2184 return 0;
2185}
2186EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2187
2188/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002189 * snd_soc_free_ac97_codec - free AC97 codec device
2190 * @codec: audio codec
2191 *
2192 * Frees AC97 codec device resources.
2193 */
2194void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2195{
2196 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002197#ifdef CONFIG_SND_SOC_AC97_BUS
2198 soc_unregister_ac97_dai_link(codec);
2199#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002200 kfree(codec->ac97->bus);
2201 kfree(codec->ac97);
2202 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002203 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002204 mutex_unlock(&codec->mutex);
2205}
2206EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2207
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002208/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 * snd_soc_cnew - create new control
2210 * @_template: control template
2211 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002212 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002213 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002214 *
2215 * Create a new mixer control from a template control.
2216 *
2217 * Returns 0 for success, else error.
2218 */
2219struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002220 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002221 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222{
2223 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002224 struct snd_kcontrol *kcontrol;
2225 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226
2227 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002228 template.index = 0;
2229
Mark Brownefb7ac32011-03-08 17:23:24 +00002230 if (!long_name)
2231 long_name = template.name;
2232
2233 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002234 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002235 if (!name)
2236 return NULL;
2237
Mark Brownefb7ac32011-03-08 17:23:24 +00002238 template.name = name;
2239 } else {
2240 template.name = long_name;
2241 }
2242
2243 kcontrol = snd_ctl_new1(&template, data);
2244
2245 kfree(name);
2246
2247 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002248}
2249EXPORT_SYMBOL_GPL(snd_soc_cnew);
2250
Liam Girdwood022658b2012-02-03 17:43:09 +00002251static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2252 const struct snd_kcontrol_new *controls, int num_controls,
2253 const char *prefix, void *data)
2254{
2255 int err, i;
2256
2257 for (i = 0; i < num_controls; i++) {
2258 const struct snd_kcontrol_new *control = &controls[i];
2259 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2260 control->name, prefix));
2261 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002262 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2263 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002264 return err;
2265 }
2266 }
2267
2268 return 0;
2269}
2270
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002271struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2272 const char *name)
2273{
2274 struct snd_card *card = soc_card->snd_card;
2275 struct snd_kcontrol *kctl;
2276
2277 if (unlikely(!name))
2278 return NULL;
2279
2280 list_for_each_entry(kctl, &card->controls, list)
2281 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2282 return kctl;
2283 return NULL;
2284}
2285EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2286
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002287/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002288 * snd_soc_add_codec_controls - add an array of controls to a codec.
2289 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002290 * duplicating this code.
2291 *
2292 * @codec: codec to add controls to
2293 * @controls: array of controls to add
2294 * @num_controls: number of elements in the array
2295 *
2296 * Return 0 for success, else error.
2297 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002298int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002299 const struct snd_kcontrol_new *controls, int num_controls)
2300{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002301 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002302
Liam Girdwood022658b2012-02-03 17:43:09 +00002303 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2304 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002305}
Liam Girdwood022658b2012-02-03 17:43:09 +00002306EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002307
2308/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002309 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002310 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002311 *
2312 * @platform: platform to add controls to
2313 * @controls: array of controls to add
2314 * @num_controls: number of elements in the array
2315 *
2316 * Return 0 for success, else error.
2317 */
2318int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2319 const struct snd_kcontrol_new *controls, int num_controls)
2320{
2321 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002322
Liam Girdwood022658b2012-02-03 17:43:09 +00002323 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2324 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002325}
2326EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2327
2328/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002329 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2330 * Convenience function to add a list of controls.
2331 *
2332 * @soc_card: SoC card to add controls to
2333 * @controls: array of controls to add
2334 * @num_controls: number of elements in the array
2335 *
2336 * Return 0 for success, else error.
2337 */
2338int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2339 const struct snd_kcontrol_new *controls, int num_controls)
2340{
2341 struct snd_card *card = soc_card->snd_card;
2342
2343 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2344 NULL, soc_card);
2345}
2346EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2347
2348/**
2349 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2350 * Convienience function to add a list of controls.
2351 *
2352 * @dai: DAI to add controls to
2353 * @controls: array of controls to add
2354 * @num_controls: number of elements in the array
2355 *
2356 * Return 0 for success, else error.
2357 */
2358int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2359 const struct snd_kcontrol_new *controls, int num_controls)
2360{
2361 struct snd_card *card = dai->card->snd_card;
2362
2363 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2364 NULL, dai);
2365}
2366EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2367
2368/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002369 * snd_soc_info_enum_double - enumerated double mixer info callback
2370 * @kcontrol: mixer control
2371 * @uinfo: control element information
2372 *
2373 * Callback to provide information about a double enumerated
2374 * mixer control.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2380{
2381 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2382
2383 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2384 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002385 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002386
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002387 if (uinfo->value.enumerated.item >= e->items)
2388 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002389 strlcpy(uinfo->value.enumerated.name,
2390 e->texts[uinfo->value.enumerated.item],
2391 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002392 return 0;
2393}
2394EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2395
2396/**
2397 * snd_soc_get_enum_double - enumerated double mixer get callback
2398 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002399 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400 *
2401 * Callback to get the value of a double enumerated mixer.
2402 *
2403 * Returns 0 for success.
2404 */
2405int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_value *ucontrol)
2407{
2408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2409 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002410 unsigned int val, item;
2411 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002412
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002413 reg_val = snd_soc_read(codec, e->reg);
2414 val = (reg_val >> e->shift_l) & e->mask;
2415 item = snd_soc_enum_val_to_item(e, val);
2416 ucontrol->value.enumerated.item[0] = item;
2417 if (e->shift_l != e->shift_r) {
2418 val = (reg_val >> e->shift_l) & e->mask;
2419 item = snd_soc_enum_val_to_item(e, val);
2420 ucontrol->value.enumerated.item[1] = item;
2421 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422
2423 return 0;
2424}
2425EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2426
2427/**
2428 * snd_soc_put_enum_double - enumerated double mixer put callback
2429 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002430 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002431 *
2432 * Callback to set the value of a double enumerated mixer.
2433 *
2434 * Returns 0 for success.
2435 */
2436int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_value *ucontrol)
2438{
2439 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2440 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002441 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002442 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002443 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002444
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002445 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002446 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002447 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002448 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002449 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002450 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002452 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002453 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002454 }
2455
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002456 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002457}
2458EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2459
2460/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002461 * snd_soc_read_signed - Read a codec register and interprete as signed value
2462 * @codec: codec
2463 * @reg: Register to read
2464 * @mask: Mask to use after shifting the register value
2465 * @shift: Right shift of register value
2466 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002467 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002468 * This functions reads a codec register. The register value is shifted right
2469 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2470 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002471 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002472 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002473 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002474static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2475 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002476{
Markus Pargmannf227b882014-01-16 16:02:10 +01002477 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002478 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002479
Markus Pargmannf227b882014-01-16 16:02:10 +01002480 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002481
Markus Pargmannf227b882014-01-16 16:02:10 +01002482 if (!sign_bit)
2483 return val;
2484
2485 /* non-negative number */
2486 if (!(val & BIT(sign_bit)))
2487 return val;
2488
2489 ret = val;
2490
2491 /*
2492 * The register most probably does not contain a full-sized int.
2493 * Instead we have an arbitrary number of bits in a signed
2494 * representation which has to be translated into a full-sized int.
2495 * This is done by filling up all bits above the sign-bit.
2496 */
2497 ret |= ~((int)(BIT(sign_bit) - 1));
2498
2499 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002500}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002501
2502/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 * snd_soc_info_volsw - single mixer info callback
2504 * @kcontrol: mixer control
2505 * @uinfo: control element information
2506 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002507 * Callback to provide information about a single mixer control, or a double
2508 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002509 *
2510 * Returns 0 for success.
2511 */
2512int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_info *uinfo)
2514{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002517 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002519 if (!mc->platform_max)
2520 mc->platform_max = mc->max;
2521 platform_max = mc->platform_max;
2522
2523 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002524 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2525 else
2526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2527
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002528 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002530 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531 return 0;
2532}
2533EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2534
2535/**
2536 * snd_soc_get_volsw - single mixer get callback
2537 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002538 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002539 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002540 * Callback to get the value of a single mixer control, or a double mixer
2541 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002542 *
2543 * Returns 0 for success.
2544 */
2545int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2546 struct snd_ctl_elem_value *ucontrol)
2547{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002548 struct soc_mixer_control *mc =
2549 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002550 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002551 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002552 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002553 unsigned int shift = mc->shift;
2554 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002555 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002556 int min = mc->min;
2557 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002558 unsigned int mask = (1 << fls(max)) - 1;
2559 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002560
Markus Pargmannf227b882014-01-16 16:02:10 +01002561 if (sign_bit)
2562 mask = BIT(sign_bit + 1) - 1;
2563
2564 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2565 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002566 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002567 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002568 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002569
2570 if (snd_soc_volsw_is_stereo(mc)) {
2571 if (reg == reg2)
2572 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002573 snd_soc_read_signed(codec, reg, mask, rshift,
2574 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002575 else
2576 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002577 snd_soc_read_signed(codec, reg2, mask, shift,
2578 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002579 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002580 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002581 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002582 }
2583
2584 return 0;
2585}
2586EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2587
2588/**
2589 * snd_soc_put_volsw - single mixer put callback
2590 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002591 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002592 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002593 * Callback to set the value of a single mixer control, or a double mixer
2594 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002595 *
2596 * Returns 0 for success.
2597 */
2598int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2599 struct snd_ctl_elem_value *ucontrol)
2600{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002601 struct soc_mixer_control *mc =
2602 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002603 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002604 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002605 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002606 unsigned int shift = mc->shift;
2607 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002608 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002609 int min = mc->min;
2610 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002611 unsigned int mask = (1 << fls(max)) - 1;
2612 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002613 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002614 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002615 unsigned int val2 = 0;
2616 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002617
Markus Pargmannf227b882014-01-16 16:02:10 +01002618 if (sign_bit)
2619 mask = BIT(sign_bit + 1) - 1;
2620
2621 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002622 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002623 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002624 val_mask = mask << shift;
2625 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002626 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002627 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002629 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002630 if (reg == reg2) {
2631 val_mask |= mask << rshift;
2632 val |= val2 << rshift;
2633 } else {
2634 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002635 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002636 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002637 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002638 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002639 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002640 return err;
2641
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002642 if (type_2r)
2643 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2644
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002645 return err;
2646}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002647EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648
Mark Browne13ac2e2008-05-28 17:58:05 +01002649/**
Brian Austin1d99f242012-03-30 10:43:55 -05002650 * snd_soc_get_volsw_sx - single mixer get callback
2651 * @kcontrol: mixer control
2652 * @ucontrol: control element information
2653 *
2654 * Callback to get the value of a single mixer control, or a double mixer
2655 * control that spans 2 registers.
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_value *ucontrol)
2661{
2662 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2663 struct soc_mixer_control *mc =
2664 (struct soc_mixer_control *)kcontrol->private_value;
2665
2666 unsigned int reg = mc->reg;
2667 unsigned int reg2 = mc->rreg;
2668 unsigned int shift = mc->shift;
2669 unsigned int rshift = mc->rshift;
2670 int max = mc->max;
2671 int min = mc->min;
2672 int mask = (1 << (fls(min + max) - 1)) - 1;
2673
2674 ucontrol->value.integer.value[0] =
2675 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2676
2677 if (snd_soc_volsw_is_stereo(mc))
2678 ucontrol->value.integer.value[1] =
2679 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2680
2681 return 0;
2682}
2683EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2684
2685/**
2686 * snd_soc_put_volsw_sx - double mixer set callback
2687 * @kcontrol: mixer control
2688 * @uinfo: control element information
2689 *
2690 * Callback to set the value of a double mixer control that spans 2 registers.
2691 *
2692 * Returns 0 for success.
2693 */
2694int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2695 struct snd_ctl_elem_value *ucontrol)
2696{
2697 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2698 struct soc_mixer_control *mc =
2699 (struct soc_mixer_control *)kcontrol->private_value;
2700
2701 unsigned int reg = mc->reg;
2702 unsigned int reg2 = mc->rreg;
2703 unsigned int shift = mc->shift;
2704 unsigned int rshift = mc->rshift;
2705 int max = mc->max;
2706 int min = mc->min;
2707 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002708 int err = 0;
Bard Liao1a390192014-04-08 11:18:10 +08002709 unsigned int val, val_mask, val2 = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002710
2711 val_mask = mask << shift;
2712 val = (ucontrol->value.integer.value[0] + min) & mask;
2713 val = val << shift;
2714
Mukund Navadad0558522012-11-09 11:53:40 +05302715 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2716 if (err < 0)
2717 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002718
2719 if (snd_soc_volsw_is_stereo(mc)) {
2720 val_mask = mask << rshift;
2721 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2722 val2 = val2 << rshift;
2723
2724 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2725 return err;
2726 }
2727 return 0;
2728}
2729EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2730
2731/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002732 * snd_soc_info_volsw_s8 - signed mixer info callback
2733 * @kcontrol: mixer control
2734 * @uinfo: control element information
2735 *
2736 * Callback to provide information about a signed mixer control.
2737 *
2738 * Returns 0 for success.
2739 */
2740int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2741 struct snd_ctl_elem_info *uinfo)
2742{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002743 struct soc_mixer_control *mc =
2744 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002745 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002746 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002747
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002748 if (!mc->platform_max)
2749 mc->platform_max = mc->max;
2750 platform_max = mc->platform_max;
2751
Mark Browne13ac2e2008-05-28 17:58:05 +01002752 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2753 uinfo->count = 2;
2754 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002755 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002756 return 0;
2757}
2758EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2759
2760/**
2761 * snd_soc_get_volsw_s8 - signed mixer get callback
2762 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002763 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002764 *
2765 * Callback to get the value of a signed mixer control.
2766 *
2767 * Returns 0 for success.
2768 */
2769int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2770 struct snd_ctl_elem_value *ucontrol)
2771{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002772 struct soc_mixer_control *mc =
2773 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002774 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002775 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002776 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002777 int val = snd_soc_read(codec, reg);
2778
2779 ucontrol->value.integer.value[0] =
2780 ((signed char)(val & 0xff))-min;
2781 ucontrol->value.integer.value[1] =
2782 ((signed char)((val >> 8) & 0xff))-min;
2783 return 0;
2784}
2785EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2786
2787/**
2788 * snd_soc_put_volsw_sgn - signed mixer put callback
2789 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002790 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002791 *
2792 * Callback to set the value of a signed mixer control.
2793 *
2794 * Returns 0 for success.
2795 */
2796int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2797 struct snd_ctl_elem_value *ucontrol)
2798{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002799 struct soc_mixer_control *mc =
2800 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002801 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002802 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002803 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002804 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002805
2806 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2807 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2808
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002809 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002810}
2811EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2812
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002813/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002814 * snd_soc_info_volsw_range - single mixer info callback with range.
2815 * @kcontrol: mixer control
2816 * @uinfo: control element information
2817 *
2818 * Callback to provide information, within a range, about a single
2819 * mixer control.
2820 *
2821 * returns 0 for success.
2822 */
2823int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
2824 struct snd_ctl_elem_info *uinfo)
2825{
2826 struct soc_mixer_control *mc =
2827 (struct soc_mixer_control *)kcontrol->private_value;
2828 int platform_max;
2829 int min = mc->min;
2830
2831 if (!mc->platform_max)
2832 mc->platform_max = mc->max;
2833 platform_max = mc->platform_max;
2834
2835 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00002836 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002837 uinfo->value.integer.min = 0;
2838 uinfo->value.integer.max = platform_max - min;
2839
2840 return 0;
2841}
2842EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
2843
2844/**
2845 * snd_soc_put_volsw_range - single mixer put value callback with range.
2846 * @kcontrol: mixer control
2847 * @ucontrol: control element information
2848 *
2849 * Callback to set the value, within a range, for a single mixer control.
2850 *
2851 * Returns 0 for success.
2852 */
2853int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
2854 struct snd_ctl_elem_value *ucontrol)
2855{
2856 struct soc_mixer_control *mc =
2857 (struct soc_mixer_control *)kcontrol->private_value;
2858 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2859 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002860 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002861 unsigned int shift = mc->shift;
2862 int min = mc->min;
2863 int max = mc->max;
2864 unsigned int mask = (1 << fls(max)) - 1;
2865 unsigned int invert = mc->invert;
2866 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00002867 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002868
2869 val = ((ucontrol->value.integer.value[0] + min) & mask);
2870 if (invert)
2871 val = max - val;
2872 val_mask = mask << shift;
2873 val = val << shift;
2874
Mark Brown9bde4f02012-12-19 16:05:00 +00002875 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09002876 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00002877 return ret;
2878
2879 if (snd_soc_volsw_is_stereo(mc)) {
2880 val = ((ucontrol->value.integer.value[1] + min) & mask);
2881 if (invert)
2882 val = max - val;
2883 val_mask = mask << shift;
2884 val = val << shift;
2885
2886 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
2887 }
2888
2889 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002890}
2891EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
2892
2893/**
2894 * snd_soc_get_volsw_range - single mixer get callback with range
2895 * @kcontrol: mixer control
2896 * @ucontrol: control element information
2897 *
2898 * Callback to get the value, within a range, of a single mixer control.
2899 *
2900 * Returns 0 for success.
2901 */
2902int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
2903 struct snd_ctl_elem_value *ucontrol)
2904{
2905 struct soc_mixer_control *mc =
2906 (struct soc_mixer_control *)kcontrol->private_value;
2907 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2908 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00002909 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002910 unsigned int shift = mc->shift;
2911 int min = mc->min;
2912 int max = mc->max;
2913 unsigned int mask = (1 << fls(max)) - 1;
2914 unsigned int invert = mc->invert;
2915
2916 ucontrol->value.integer.value[0] =
2917 (snd_soc_read(codec, reg) >> shift) & mask;
2918 if (invert)
2919 ucontrol->value.integer.value[0] =
2920 max - ucontrol->value.integer.value[0];
2921 ucontrol->value.integer.value[0] =
2922 ucontrol->value.integer.value[0] - min;
2923
Mark Brown9bde4f02012-12-19 16:05:00 +00002924 if (snd_soc_volsw_is_stereo(mc)) {
2925 ucontrol->value.integer.value[1] =
2926 (snd_soc_read(codec, rreg) >> shift) & mask;
2927 if (invert)
2928 ucontrol->value.integer.value[1] =
2929 max - ucontrol->value.integer.value[1];
2930 ucontrol->value.integer.value[1] =
2931 ucontrol->value.integer.value[1] - min;
2932 }
2933
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01002934 return 0;
2935}
2936EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
2937
2938/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002939 * snd_soc_limit_volume - Set new limit to an existing volume control.
2940 *
2941 * @codec: where to look for the control
2942 * @name: Name of the control
2943 * @max: new maximum limit
2944 *
2945 * Return 0 for success, else error.
2946 */
2947int snd_soc_limit_volume(struct snd_soc_codec *codec,
2948 const char *name, int max)
2949{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002950 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002951 struct snd_kcontrol *kctl;
2952 struct soc_mixer_control *mc;
2953 int found = 0;
2954 int ret = -EINVAL;
2955
2956 /* Sanity check for name and max */
2957 if (unlikely(!name || max <= 0))
2958 return -EINVAL;
2959
2960 list_for_each_entry(kctl, &card->controls, list) {
2961 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2962 found = 1;
2963 break;
2964 }
2965 }
2966 if (found) {
2967 mc = (struct soc_mixer_control *)kctl->private_value;
2968 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002969 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002970 ret = 0;
2971 }
2972 }
2973 return ret;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2976
Mark Brown71d08512011-10-10 18:31:26 +01002977int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2978 struct snd_ctl_elem_info *uinfo)
2979{
2980 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2981 struct soc_bytes *params = (void *)kcontrol->private_value;
2982
2983 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2984 uinfo->count = params->num_regs * codec->val_bytes;
2985
2986 return 0;
2987}
2988EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2989
2990int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2991 struct snd_ctl_elem_value *ucontrol)
2992{
2993 struct soc_bytes *params = (void *)kcontrol->private_value;
2994 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2995 int ret;
2996
2997 if (codec->using_regmap)
2998 ret = regmap_raw_read(codec->control_data, params->base,
2999 ucontrol->value.bytes.data,
3000 params->num_regs * codec->val_bytes);
3001 else
3002 ret = -EINVAL;
3003
Mark Brownf831b052012-02-17 16:20:33 -08003004 /* Hide any masked bytes to ensure consistent data reporting */
3005 if (ret == 0 && params->mask) {
3006 switch (codec->val_bytes) {
3007 case 1:
3008 ucontrol->value.bytes.data[0] &= ~params->mask;
3009 break;
3010 case 2:
3011 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003012 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003013 break;
3014 case 4:
3015 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003016 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003017 break;
3018 default:
3019 return -EINVAL;
3020 }
3021 }
3022
Mark Brown71d08512011-10-10 18:31:26 +01003023 return ret;
3024}
3025EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3026
3027int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3028 struct snd_ctl_elem_value *ucontrol)
3029{
3030 struct soc_bytes *params = (void *)kcontrol->private_value;
3031 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003032 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003033 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003034 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003035
Mark Brownf831b052012-02-17 16:20:33 -08003036 if (!codec->using_regmap)
3037 return -EINVAL;
3038
Mark Brownf831b052012-02-17 16:20:33 -08003039 len = params->num_regs * codec->val_bytes;
3040
Mark Brownb5a8fe42013-01-20 21:42:22 +09003041 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3042 if (!data)
3043 return -ENOMEM;
3044
Mark Brownf831b052012-02-17 16:20:33 -08003045 /*
3046 * If we've got a mask then we need to preserve the register
3047 * bits. We shouldn't modify the incoming data so take a
3048 * copy.
3049 */
3050 if (params->mask) {
3051 ret = regmap_read(codec->control_data, params->base, &val);
3052 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003053 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003054
3055 val &= params->mask;
3056
Mark Brownf831b052012-02-17 16:20:33 -08003057 switch (codec->val_bytes) {
3058 case 1:
3059 ((u8 *)data)[0] &= ~params->mask;
3060 ((u8 *)data)[0] |= val;
3061 break;
3062 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003063 mask = ~params->mask;
3064 ret = regmap_parse_val(codec->control_data,
3065 &mask, &mask);
3066 if (ret != 0)
3067 goto out;
3068
3069 ((u16 *)data)[0] &= mask;
3070
3071 ret = regmap_parse_val(codec->control_data,
3072 &val, &val);
3073 if (ret != 0)
3074 goto out;
3075
3076 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003077 break;
3078 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003079 mask = ~params->mask;
3080 ret = regmap_parse_val(codec->control_data,
3081 &mask, &mask);
3082 if (ret != 0)
3083 goto out;
3084
3085 ((u32 *)data)[0] &= mask;
3086
3087 ret = regmap_parse_val(codec->control_data,
3088 &val, &val);
3089 if (ret != 0)
3090 goto out;
3091
3092 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003093 break;
3094 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003095 ret = -EINVAL;
3096 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003097 }
3098 }
3099
3100 ret = regmap_raw_write(codec->control_data, params->base,
3101 data, len);
3102
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003103out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003104 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003105
3106 return ret;
3107}
3108EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3109
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003110/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003111 * snd_soc_info_xr_sx - signed multi register info callback
3112 * @kcontrol: mreg control
3113 * @uinfo: control element information
3114 *
3115 * Callback to provide information of a control that can
3116 * span multiple codec registers which together
3117 * forms a single signed value in a MSB/LSB manner.
3118 *
3119 * Returns 0 for success.
3120 */
3121int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3122 struct snd_ctl_elem_info *uinfo)
3123{
3124 struct soc_mreg_control *mc =
3125 (struct soc_mreg_control *)kcontrol->private_value;
3126 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3127 uinfo->count = 1;
3128 uinfo->value.integer.min = mc->min;
3129 uinfo->value.integer.max = mc->max;
3130
3131 return 0;
3132}
3133EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3134
3135/**
3136 * snd_soc_get_xr_sx - signed multi register get callback
3137 * @kcontrol: mreg control
3138 * @ucontrol: control element information
3139 *
3140 * Callback to get the value of a control that can span
3141 * multiple codec registers which together forms a single
3142 * signed value in a MSB/LSB manner. The control supports
3143 * specifying total no of bits used to allow for bitfields
3144 * across the multiple codec registers.
3145 *
3146 * Returns 0 for success.
3147 */
3148int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3149 struct snd_ctl_elem_value *ucontrol)
3150{
3151 struct soc_mreg_control *mc =
3152 (struct soc_mreg_control *)kcontrol->private_value;
3153 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3154 unsigned int regbase = mc->regbase;
3155 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003156 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003157 unsigned int regwmask = (1<<regwshift)-1;
3158 unsigned int invert = mc->invert;
3159 unsigned long mask = (1UL<<mc->nbits)-1;
3160 long min = mc->min;
3161 long max = mc->max;
3162 long val = 0;
3163 unsigned long regval;
3164 unsigned int i;
3165
3166 for (i = 0; i < regcount; i++) {
3167 regval = snd_soc_read(codec, regbase+i) & regwmask;
3168 val |= regval << (regwshift*(regcount-i-1));
3169 }
3170 val &= mask;
3171 if (min < 0 && val > max)
3172 val |= ~mask;
3173 if (invert)
3174 val = max - val;
3175 ucontrol->value.integer.value[0] = val;
3176
3177 return 0;
3178}
3179EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3180
3181/**
3182 * snd_soc_put_xr_sx - signed multi register get callback
3183 * @kcontrol: mreg control
3184 * @ucontrol: control element information
3185 *
3186 * Callback to set the value of a control that can span
3187 * multiple codec registers which together forms a single
3188 * signed value in a MSB/LSB manner. The control supports
3189 * specifying total no of bits used to allow for bitfields
3190 * across the multiple codec registers.
3191 *
3192 * Returns 0 for success.
3193 */
3194int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3195 struct snd_ctl_elem_value *ucontrol)
3196{
3197 struct soc_mreg_control *mc =
3198 (struct soc_mreg_control *)kcontrol->private_value;
3199 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3200 unsigned int regbase = mc->regbase;
3201 unsigned int regcount = mc->regcount;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01003202 unsigned int regwshift = codec->val_bytes * BITS_PER_BYTE;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003203 unsigned int regwmask = (1<<regwshift)-1;
3204 unsigned int invert = mc->invert;
3205 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003206 long max = mc->max;
3207 long val = ucontrol->value.integer.value[0];
3208 unsigned int i, regval, regmask;
3209 int err;
3210
3211 if (invert)
3212 val = max - val;
3213 val &= mask;
3214 for (i = 0; i < regcount; i++) {
3215 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3216 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3217 err = snd_soc_update_bits_locked(codec, regbase+i,
3218 regmask, regval);
3219 if (err < 0)
3220 return err;
3221 }
3222
3223 return 0;
3224}
3225EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3226
3227/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003228 * snd_soc_get_strobe - strobe get callback
3229 * @kcontrol: mixer control
3230 * @ucontrol: control element information
3231 *
3232 * Callback get the value of a strobe mixer control.
3233 *
3234 * Returns 0 for success.
3235 */
3236int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3237 struct snd_ctl_elem_value *ucontrol)
3238{
3239 struct soc_mixer_control *mc =
3240 (struct soc_mixer_control *)kcontrol->private_value;
3241 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3242 unsigned int reg = mc->reg;
3243 unsigned int shift = mc->shift;
3244 unsigned int mask = 1 << shift;
3245 unsigned int invert = mc->invert != 0;
3246 unsigned int val = snd_soc_read(codec, reg) & mask;
3247
3248 if (shift != 0 && val != 0)
3249 val = val >> shift;
3250 ucontrol->value.enumerated.item[0] = val ^ invert;
3251
3252 return 0;
3253}
3254EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3255
3256/**
3257 * snd_soc_put_strobe - strobe put callback
3258 * @kcontrol: mixer control
3259 * @ucontrol: control element information
3260 *
3261 * Callback strobe a register bit to high then low (or the inverse)
3262 * in one pass of a single mixer enum control.
3263 *
3264 * Returns 1 for success.
3265 */
3266int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3267 struct snd_ctl_elem_value *ucontrol)
3268{
3269 struct soc_mixer_control *mc =
3270 (struct soc_mixer_control *)kcontrol->private_value;
3271 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3272 unsigned int reg = mc->reg;
3273 unsigned int shift = mc->shift;
3274 unsigned int mask = 1 << shift;
3275 unsigned int invert = mc->invert != 0;
3276 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3277 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3278 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3279 int err;
3280
3281 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3282 if (err < 0)
3283 return err;
3284
3285 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3286 return err;
3287}
3288EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3289
3290/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003291 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3292 * @dai: DAI
3293 * @clk_id: DAI specific clock ID
3294 * @freq: new clock frequency in Hz
3295 * @dir: new clock direction - input/output.
3296 *
3297 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3298 */
3299int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3300 unsigned int freq, int dir)
3301{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003302 if (dai->driver && dai->driver->ops->set_sysclk)
3303 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003304 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003305 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003306 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003307 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003308 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003309}
3310EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3311
3312/**
Mark Brownec4ee522011-03-07 20:58:11 +00003313 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3314 * @codec: CODEC
3315 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003316 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003317 * @freq: new clock frequency in Hz
3318 * @dir: new clock direction - input/output.
3319 *
3320 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3321 */
3322int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003323 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003324{
3325 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003326 return codec->driver->set_sysclk(codec, clk_id, source,
3327 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003328 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003329 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003330}
3331EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3332
3333/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003334 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3335 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003336 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003337 * @div: new clock divisor.
3338 *
3339 * Configures the clock dividers. This is used to derive the best DAI bit and
3340 * frame clocks from the system or master clock. It's best to set the DAI bit
3341 * and frame clocks as low as possible to save system power.
3342 */
3343int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3344 int div_id, int div)
3345{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003346 if (dai->driver && dai->driver->ops->set_clkdiv)
3347 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003348 else
3349 return -EINVAL;
3350}
3351EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3352
3353/**
3354 * snd_soc_dai_set_pll - configure DAI PLL.
3355 * @dai: DAI
3356 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003357 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003358 * @freq_in: PLL input clock frequency in Hz
3359 * @freq_out: requested PLL output clock frequency in Hz
3360 *
3361 * Configures and enables PLL to generate output clock based on input clock.
3362 */
Mark Brown85488032009-09-05 18:52:16 +01003363int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3364 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003365{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003366 if (dai->driver && dai->driver->ops->set_pll)
3367 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003368 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003369 else if (dai->codec && dai->codec->driver->set_pll)
3370 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3371 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003372 else
3373 return -EINVAL;
3374}
3375EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3376
Mark Brownec4ee522011-03-07 20:58:11 +00003377/*
3378 * snd_soc_codec_set_pll - configure codec PLL.
3379 * @codec: CODEC
3380 * @pll_id: DAI specific PLL ID
3381 * @source: DAI specific source for the PLL
3382 * @freq_in: PLL input clock frequency in Hz
3383 * @freq_out: requested PLL output clock frequency in Hz
3384 *
3385 * Configures and enables PLL to generate output clock based on input clock.
3386 */
3387int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3388 unsigned int freq_in, unsigned int freq_out)
3389{
3390 if (codec->driver->set_pll)
3391 return codec->driver->set_pll(codec, pll_id, source,
3392 freq_in, freq_out);
3393 else
3394 return -EINVAL;
3395}
3396EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3397
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003398/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003399 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3400 * @dai: DAI
3401 * @ratio Ratio of BCLK to Sample rate.
3402 *
3403 * Configures the DAI for a preset BCLK to sample rate ratio.
3404 */
3405int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3406{
3407 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3408 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3409 else
3410 return -EINVAL;
3411}
3412EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3413
3414/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003415 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3416 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003417 * @fmt: SND_SOC_DAIFMT_ format value.
3418 *
3419 * Configures the DAI hardware format and clocking.
3420 */
3421int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3422{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003423 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003424 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003425 if (dai->driver->ops->set_fmt == NULL)
3426 return -ENOTSUPP;
3427 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003428}
3429EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3430
3431/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003432 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003433 * @slots: Number of slots in use.
3434 * @tx_mask: bitmask representing active TX slots.
3435 * @rx_mask: bitmask representing active RX slots.
3436 *
3437 * Generates the TDM tx and rx slot default masks for DAI.
3438 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003439static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003440 unsigned int *tx_mask,
3441 unsigned int *rx_mask)
3442{
3443 if (*tx_mask || *rx_mask)
3444 return 0;
3445
3446 if (!slots)
3447 return -EINVAL;
3448
3449 *tx_mask = (1 << slots) - 1;
3450 *rx_mask = (1 << slots) - 1;
3451
3452 return 0;
3453}
3454
3455/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003456 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3457 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003458 * @tx_mask: bitmask representing active TX slots.
3459 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003460 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003461 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003462 *
3463 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3464 * specific.
3465 */
3466int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003467 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003468{
Xiubo Lie5c21512014-03-21 14:17:12 +08003469 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3470 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003471 &tx_mask, &rx_mask);
3472 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003473 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003474
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003475 if (dai->driver && dai->driver->ops->set_tdm_slot)
3476 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003477 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003478 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003479 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003480}
3481EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3482
3483/**
Barry Song472df3c2009-09-12 01:16:29 +08003484 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3485 * @dai: DAI
3486 * @tx_num: how many TX channels
3487 * @tx_slot: pointer to an array which imply the TX slot number channel
3488 * 0~num-1 uses
3489 * @rx_num: how many RX channels
3490 * @rx_slot: pointer to an array which imply the RX slot number channel
3491 * 0~num-1 uses
3492 *
3493 * configure the relationship between channel number and TDM slot number.
3494 */
3495int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3496 unsigned int tx_num, unsigned int *tx_slot,
3497 unsigned int rx_num, unsigned int *rx_slot)
3498{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003499 if (dai->driver && dai->driver->ops->set_channel_map)
3500 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003501 rx_num, rx_slot);
3502 else
3503 return -EINVAL;
3504}
3505EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3506
3507/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003508 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3509 * @dai: DAI
3510 * @tristate: tristate enable
3511 *
3512 * Tristates the DAI so that others can use it.
3513 */
3514int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3515{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003516 if (dai->driver && dai->driver->ops->set_tristate)
3517 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003518 else
3519 return -EINVAL;
3520}
3521EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3522
3523/**
3524 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3525 * @dai: DAI
3526 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003527 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003528 *
3529 * Mutes the DAI DAC.
3530 */
Mark Brownda183962013-02-06 15:44:07 +00003531int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3532 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003533{
Mark Brownda183962013-02-06 15:44:07 +00003534 if (!dai->driver)
3535 return -ENOTSUPP;
3536
3537 if (dai->driver->ops->mute_stream)
3538 return dai->driver->ops->mute_stream(dai, mute, direction);
3539 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3540 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003541 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003542 else
Mark Brown04570c62012-04-13 19:16:03 +01003543 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003544}
3545EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3546
Mark Brownc5af3a22008-11-28 13:29:45 +00003547/**
3548 * snd_soc_register_card - Register a card with the ASoC core
3549 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003550 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003551 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003552 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303553int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003554{
Mark Brownb19e6e72012-03-14 21:18:39 +00003555 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003556
Mark Brownc5af3a22008-11-28 13:29:45 +00003557 if (!card->name || !card->dev)
3558 return -EINVAL;
3559
Stephen Warren5a504962011-12-21 10:40:59 -07003560 for (i = 0; i < card->num_links; i++) {
3561 struct snd_soc_dai_link *link = &card->dai_link[i];
3562
3563 /*
3564 * Codec must be specified by 1 of name or OF node,
3565 * not both or neither.
3566 */
3567 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003568 dev_err(card->dev,
3569 "ASoC: Neither/both codec name/of_node are set for %s\n",
3570 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003571 return -EINVAL;
3572 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003573 /* Codec DAI name must be specified */
3574 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003575 dev_err(card->dev,
3576 "ASoC: codec_dai_name not set for %s\n",
3577 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003578 return -EINVAL;
3579 }
Stephen Warren5a504962011-12-21 10:40:59 -07003580
3581 /*
3582 * Platform may be specified by either name or OF node, but
3583 * can be left unspecified, and a dummy platform will be used.
3584 */
3585 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003586 dev_err(card->dev,
3587 "ASoC: Both platform name/of_node are set for %s\n",
3588 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003589 return -EINVAL;
3590 }
3591
3592 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003593 * CPU device may be specified by either name or OF node, but
3594 * can be left unspecified, and will be matched based on DAI
3595 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003596 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003597 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003598 dev_err(card->dev,
3599 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3600 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003601 return -EINVAL;
3602 }
3603 /*
3604 * At least one of CPU DAI name or CPU device name/node must be
3605 * specified
3606 */
3607 if (!link->cpu_dai_name &&
3608 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003609 dev_err(card->dev,
3610 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3611 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003612 return -EINVAL;
3613 }
3614 }
3615
Mark Browned77cc12011-05-03 18:25:34 +01003616 dev_set_drvdata(card->dev, card);
3617
Stephen Warren111c6412011-01-28 14:26:35 -07003618 snd_soc_initialize_card_lists(card);
3619
Vinod Koul150dd2f2011-01-13 22:48:32 +05303620 soc_init_card_debugfs(card);
3621
Mark Brown181a6892012-03-14 20:18:49 +00003622 card->rtd = devm_kzalloc(card->dev,
3623 sizeof(struct snd_soc_pcm_runtime) *
3624 (card->num_links + card->num_aux_devs),
3625 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003626 if (card->rtd == NULL)
3627 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003628 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003629 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003630
3631 for (i = 0; i < card->num_links; i++)
3632 card->rtd[i].dai_link = &card->dai_link[i];
3633
Mark Brownc5af3a22008-11-28 13:29:45 +00003634 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003635 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003636 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003637 mutex_init(&card->mutex);
Liam Girdwooda73fb2df2012-03-07 10:38:26 +00003638 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003639
Mark Brownb19e6e72012-03-14 21:18:39 +00003640 ret = snd_soc_instantiate_card(card);
3641 if (ret != 0)
3642 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003643
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003644 /* deactivate pins to sleep state */
3645 for (i = 0; i < card->num_rtd; i++) {
3646 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3647 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3648 if (!codec_dai->active)
3649 pinctrl_pm_select_sleep_state(codec_dai->dev);
3650 if (!cpu_dai->active)
3651 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3652 }
3653
Mark Brownb19e6e72012-03-14 21:18:39 +00003654 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003655}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303656EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003657
3658/**
3659 * snd_soc_unregister_card - Unregister a card with the ASoC core
3660 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003661 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003662 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003663 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303664int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003665{
Vinod Koulb0e26482011-01-13 22:48:02 +05303666 if (card->instantiated)
3667 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003668 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003669
3670 return 0;
3671}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303672EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003673
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003674/*
3675 * Simplify DAI link configuration by removing ".-1" from device names
3676 * and sanitizing names.
3677 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003678static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003679{
3680 char *found, name[NAME_SIZE];
3681 int id1, id2;
3682
3683 if (dev_name(dev) == NULL)
3684 return NULL;
3685
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003686 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003687
3688 /* are we a "%s.%d" name (platform and SPI components) */
3689 found = strstr(name, dev->driver->name);
3690 if (found) {
3691 /* get ID */
3692 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3693
3694 /* discard ID from name if ID == -1 */
3695 if (*id == -1)
3696 found[strlen(dev->driver->name)] = '\0';
3697 }
3698
3699 } else {
3700 /* I2C component devices are named "bus-addr" */
3701 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3702 char tmp[NAME_SIZE];
3703
3704 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003705 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003706
3707 /* sanitize component name for DAI link creation */
3708 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003709 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003710 } else
3711 *id = 0;
3712 }
3713
3714 return kstrdup(name, GFP_KERNEL);
3715}
3716
3717/*
3718 * Simplify DAI link naming for single devices with multiple DAIs by removing
3719 * any ".-1" and using the DAI name (instead of device name).
3720 */
3721static inline char *fmt_multiple_name(struct device *dev,
3722 struct snd_soc_dai_driver *dai_drv)
3723{
3724 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003725 dev_err(dev,
3726 "ASoC: error - multiple DAI %s registered with no name\n",
3727 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003728 return NULL;
3729 }
3730
3731 return kstrdup(dai_drv->name, GFP_KERNEL);
3732}
3733
Mark Brown91151712008-11-30 23:31:24 +00003734/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003735 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003736 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003737 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003738 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003739static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003740{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003741 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003742
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003743 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003744 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3745 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003746 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003747 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003748 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003749 }
Mark Brown91151712008-11-30 23:31:24 +00003750}
Mark Brown91151712008-11-30 23:31:24 +00003751
3752/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003753 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003754 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003755 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003756 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3757 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003758 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003759 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003760 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3761 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003762 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003763static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003764 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3765 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003766{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003767 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003768 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003769 unsigned int i;
3770 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003771
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003772 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003773
3774 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003775
3776 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003777 if (dai == NULL) {
3778 ret = -ENOMEM;
3779 goto err;
3780 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003781
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003782 /*
3783 * Back in the old days when we still had component-less DAIs,
3784 * instead of having a static name, component-less DAIs would
3785 * inherit the name of the parent device so it is possible to
3786 * register multiple instances of the DAI. We still need to keep
3787 * the same naming style even though those DAIs are not
3788 * component-less anymore.
3789 */
3790 if (count == 1 && legacy_dai_naming) {
3791 dai->name = fmt_single_name(dev, &dai->id);
3792 } else {
3793 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3794 if (dai_drv[i].id)
3795 dai->id = dai_drv[i].id;
3796 else
3797 dai->id = i;
3798 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003799 if (dai->name == NULL) {
3800 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003801 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003802 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003803 }
3804
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003805 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003806 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003807 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003808 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00003809 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003810 if (!dai->driver->ops)
3811 dai->driver->ops = &null_dai_ops;
3812
Peter Ujfalusi5f800082012-08-07 10:24:13 +03003813 if (!dai->codec)
3814 dai->dapm.idle_bias_off = 1;
3815
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003816 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01003817
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003818 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003819 }
3820
3821 return 0;
3822
3823err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003824 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00003825
3826 return ret;
3827}
Mark Brown91151712008-11-30 23:31:24 +00003828
3829/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003830 * snd_soc_register_component - Register a component with the ASoC core
3831 *
3832 */
3833static int
3834__snd_soc_register_component(struct device *dev,
3835 struct snd_soc_component *cmpnt,
3836 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003837 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003838 struct snd_soc_dai_driver *dai_drv,
3839 int num_dai, bool allow_single_dai)
3840{
3841 int ret;
3842
3843 dev_dbg(dev, "component register %s\n", dev_name(dev));
3844
3845 if (!cmpnt) {
3846 dev_err(dev, "ASoC: Failed to connecting component\n");
3847 return -ENOMEM;
3848 }
3849
3850 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
3851 if (!cmpnt->name) {
3852 dev_err(dev, "ASoC: Failed to simplifying name\n");
3853 return -ENOMEM;
3854 }
3855
3856 cmpnt->dev = dev;
3857 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07003858 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003859 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01003860 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003861
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003862 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
3863 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003864 if (ret < 0) {
3865 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
3866 goto error_component_name;
3867 }
3868
3869 mutex_lock(&client_mutex);
3870 list_add(&cmpnt->list, &component_list);
3871 mutex_unlock(&client_mutex);
3872
3873 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
3874
3875 return ret;
3876
3877error_component_name:
3878 kfree(cmpnt->name);
3879
3880 return ret;
3881}
3882
3883int snd_soc_register_component(struct device *dev,
3884 const struct snd_soc_component_driver *cmpnt_drv,
3885 struct snd_soc_dai_driver *dai_drv,
3886 int num_dai)
3887{
3888 struct snd_soc_component *cmpnt;
3889
3890 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
3891 if (!cmpnt) {
3892 dev_err(dev, "ASoC: Failed to allocate memory\n");
3893 return -ENOMEM;
3894 }
3895
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003896 cmpnt->ignore_pmdown_time = true;
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003897 cmpnt->registered_as_component = true;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01003898
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003899 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003900 dai_drv, num_dai, true);
3901}
3902EXPORT_SYMBOL_GPL(snd_soc_register_component);
3903
3904/**
3905 * snd_soc_unregister_component - Unregister a component from the ASoC core
3906 *
3907 */
3908void snd_soc_unregister_component(struct device *dev)
3909{
3910 struct snd_soc_component *cmpnt;
3911
3912 list_for_each_entry(cmpnt, &component_list, list) {
Lars-Peter Clausen98e639f2014-03-18 09:02:11 +01003913 if (dev == cmpnt->dev && cmpnt->registered_as_component)
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003914 goto found;
3915 }
3916 return;
3917
3918found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003919 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07003920
3921 mutex_lock(&client_mutex);
3922 list_del(&cmpnt->list);
3923 mutex_unlock(&client_mutex);
3924
3925 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
3926 kfree(cmpnt->name);
3927}
3928EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
3929
3930/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003931 * snd_soc_add_platform - Add a platform to the ASoC core
3932 * @dev: The parent device for the platform
3933 * @platform: The platform to add
3934 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00003935 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003936int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01003937 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003938{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003939 int ret;
3940
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003941 /* create platform component name */
3942 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03003943 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003944 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003945
3946 platform->dev = dev;
3947 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003948 platform->dapm.dev = dev;
3949 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003950 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00003951 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003952
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01003953 /* register component */
3954 ret = __snd_soc_register_component(dev, &platform->component,
3955 &platform_drv->component_driver,
3956 NULL, NULL, 0, false);
3957 if (ret < 0) {
3958 dev_err(platform->component.dev,
3959 "ASoC: Failed to register component: %d\n", ret);
3960 return ret;
3961 }
3962
Mark Brown12a48a8c2008-12-03 19:40:30 +00003963 mutex_lock(&client_mutex);
3964 list_add(&platform->list, &platform_list);
3965 mutex_unlock(&client_mutex);
3966
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003967 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003968
3969 return 0;
3970}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003971EXPORT_SYMBOL_GPL(snd_soc_add_platform);
3972
3973/**
3974 * snd_soc_register_platform - Register a platform with the ASoC core
3975 *
3976 * @platform: platform to register
3977 */
3978int snd_soc_register_platform(struct device *dev,
3979 const struct snd_soc_platform_driver *platform_drv)
3980{
3981 struct snd_soc_platform *platform;
3982 int ret;
3983
3984 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
3985
3986 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3987 if (platform == NULL)
3988 return -ENOMEM;
3989
3990 ret = snd_soc_add_platform(dev, platform, platform_drv);
3991 if (ret)
3992 kfree(platform);
3993
3994 return ret;
3995}
Mark Brown12a48a8c2008-12-03 19:40:30 +00003996EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3997
3998/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02003999 * snd_soc_remove_platform - Remove a platform from the ASoC core
4000 * @platform: the platform to remove
4001 */
4002void snd_soc_remove_platform(struct snd_soc_platform *platform)
4003{
Lars-Peter Clausenb37f1d12014-03-18 09:02:12 +01004004 snd_soc_unregister_component(platform->dev);
4005
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004006 mutex_lock(&client_mutex);
4007 list_del(&platform->list);
4008 mutex_unlock(&client_mutex);
4009
4010 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4011 platform->name);
4012 kfree(platform->name);
4013}
4014EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4015
4016struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4017{
4018 struct snd_soc_platform *platform;
4019
4020 list_for_each_entry(platform, &platform_list, list) {
4021 if (dev == platform->dev)
4022 return platform;
4023 }
4024
4025 return NULL;
4026}
4027EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4028
4029/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004030 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4031 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004032 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004033 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004034void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004035{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004036 struct snd_soc_platform *platform;
4037
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004038 platform = snd_soc_lookup_platform(dev);
4039 if (!platform)
4040 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004041
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004042 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004043 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004044}
4045EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4046
Mark Brown151ab222009-05-09 16:22:58 +01004047static u64 codec_format_map[] = {
4048 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4049 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4050 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4051 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4052 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4053 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4054 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4055 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4056 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4057 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4058 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4059 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4060 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4061 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4062 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4063 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4064};
4065
4066/* Fix up the DAI formats for endianness: codecs don't actually see
4067 * the endianness of the data but we're using the CPU format
4068 * definitions which do need to include endianness so we ensure that
4069 * codec DAIs always have both big and little endian variants set.
4070 */
4071static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4072{
4073 int i;
4074
4075 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4076 if (stream->formats & codec_format_map[i])
4077 stream->formats |= codec_format_map[i];
4078}
4079
Mark Brown0d0cf002008-12-10 14:32:45 +00004080/**
4081 * snd_soc_register_codec - Register a codec with the ASoC core
4082 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004083 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004084 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004085int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004086 const struct snd_soc_codec_driver *codec_drv,
4087 struct snd_soc_dai_driver *dai_drv,
4088 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004089{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004090 struct snd_soc_codec *codec;
4091 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004092
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004093 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004094
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004095 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4096 if (codec == NULL)
4097 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004098
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004099 /* create CODEC component name */
4100 codec->name = fmt_single_name(dev, &codec->id);
4101 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004102 ret = -ENOMEM;
4103 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004104 }
4105
Mark Brownc3acec22010-12-02 16:15:29 +00004106 codec->write = codec_drv->write;
4107 codec->read = codec_drv->read;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004108 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004109 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4110 codec->dapm.dev = dev;
4111 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004112 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004113 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004114 codec->dev = dev;
4115 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004116 codec->num_dai = num_dai;
Lars-Peter Clausen6137a5c2014-03-18 09:02:06 +01004117 codec->val_bytes = codec_drv->reg_word_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004118 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004119
4120 for (i = 0; i < num_dai; i++) {
4121 fixup_codec_formats(&dai_drv[i].playback);
4122 fixup_codec_formats(&dai_drv[i].capture);
4123 }
4124
Mark Brown054880f2012-04-09 17:29:19 +01004125 mutex_lock(&client_mutex);
4126 list_add(&codec->list, &codec_list);
4127 mutex_unlock(&client_mutex);
4128
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004129 /* register component */
4130 ret = __snd_soc_register_component(dev, &codec->component,
4131 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004132 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004133 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004134 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004135 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004136 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004137
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004138 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004139 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004140
Kuninori Morimotof790b942013-02-25 00:40:09 -08004141fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004142 mutex_lock(&client_mutex);
4143 list_del(&codec->list);
4144 mutex_unlock(&client_mutex);
4145
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004146 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004147fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004148 kfree(codec);
4149 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004150}
4151EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4152
4153/**
4154 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4155 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004156 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004157 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004158void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004159{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004160 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004161
4162 list_for_each_entry(codec, &codec_list, list) {
4163 if (dev == codec->dev)
4164 goto found;
4165 }
4166 return;
4167
4168found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004169 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004170
Mark Brown0d0cf002008-12-10 14:32:45 +00004171 mutex_lock(&client_mutex);
4172 list_del(&codec->list);
4173 mutex_unlock(&client_mutex);
4174
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004175 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004176
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004177 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004178 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004179 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004180}
4181EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4182
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004183/* Retrieve a card's name from device tree */
4184int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4185 const char *propname)
4186{
4187 struct device_node *np = card->dev->of_node;
4188 int ret;
4189
4190 ret = of_property_read_string_index(np, propname, 0, &card->name);
4191 /*
4192 * EINVAL means the property does not exist. This is fine providing
4193 * card->name was previously set, which is checked later in
4194 * snd_soc_register_card.
4195 */
4196 if (ret < 0 && ret != -EINVAL) {
4197 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004198 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004199 propname, ret);
4200 return ret;
4201 }
4202
4203 return 0;
4204}
4205EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4206
Xiubo Li9a6d4862014-02-08 15:59:52 +08004207static const struct snd_soc_dapm_widget simple_widgets[] = {
4208 SND_SOC_DAPM_MIC("Microphone", NULL),
4209 SND_SOC_DAPM_LINE("Line", NULL),
4210 SND_SOC_DAPM_HP("Headphone", NULL),
4211 SND_SOC_DAPM_SPK("Speaker", NULL),
4212};
4213
4214int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4215 const char *propname)
4216{
4217 struct device_node *np = card->dev->of_node;
4218 struct snd_soc_dapm_widget *widgets;
4219 const char *template, *wname;
4220 int i, j, num_widgets, ret;
4221
4222 num_widgets = of_property_count_strings(np, propname);
4223 if (num_widgets < 0) {
4224 dev_err(card->dev,
4225 "ASoC: Property '%s' does not exist\n", propname);
4226 return -EINVAL;
4227 }
4228 if (num_widgets & 1) {
4229 dev_err(card->dev,
4230 "ASoC: Property '%s' length is not even\n", propname);
4231 return -EINVAL;
4232 }
4233
4234 num_widgets /= 2;
4235 if (!num_widgets) {
4236 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4237 propname);
4238 return -EINVAL;
4239 }
4240
4241 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4242 GFP_KERNEL);
4243 if (!widgets) {
4244 dev_err(card->dev,
4245 "ASoC: Could not allocate memory for widgets\n");
4246 return -ENOMEM;
4247 }
4248
4249 for (i = 0; i < num_widgets; i++) {
4250 ret = of_property_read_string_index(np, propname,
4251 2 * i, &template);
4252 if (ret) {
4253 dev_err(card->dev,
4254 "ASoC: Property '%s' index %d read error:%d\n",
4255 propname, 2 * i, ret);
4256 return -EINVAL;
4257 }
4258
4259 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4260 if (!strncmp(template, simple_widgets[j].name,
4261 strlen(simple_widgets[j].name))) {
4262 widgets[i] = simple_widgets[j];
4263 break;
4264 }
4265 }
4266
4267 if (j >= ARRAY_SIZE(simple_widgets)) {
4268 dev_err(card->dev,
4269 "ASoC: DAPM widget '%s' is not supported\n",
4270 template);
4271 return -EINVAL;
4272 }
4273
4274 ret = of_property_read_string_index(np, propname,
4275 (2 * i) + 1,
4276 &wname);
4277 if (ret) {
4278 dev_err(card->dev,
4279 "ASoC: Property '%s' index %d read error:%d\n",
4280 propname, (2 * i) + 1, ret);
4281 return -EINVAL;
4282 }
4283
4284 widgets[i].name = wname;
4285 }
4286
4287 card->dapm_widgets = widgets;
4288 card->num_dapm_widgets = num_widgets;
4289
4290 return 0;
4291}
4292EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4293
Xiubo Li89c67852014-02-14 09:34:35 +08004294int snd_soc_of_parse_tdm_slot(struct device_node *np,
4295 unsigned int *slots,
4296 unsigned int *slot_width)
4297{
4298 u32 val;
4299 int ret;
4300
4301 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4302 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4303 if (ret)
4304 return ret;
4305
4306 if (slots)
4307 *slots = val;
4308 }
4309
4310 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4311 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4312 if (ret)
4313 return ret;
4314
4315 if (slot_width)
4316 *slot_width = val;
4317 }
4318
4319 return 0;
4320}
4321EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4322
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004323int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4324 const char *propname)
4325{
4326 struct device_node *np = card->dev->of_node;
4327 int num_routes;
4328 struct snd_soc_dapm_route *routes;
4329 int i, ret;
4330
4331 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004332 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004333 dev_err(card->dev,
4334 "ASoC: Property '%s' does not exist or its length is not even\n",
4335 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004336 return -EINVAL;
4337 }
4338 num_routes /= 2;
4339 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004340 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004341 propname);
4342 return -EINVAL;
4343 }
4344
4345 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4346 GFP_KERNEL);
4347 if (!routes) {
4348 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004349 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004350 return -EINVAL;
4351 }
4352
4353 for (i = 0; i < num_routes; i++) {
4354 ret = of_property_read_string_index(np, propname,
4355 2 * i, &routes[i].sink);
4356 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004357 dev_err(card->dev,
4358 "ASoC: Property '%s' index %d could not be read: %d\n",
4359 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004360 return -EINVAL;
4361 }
4362 ret = of_property_read_string_index(np, propname,
4363 (2 * i) + 1, &routes[i].source);
4364 if (ret) {
4365 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004366 "ASoC: Property '%s' index %d could not be read: %d\n",
4367 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004368 return -EINVAL;
4369 }
4370 }
4371
4372 card->num_dapm_routes = num_routes;
4373 card->dapm_routes = routes;
4374
4375 return 0;
4376}
4377EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4378
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004379unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4380 const char *prefix)
4381{
4382 int ret, i;
4383 char prop[128];
4384 unsigned int format = 0;
4385 int bit, frame;
4386 const char *str;
4387 struct {
4388 char *name;
4389 unsigned int val;
4390 } of_fmt_table[] = {
4391 { "i2s", SND_SOC_DAIFMT_I2S },
4392 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4393 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4394 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4395 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4396 { "ac97", SND_SOC_DAIFMT_AC97 },
4397 { "pdm", SND_SOC_DAIFMT_PDM},
4398 { "msb", SND_SOC_DAIFMT_MSB },
4399 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004400 };
4401
4402 if (!prefix)
4403 prefix = "";
4404
4405 /*
4406 * check "[prefix]format = xxx"
4407 * SND_SOC_DAIFMT_FORMAT_MASK area
4408 */
4409 snprintf(prop, sizeof(prop), "%sformat", prefix);
4410 ret = of_property_read_string(np, prop, &str);
4411 if (ret == 0) {
4412 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4413 if (strcmp(str, of_fmt_table[i].name) == 0) {
4414 format |= of_fmt_table[i].val;
4415 break;
4416 }
4417 }
4418 }
4419
4420 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004421 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004422 * SND_SOC_DAIFMT_CLOCK_MASK area
4423 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004424 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4425 if (of_get_property(np, prop, NULL))
4426 format |= SND_SOC_DAIFMT_CONT;
4427 else
4428 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004429
4430 /*
4431 * check "[prefix]bitclock-inversion"
4432 * check "[prefix]frame-inversion"
4433 * SND_SOC_DAIFMT_INV_MASK area
4434 */
4435 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4436 bit = !!of_get_property(np, prop, NULL);
4437
4438 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4439 frame = !!of_get_property(np, prop, NULL);
4440
4441 switch ((bit << 4) + frame) {
4442 case 0x11:
4443 format |= SND_SOC_DAIFMT_IB_IF;
4444 break;
4445 case 0x10:
4446 format |= SND_SOC_DAIFMT_IB_NF;
4447 break;
4448 case 0x01:
4449 format |= SND_SOC_DAIFMT_NB_IF;
4450 break;
4451 default:
4452 /* SND_SOC_DAIFMT_NB_NF is default */
4453 break;
4454 }
4455
4456 /*
4457 * check "[prefix]bitclock-master"
4458 * check "[prefix]frame-master"
4459 * SND_SOC_DAIFMT_MASTER_MASK area
4460 */
4461 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4462 bit = !!of_get_property(np, prop, NULL);
4463
4464 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4465 frame = !!of_get_property(np, prop, NULL);
4466
4467 switch ((bit << 4) + frame) {
4468 case 0x11:
4469 format |= SND_SOC_DAIFMT_CBM_CFM;
4470 break;
4471 case 0x10:
4472 format |= SND_SOC_DAIFMT_CBM_CFS;
4473 break;
4474 case 0x01:
4475 format |= SND_SOC_DAIFMT_CBS_CFM;
4476 break;
4477 default:
4478 format |= SND_SOC_DAIFMT_CBS_CFS;
4479 break;
4480 }
4481
4482 return format;
4483}
4484EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4485
Kuninori Morimotocb470082013-09-10 17:39:56 -07004486int snd_soc_of_get_dai_name(struct device_node *of_node,
4487 const char **dai_name)
4488{
4489 struct snd_soc_component *pos;
4490 struct of_phandle_args args;
4491 int ret;
4492
4493 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4494 "#sound-dai-cells", 0, &args);
4495 if (ret)
4496 return ret;
4497
4498 ret = -EPROBE_DEFER;
4499
4500 mutex_lock(&client_mutex);
4501 list_for_each_entry(pos, &component_list, list) {
4502 if (pos->dev->of_node != args.np)
4503 continue;
4504
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004505 if (pos->driver->of_xlate_dai_name) {
4506 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4507 } else {
4508 int id = -1;
4509
4510 switch (args.args_count) {
4511 case 0:
4512 id = 0; /* same as dai_drv[0] */
4513 break;
4514 case 1:
4515 id = args.args[0];
4516 break;
4517 default:
4518 /* not supported */
4519 break;
4520 }
4521
4522 if (id < 0 || id >= pos->num_dai) {
4523 ret = -EINVAL;
Xiubo Lie41975e2013-12-20 14:39:51 +08004524 break;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004525 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004526
4527 ret = 0;
4528
4529 *dai_name = pos->dai_drv[id].name;
4530 if (!*dai_name)
4531 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004532 }
4533
Kuninori Morimotocb470082013-09-10 17:39:56 -07004534 break;
4535 }
4536 mutex_unlock(&client_mutex);
4537
4538 of_node_put(args.np);
4539
4540 return ret;
4541}
4542EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4543
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004544static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004545{
Mark Brown384c89e2008-12-03 17:34:03 +00004546#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004547 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4548 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004549 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004550 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004551 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004552
Mark Brown8a9dab12011-01-10 22:25:21 +00004553 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004554 &codec_list_fops))
4555 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4556
Mark Brown8a9dab12011-01-10 22:25:21 +00004557 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004558 &dai_list_fops))
4559 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004560
Mark Brown8a9dab12011-01-10 22:25:21 +00004561 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004562 &platform_list_fops))
4563 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004564#endif
4565
Mark Brownfb257892011-04-28 10:57:54 +01004566 snd_soc_util_init();
4567
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004568 return platform_driver_register(&soc_driver);
4569}
Mark Brown4abe8e12010-10-12 17:41:03 +01004570module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004571
Mark Brown7d8c16a2008-11-30 22:11:24 +00004572static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004573{
Mark Brownfb257892011-04-28 10:57:54 +01004574 snd_soc_util_exit();
4575
Mark Brown384c89e2008-12-03 17:34:03 +00004576#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004577 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004578#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004579 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004580}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004581module_exit(snd_soc_exit);
4582
4583/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004584MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004585MODULE_DESCRIPTION("ALSA SoC Core");
4586MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004587MODULE_ALIAS("platform:soc-audio");