blob: b2d889667ab44f649c249b1a6b663bf79017446b [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 Clausenb92d1502011-08-27 18:24:14 +0200157 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000158 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000159 if (codec->driver->display_register) {
160 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000161 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100162 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000163 /* only support larger than PAGE_SIZE bytes debugfs
164 * entries for the default case */
165 if (p >= pos) {
166 if (total + len >= count - 1)
167 break;
168 format_register_str(codec, i, buf + total, len);
169 total += len;
170 }
171 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100172 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000173 }
174
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000175 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000176
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000177 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000178}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000179
Mark Brown2624d5f2009-11-03 21:56:13 +0000180static ssize_t codec_reg_show(struct device *dev,
181 struct device_attribute *attr, char *buf)
182{
Mark Brown36ae1a92012-01-06 17:12:45 -0800183 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000185 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000186}
187
188static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
189
Mark Browndbe21402010-02-12 11:37:24 +0000190static ssize_t pmdown_time_show(struct device *dev,
191 struct device_attribute *attr, char *buf)
192{
Mark Brown36ae1a92012-01-06 17:12:45 -0800193 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000194
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000195 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000196}
197
198static ssize_t pmdown_time_set(struct device *dev,
199 struct device_attribute *attr,
200 const char *buf, size_t count)
201{
Mark Brown36ae1a92012-01-06 17:12:45 -0800202 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700203 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000204
Jingoo Hanb785a492013-07-19 16:24:59 +0900205 ret = kstrtol(buf, 10, &rtd->pmdown_time);
Mark Brownc593b522010-10-27 20:11:17 -0700206 if (ret)
207 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000208
209 return count;
210}
211
212static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
213
Mark Brown2624d5f2009-11-03 21:56:13 +0000214#ifdef CONFIG_DEBUG_FS
Mark Brown2624d5f2009-11-03 21:56:13 +0000215static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000216 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000217{
218 ssize_t ret;
219 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000220 char *buf;
221
222 if (*ppos < 0 || !count)
223 return -EINVAL;
224
225 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000226 if (!buf)
227 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000228
229 ret = soc_codec_reg_show(codec, buf, count, *ppos);
230 if (ret >= 0) {
231 if (copy_to_user(user_buf, buf, ret)) {
232 kfree(buf);
233 return -EFAULT;
234 }
235 *ppos += ret;
236 }
237
Mark Brown2624d5f2009-11-03 21:56:13 +0000238 kfree(buf);
239 return ret;
240}
241
242static ssize_t codec_reg_write_file(struct file *file,
243 const char __user *user_buf, size_t count, loff_t *ppos)
244{
245 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700246 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000247 char *start = buf;
248 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000249 struct snd_soc_codec *codec = file->private_data;
Jingoo Hanb785a492013-07-19 16:24:59 +0900250 int ret;
Mark Brown2624d5f2009-11-03 21:56:13 +0000251
252 buf_size = min(count, (sizeof(buf)-1));
253 if (copy_from_user(buf, user_buf, buf_size))
254 return -EFAULT;
255 buf[buf_size] = 0;
256
Mark Brown2624d5f2009-11-03 21:56:13 +0000257 while (*start == ' ')
258 start++;
259 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000260 while (*start == ' ')
261 start++;
Jingoo Hanb785a492013-07-19 16:24:59 +0900262 ret = kstrtoul(start, 16, &value);
263 if (ret)
264 return ret;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000265
266 /* Userspace has been fiddling around behind the kernel's back */
Rusty Russell373d4d02013-01-21 17:17:39 +1030267 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE);
Mark Brown0d51a9c2011-01-06 16:04:57 +0000268
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000269 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000270 return buf_size;
271}
272
273static const struct file_operations codec_reg_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700274 .open = simple_open,
Mark Brown2624d5f2009-11-03 21:56:13 +0000275 .read = codec_reg_read_file,
276 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200277 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000278};
279
280static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
281{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200282 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
283
284 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
285 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000286 if (!codec->debugfs_codec_root) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200287 dev_warn(codec->dev,
288 "ASoC: Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 return;
290 }
291
Mark Brownaaee8ef2011-01-26 20:53:50 +0000292 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
293 &codec->cache_sync);
294 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
295 &codec->cache_only);
296
Mark Brown2624d5f2009-11-03 21:56:13 +0000297 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
298 codec->debugfs_codec_root,
299 codec, &codec_reg_fops);
300 if (!codec->debugfs_reg)
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200301 dev_warn(codec->dev,
302 "ASoC: Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000303
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200304 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000305}
306
307static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
308{
309 debugfs_remove_recursive(codec->debugfs_codec_root);
310}
311
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000312static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
313{
314 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
315
316 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
317 debugfs_card_root);
318 if (!platform->debugfs_platform_root) {
319 dev_warn(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000320 "ASoC: Failed to create platform debugfs directory\n");
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000321 return;
322 }
323
324 snd_soc_dapm_debugfs_init(&platform->dapm,
325 platform->debugfs_platform_root);
326}
327
328static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
329{
330 debugfs_remove_recursive(platform->debugfs_platform_root);
331}
332
Mark Brownc3c5a192010-09-15 18:15:14 +0100333static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
334 size_t count, loff_t *ppos)
335{
336 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100337 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100338 struct snd_soc_codec *codec;
339
340 if (!buf)
341 return -ENOMEM;
342
Mark Brown2b194f9d2010-10-13 10:52:16 +0100343 list_for_each_entry(codec, &codec_list, list) {
344 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
345 codec->name);
346 if (len >= 0)
347 ret += len;
348 if (ret > PAGE_SIZE) {
349 ret = PAGE_SIZE;
350 break;
351 }
352 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100353
354 if (ret >= 0)
355 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
356
357 kfree(buf);
358
359 return ret;
360}
361
362static const struct file_operations codec_list_fops = {
363 .read = codec_list_read_file,
364 .llseek = default_llseek,/* read accesses f_pos */
365};
366
Mark Brownf3208782010-09-15 18:19:07 +0100367static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
368 size_t count, loff_t *ppos)
369{
370 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100371 ssize_t len, ret = 0;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100372 struct snd_soc_component *component;
Mark Brownf3208782010-09-15 18:19:07 +0100373 struct snd_soc_dai *dai;
374
375 if (!buf)
376 return -ENOMEM;
377
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100378 list_for_each_entry(component, &component_list, list) {
379 list_for_each_entry(dai, &component->dai_list, list) {
380 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
381 dai->name);
382 if (len >= 0)
383 ret += len;
384 if (ret > PAGE_SIZE) {
385 ret = PAGE_SIZE;
386 break;
387 }
Mark Brown2b194f9d2010-10-13 10:52:16 +0100388 }
389 }
Mark Brownf3208782010-09-15 18:19:07 +0100390
Mark Brown2b194f9d2010-10-13 10:52:16 +0100391 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100392
393 kfree(buf);
394
395 return ret;
396}
397
398static const struct file_operations dai_list_fops = {
399 .read = dai_list_read_file,
400 .llseek = default_llseek,/* read accesses f_pos */
401};
402
Mark Brown19c7ac22010-09-15 18:22:40 +0100403static ssize_t platform_list_read_file(struct file *file,
404 char __user *user_buf,
405 size_t count, loff_t *ppos)
406{
407 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100408 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100409 struct snd_soc_platform *platform;
410
411 if (!buf)
412 return -ENOMEM;
413
Mark Brown2b194f9d2010-10-13 10:52:16 +0100414 list_for_each_entry(platform, &platform_list, list) {
415 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
416 platform->name);
417 if (len >= 0)
418 ret += len;
419 if (ret > PAGE_SIZE) {
420 ret = PAGE_SIZE;
421 break;
422 }
423 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100424
Mark Brown2b194f9d2010-10-13 10:52:16 +0100425 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100426
427 kfree(buf);
428
429 return ret;
430}
431
432static const struct file_operations platform_list_fops = {
433 .read = platform_list_read_file,
434 .llseek = default_llseek,/* read accesses f_pos */
435};
436
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200437static void soc_init_card_debugfs(struct snd_soc_card *card)
438{
439 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000440 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200441 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200442 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100443 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200444 return;
445 }
446
447 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
448 card->debugfs_card_root,
449 &card->pop_time);
450 if (!card->debugfs_pop_time)
451 dev_warn(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000452 "ASoC: Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200453}
454
455static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
456{
457 debugfs_remove_recursive(card->debugfs_card_root);
458}
459
Mark Brown2624d5f2009-11-03 21:56:13 +0000460#else
461
462static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
463{
464}
465
466static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
467{
468}
Axel Linb95fccb2010-11-09 17:06:44 +0800469
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000470static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
471{
472}
473
474static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
475{
476}
477
Axel Linb95fccb2010-11-09 17:06:44 +0800478static inline void soc_init_card_debugfs(struct snd_soc_card *card)
479{
480}
481
482static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
483{
484}
Mark Brown2624d5f2009-11-03 21:56:13 +0000485#endif
486
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100487struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card,
488 const char *dai_link, int stream)
489{
490 int i;
491
492 for (i = 0; i < card->num_links; i++) {
493 if (card->rtd[i].dai_link->no_pcm &&
494 !strcmp(card->rtd[i].dai_link->name, dai_link))
495 return card->rtd[i].pcm->streams[stream].substream;
496 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000497 dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100498 return NULL;
499}
500EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream);
501
502struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card,
503 const char *dai_link)
504{
505 int i;
506
507 for (i = 0; i < card->num_links; i++) {
508 if (!strcmp(card->rtd[i].dai_link->name, dai_link))
509 return &card->rtd[i];
510 }
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000511 dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link);
Liam Girdwood47c88ff2012-04-25 12:12:53 +0100512 return NULL;
513}
514EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime);
515
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200516#ifdef CONFIG_SND_SOC_AC97_BUS
517/* unregister ac97 codec */
518static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
519{
520 if (codec->ac97->dev.bus)
521 device_unregister(&codec->ac97->dev);
522 return 0;
523}
524
525/* stop no dev release warning */
526static void soc_ac97_device_release(struct device *dev){}
527
528/* register ac97 codec to bus */
529static int soc_ac97_dev_register(struct snd_soc_codec *codec)
530{
531 int err;
532
533 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100534 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200535 codec->ac97->dev.release = soc_ac97_device_release;
536
Kay Sieversbb072bf2008-11-02 03:50:35 +0100537 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000538 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200539 err = device_register(&codec->ac97->dev);
540 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000541 dev_err(codec->dev, "ASoC: Can't register ac97 bus\n");
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200542 codec->ac97->dev.bus = NULL;
543 return err;
544 }
545 return 0;
546}
547#endif
548
Richard Fitzgerald9d58a072013-08-05 13:17:28 +0100549static void codec2codec_close_delayed_work(struct work_struct *work)
550{
551 /* Currently nothing to do for c2c links
552 * Since c2c links are internal nodes in the DAPM graph and
553 * don't interface with the outside world or application layer
554 * we don't have to do any special handling on close.
555 */
556}
557
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000558#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200559/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000560int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200561{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000562 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200563 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200564 int i;
565
Daniel Macke3509ff2009-06-03 17:44:49 +0200566 /* If the initialization of this soc device failed, there is no codec
567 * associated with it. Just bail out in this case.
568 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200570 return 0;
571
Andy Green6ed25972008-06-13 16:24:05 +0100572 /* Due to the resume being scheduled into a workqueue we could
573 * suspend before that's finished - wait for it to complete.
574 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 snd_power_lock(card->snd_card);
576 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
577 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100578
579 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000580 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100581
Mark Browna00f90f2010-12-02 16:24:24 +0000582 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000583 for (i = 0; i < card->num_rtd; i++) {
584 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
585 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100586
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000587 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100588 continue;
589
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 if (drv->ops->digital_mute && dai->playback_active)
591 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200592 }
593
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100594 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000595 for (i = 0; i < card->num_rtd; i++) {
596 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100597 continue;
598
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000599 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100600 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100601
Mark Brown87506542008-11-18 20:50:34 +0000602 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000603 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200604
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000605 for (i = 0; i < card->num_rtd; i++) {
606 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
607 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100608
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000609 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100610 continue;
611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
613 cpu_dai->driver->suspend(cpu_dai);
614 if (platform->driver->suspend && !platform->suspended) {
615 platform->driver->suspend(cpu_dai);
616 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100617 }
618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200619
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000620 /* close any waiting streams and save state */
621 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo43829732012-08-20 14:51:24 -0700622 flush_delayed_work(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200623 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000624 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100625
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000626 for (i = 0; i < card->num_rtd; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000627
628 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100629 continue;
630
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800631 snd_soc_dapm_stream_event(&card->rtd[i],
632 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800633 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000634
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800635 snd_soc_dapm_stream_event(&card->rtd[i],
636 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800637 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000638 }
639
Mark Browne2d32ff2012-08-31 17:38:32 -0700640 /* Recheck all analogue paths too */
641 dapm_mark_io_dirty(&card->dapm);
642 snd_soc_dapm_sync(&card->dapm);
643
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200645 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000646 /* If there are paths active then the CODEC will be held with
647 * bias _ON and should not be suspended. */
648 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200649 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000650 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000651 /*
652 * If the CODEC is capable of idle
653 * bias off then being in STANDBY
654 * means it's doing something,
655 * otherwise fall through.
656 */
657 if (codec->dapm.idle_bias_off) {
658 dev_dbg(codec->dev,
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200659 "ASoC: idle_bias_off CODEC on over suspend\n");
Mark Brown125a25d2012-01-31 15:49:10 +0000660 break;
661 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000662 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100663 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000664 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900665 codec->cache_sync = 1;
Mark Brownda8b8e02012-09-12 12:21:52 +0800666 if (codec->using_regmap)
667 regcache_mark_dirty(codec->control_data);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800668 /* deactivate pins to sleep state */
669 pinctrl_pm_select_sleep_state(codec->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000670 break;
671 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200672 dev_dbg(codec->dev,
673 "ASoC: CODEC is on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000674 break;
675 }
676 }
677 }
678
679 for (i = 0; i < card->num_rtd; i++) {
680 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
681
682 if (card->rtd[i].dai_link->ignore_suspend)
683 continue;
684
685 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
686 cpu_dai->driver->suspend(cpu_dai);
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800687
688 /* deactivate pins to sleep state */
689 pinctrl_pm_select_sleep_state(cpu_dai->dev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690 }
691
Mark Brown87506542008-11-18 20:50:34 +0000692 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000693 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200694
695 return 0;
696}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000697EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200698
Andy Green6ed25972008-06-13 16:24:05 +0100699/* deferred resume work, so resume can complete before we finished
700 * setting our codec back up, which can be very slow on I2C
701 */
702static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200703{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000704 struct snd_soc_card *card =
705 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200706 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200707 int i;
708
Andy Green6ed25972008-06-13 16:24:05 +0100709 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
710 * so userspace apps are blocked from touching us
711 */
712
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000713 dev_dbg(card->dev, "ASoC: starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100714
Mark Brown99497882010-05-07 20:24:05 +0100715 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000716 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100717
Mark Brown87506542008-11-18 20:50:34 +0000718 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000719 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200720
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000721 /* resume AC97 DAIs */
722 for (i = 0; i < card->num_rtd; i++) {
723 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100726 continue;
727
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000728 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
729 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200730 }
731
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200732 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000733 /* If the CODEC was idle over suspend then it will have been
734 * left with bias OFF or STANDBY and suspended so we must now
735 * resume. Otherwise the suspend was suppressed.
736 */
737 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200738 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 case SND_SOC_BIAS_STANDBY:
740 case SND_SOC_BIAS_OFF:
741 codec->driver->resume(codec);
742 codec->suspended = 0;
743 break;
744 default:
Michał Mirosław10e8aa92013-05-04 22:21:38 +0200745 dev_dbg(codec->dev,
746 "ASoC: CODEC was on over suspend\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000747 break;
748 }
Mark Brown1547aba2010-05-07 21:11:40 +0100749 }
750 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200751
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000752 for (i = 0; i < card->num_rtd; i++) {
Mark Brown3efab7d2010-05-09 13:25:43 +0100753
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000754 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100755 continue;
756
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800757 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000758 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800759 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000760
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800761 snd_soc_dapm_stream_event(&card->rtd[i],
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000762 SNDRV_PCM_STREAM_CAPTURE,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800763 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200764 }
765
Mark Brown3ff3f642008-05-19 12:32:25 +0200766 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 for (i = 0; i < card->num_rtd; i++) {
768 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
769 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000771 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100772 continue;
773
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000774 if (drv->ops->digital_mute && dai->playback_active)
775 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200776 }
777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000778 for (i = 0; i < card->num_rtd; i++) {
779 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
780 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100781
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000782 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100783 continue;
784
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
786 cpu_dai->driver->resume(cpu_dai);
787 if (platform->driver->resume && platform->suspended) {
788 platform->driver->resume(cpu_dai);
789 platform->suspended = 0;
790 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791 }
792
Mark Brown87506542008-11-18 20:50:34 +0000793 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000794 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200795
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000796 dev_dbg(card->dev, "ASoC: resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100797
798 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000799 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Mark Browne2d32ff2012-08-31 17:38:32 -0700800
801 /* Recheck all analogue paths too */
802 dapm_mark_io_dirty(&card->dapm);
803 snd_soc_dapm_sync(&card->dapm);
Andy Green6ed25972008-06-13 16:24:05 +0100804}
805
806/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000807int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100808{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000809 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600810 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200811
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800812 /* If the initialization of this soc device failed, there is no codec
813 * associated with it. Just bail out in this case.
814 */
815 if (list_empty(&card->codec_dev_list))
816 return 0;
817
Nicolin Chen988e8cc2013-11-04 14:57:31 +0800818 /* activate pins from sleep state */
819 for (i = 0; i < card->num_rtd; i++) {
820 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
821 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
822 if (cpu_dai->active)
823 pinctrl_pm_select_default_state(cpu_dai->dev);
824 if (codec_dai->active)
825 pinctrl_pm_select_default_state(codec_dai->dev);
826 }
827
Mark Brown64ab9ba2009-03-31 11:27:03 +0100828 /* AC97 devices might have other drivers hanging off them so
829 * need to resume immediately. Other drivers don't have that
830 * problem and may take a substantial amount of time to resume
831 * due to I/O costs and anti-pop so handle them out of line.
832 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000833 for (i = 0; i < card->num_rtd; i++) {
834 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600835 ac97_control |= cpu_dai->driver->ac97_control;
836 }
837 if (ac97_control) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000838 dev_dbg(dev, "ASoC: Resuming AC97 immediately\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600839 soc_resume_deferred(&card->deferred_resume_work);
840 } else {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000841 dev_dbg(dev, "ASoC: Scheduling resume work\n");
Stephen Warren82e14e82011-05-25 14:06:41 -0600842 if (!schedule_work(&card->deferred_resume_work))
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000843 dev_err(dev, "ASoC: resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100844 }
Andy Green6ed25972008-06-13 16:24:05 +0100845
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200846 return 0;
847}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000848EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200849#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000850#define snd_soc_suspend NULL
851#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200852#endif
853
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100854static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800855};
856
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100857static struct snd_soc_codec *soc_find_codec(const struct device_node *codec_of_node,
858 const char *codec_name)
859{
860 struct snd_soc_codec *codec;
861
862 list_for_each_entry(codec, &codec_list, list) {
863 if (codec_of_node) {
864 if (codec->dev->of_node != codec_of_node)
865 continue;
866 } else {
867 if (strcmp(codec->name, codec_name))
868 continue;
869 }
870
871 return codec;
872 }
873
874 return NULL;
875}
876
877static struct snd_soc_dai *soc_find_codec_dai(struct snd_soc_codec *codec,
878 const char *codec_dai_name)
879{
880 struct snd_soc_dai *codec_dai;
881
882 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
883 if (!strcmp(codec_dai->name, codec_dai_name)) {
884 return codec_dai;
885 }
886 }
887
888 return NULL;
889}
890
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000891static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200892{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
894 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100895 struct snd_soc_component *component;
Mark Brown435c5e22008-12-04 15:32:53 +0000896 struct snd_soc_platform *platform;
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100897 struct snd_soc_dai *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100898 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200899
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000900 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000901
Mark Brownb19e6e72012-03-14 21:18:39 +0000902 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100903 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600904 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100905 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600906 continue;
907 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100908 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600909 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100910 list_for_each_entry(cpu_dai, &component->dai_list, list) {
911 if (dai_link->cpu_dai_name &&
912 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
913 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700914
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100915 rtd->cpu_dai = cpu_dai;
916 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000917 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000918
919 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000920 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000922 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000923 }
924
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100925 /* Find CODEC from registered list */
926 rtd->codec = soc_find_codec(dai_link->codec_of_node,
927 dai_link->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000928 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000929 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000930 dai_link->codec_name);
931 return -EPROBE_DEFER;
932 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100933
Misael Lopez Cruz12023a92014-03-21 16:27:25 +0100934 /* Find CODEC DAI from registered list */
935 rtd->codec_dai = soc_find_codec_dai(rtd->codec,
936 dai_link->codec_dai_name);
937 if (!rtd->codec_dai) {
938 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
939 dai_link->codec_dai_name);
940 return -EPROBE_DEFER;
941 }
942
Mark Brown848dd8b2011-04-27 18:16:32 +0100943 /* if there's no platform we match on the empty platform */
944 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700945 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100946 platform_name = "snd-soc-dummy";
947
Mark Brownb19e6e72012-03-14 21:18:39 +0000948 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000949 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700950 if (dai_link->platform_of_node) {
951 if (platform->dev->of_node !=
952 dai_link->platform_of_node)
953 continue;
954 } else {
955 if (strcmp(platform->name, platform_name))
956 continue;
957 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700958
959 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000960 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000961 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000962 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000963 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000964 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000965 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000966
967 card->num_rtd++;
968
969 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970}
971
Stephen Warrend12cd192012-06-08 12:34:22 -0600972static int soc_remove_platform(struct snd_soc_platform *platform)
973{
974 int ret;
975
976 if (platform->driver->remove) {
977 ret = platform->driver->remove(platform);
978 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000979 dev_err(platform->dev, "ASoC: failed to remove %d\n",
980 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600981 }
982
983 /* Make sure all DAPM widgets are freed */
984 snd_soc_dapm_free(&platform->dapm);
985
986 soc_cleanup_platform_debugfs(platform);
987 platform->probed = 0;
988 list_del(&platform->card_list);
989 module_put(platform->dev->driver->owner);
990
991 return 0;
992}
993
Jarkko Nikula589c3562010-12-06 16:27:07 +0200994static void soc_remove_codec(struct snd_soc_codec *codec)
995{
996 int err;
997
998 if (codec->driver->remove) {
999 err = codec->driver->remove(codec);
1000 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001001 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001002 }
1003
1004 /* Make sure all DAPM widgets are freed */
1005 snd_soc_dapm_free(&codec->dapm);
1006
1007 soc_cleanup_codec_debugfs(codec);
1008 codec->probed = 0;
1009 list_del(&codec->card_list);
1010 module_put(codec->dev->driver->owner);
1011}
1012
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001013static void soc_remove_codec_dai(struct snd_soc_dai *codec_dai, int order)
1014{
1015 int err;
1016
1017 if (codec_dai && codec_dai->probed &&
1018 codec_dai->driver->remove_order == order) {
1019 if (codec_dai->driver->remove) {
1020 err = codec_dai->driver->remove(codec_dai);
1021 if (err < 0)
1022 dev_err(codec_dai->dev,
1023 "ASoC: failed to remove %s: %d\n",
1024 codec_dai->name, err);
1025 }
1026 codec_dai->probed = 0;
1027 list_del(&codec_dai->card_list);
1028 }
1029}
1030
Stephen Warren62ae68f2012-06-08 12:34:23 -06001031static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001032{
1033 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001034 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1035 int err;
1036
1037 /* unregister the rtd device */
1038 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001039 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1040 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1041 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001042 rtd->dev_registered = 0;
1043 }
1044
1045 /* remove the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001046 soc_remove_codec_dai(codec_dai, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001047
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001048 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001049 if (cpu_dai && cpu_dai->probed &&
1050 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001051 if (cpu_dai->driver->remove) {
1052 err = cpu_dai->driver->remove(cpu_dai);
1053 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001054 dev_err(cpu_dai->dev,
1055 "ASoC: failed to remove %s: %d\n",
1056 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001057 }
1058 cpu_dai->probed = 0;
1059 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001060
Stephen Warren18d75642012-06-08 12:34:21 -06001061 if (!cpu_dai->codec) {
1062 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001063 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001064 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001065 }
1066}
1067
Stephen Warren62ae68f2012-06-08 12:34:23 -06001068static void soc_remove_link_components(struct snd_soc_card *card, int num,
1069 int order)
1070{
1071 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1072 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1073 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1074 struct snd_soc_platform *platform = rtd->platform;
1075 struct snd_soc_codec *codec;
1076
1077 /* remove the platform */
1078 if (platform && platform->probed &&
1079 platform->driver->remove_order == order) {
1080 soc_remove_platform(platform);
1081 }
1082
1083 /* remove the CODEC-side CODEC */
1084 if (codec_dai) {
1085 codec = codec_dai->codec;
1086 if (codec && codec->probed &&
1087 codec->driver->remove_order == order)
1088 soc_remove_codec(codec);
1089 }
1090
1091 /* remove any CPU-side CODEC */
1092 if (cpu_dai) {
1093 codec = cpu_dai->codec;
1094 if (codec && codec->probed &&
1095 codec->driver->remove_order == order)
1096 soc_remove_codec(codec);
1097 }
1098}
1099
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001100static void soc_remove_dai_links(struct snd_soc_card *card)
1101{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001102 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001103
Liam Girdwood0168bf02011-06-07 16:08:05 +01001104 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1105 order++) {
1106 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001107 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001108 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001109
1110 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1111 order++) {
1112 for (dai = 0; dai < card->num_rtd; dai++)
1113 soc_remove_link_components(card, dai, order);
1114 }
1115
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001116 card->num_rtd = 0;
1117}
1118
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001119static void soc_set_name_prefix(struct snd_soc_card *card,
1120 struct snd_soc_codec *codec)
1121{
1122 int i;
1123
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001124 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001125 return;
1126
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001127 for (i = 0; i < card->num_configs; i++) {
1128 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001129 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1130 codec->name_prefix = map->name_prefix;
1131 break;
1132 }
1133 }
1134}
1135
Jarkko Nikula589c3562010-12-06 16:27:07 +02001136static int soc_probe_codec(struct snd_soc_card *card,
1137 struct snd_soc_codec *codec)
1138{
1139 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001140 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001141 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001142
1143 codec->card = card;
1144 codec->dapm.card = card;
1145 soc_set_name_prefix(card, codec);
1146
Jarkko Nikula70d29332011-01-27 16:24:22 +02001147 if (!try_module_get(codec->dev->driver->owner))
1148 return -ENODEV;
1149
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001150 soc_init_codec_debugfs(codec);
1151
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001152 if (driver->dapm_widgets)
1153 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1154 driver->num_dapm_widgets);
1155
Mark Brown888df392012-02-16 19:37:51 -08001156 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001157 list_for_each_entry(dai, &codec->component.dai_list, list)
Mark Brown888df392012-02-16 19:37:51 -08001158 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
Mark Brown888df392012-02-16 19:37:51 -08001159
Mark Brown33c5f962011-08-22 18:40:30 +01001160 codec->dapm.idle_bias_off = driver->idle_bias_off;
1161
Xiubo Lia32c17b2014-03-11 12:43:22 +08001162 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1163 /* Set the default I/O up try regmap */
1164 ret = snd_soc_codec_set_cache_io(codec, NULL);
1165 if (ret < 0) {
1166 dev_err(codec->dev,
1167 "Failed to set cache I/O: %d\n", ret);
1168 goto err_probe;
1169 }
1170 }
Xiubo Life2265e2014-02-27 17:49:52 +08001171
Mark Brown89b95ac02011-03-07 16:38:44 +00001172 if (driver->probe) {
1173 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001174 if (ret < 0) {
1175 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001176 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001177 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001179 WARN(codec->dapm.idle_bias_off &&
1180 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001181 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1182 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001183 }
1184
Mark Brownb7af1da2011-04-07 19:18:44 +09001185 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001186 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001187 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001188 if (driver->dapm_routes)
1189 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1190 driver->num_dapm_routes);
1191
Jarkko Nikula589c3562010-12-06 16:27:07 +02001192 /* mark codec as probed and add to card codec list */
1193 codec->probed = 1;
1194 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001195 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001196
Jarkko Nikula70d29332011-01-27 16:24:22 +02001197 return 0;
1198
1199err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001200 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d29332011-01-27 16:24:22 +02001201 module_put(codec->dev->driver->owner);
1202
Jarkko Nikula589c3562010-12-06 16:27:07 +02001203 return ret;
1204}
1205
Liam Girdwood956245e2011-07-01 16:54:08 +01001206static int soc_probe_platform(struct snd_soc_card *card,
1207 struct snd_soc_platform *platform)
1208{
1209 int ret = 0;
1210 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001211 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001212 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001213
1214 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001215 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001216
1217 if (!try_module_get(platform->dev->driver->owner))
1218 return -ENODEV;
1219
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001220 soc_init_platform_debugfs(platform);
1221
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001222 if (driver->dapm_widgets)
1223 snd_soc_dapm_new_controls(&platform->dapm,
1224 driver->dapm_widgets, driver->num_dapm_widgets);
1225
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001226 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001227 list_for_each_entry(component, &component_list, list) {
1228 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001229 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001230 list_for_each_entry(dai, &component->dai_list, list)
1231 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001232 }
1233
Stephen Warren3fec6b62012-04-05 12:28:01 -06001234 platform->dapm.idle_bias_off = 1;
1235
Liam Girdwood956245e2011-07-01 16:54:08 +01001236 if (driver->probe) {
1237 ret = driver->probe(platform);
1238 if (ret < 0) {
1239 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001240 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001241 goto err_probe;
1242 }
1243 }
1244
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001245 if (driver->controls)
1246 snd_soc_add_platform_controls(platform, driver->controls,
1247 driver->num_controls);
1248 if (driver->dapm_routes)
1249 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1250 driver->num_dapm_routes);
1251
Liam Girdwood956245e2011-07-01 16:54:08 +01001252 /* mark platform as probed and add to card platform list */
1253 platform->probed = 1;
1254 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001255 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001256
1257 return 0;
1258
1259err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001260 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001261 module_put(platform->dev->driver->owner);
1262
1263 return ret;
1264}
1265
Mark Brown36ae1a92012-01-06 17:12:45 -08001266static void rtd_release(struct device *dev)
1267{
1268 kfree(dev);
1269}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001270
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001271static int soc_aux_dev_init(struct snd_soc_card *card,
1272 struct snd_soc_codec *codec,
1273 int num)
1274{
1275 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1276 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1277 const char *temp;
1278 int ret;
1279
1280 rtd->card = card;
1281
1282 temp = codec->name_prefix;
1283 codec->name_prefix = NULL;
1284
1285 /* do machine specific initialization */
1286 if (aux_dev->init) {
1287 ret = aux_dev->init(&codec->dapm);
1288 if (ret < 0)
1289 return ret;
1290 }
1291
1292 codec->name_prefix = temp;
1293
1294 rtd->codec = codec;
1295
1296 return 0;
1297}
1298
1299static int soc_dai_link_init(struct snd_soc_card *card,
1300 struct snd_soc_codec *codec,
1301 int num)
1302{
1303 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1304 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1305 const char *temp;
1306 int ret;
1307
1308 rtd->card = card;
1309
1310 /* machine controls, routes and widgets are not prefixed */
1311 temp = codec->name_prefix;
1312 codec->name_prefix = NULL;
1313
1314 /* do machine specific initialization */
1315 if (dai_link->init) {
1316 ret = dai_link->init(rtd);
1317 if (ret < 0)
1318 return ret;
1319 }
1320
1321 codec->name_prefix = temp;
1322
1323 rtd->codec = codec;
1324
1325 return 0;
1326}
1327
Jarkko Nikula589c3562010-12-06 16:27:07 +02001328static int soc_post_component_init(struct snd_soc_card *card,
1329 struct snd_soc_codec *codec,
1330 int num, int dailess)
1331{
1332 struct snd_soc_dai_link *dai_link = NULL;
1333 struct snd_soc_aux_dev *aux_dev = NULL;
1334 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001335 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 int ret = 0;
1337
1338 if (!dailess) {
1339 dai_link = &card->dai_link[num];
1340 rtd = &card->rtd[num];
1341 name = dai_link->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001342 ret = soc_dai_link_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001343 } else {
1344 aux_dev = &card->aux_dev[num];
1345 rtd = &card->rtd_aux[num];
1346 name = aux_dev->name;
Misael Lopez Cruz503ae5e2014-04-24 14:01:44 +02001347 ret = soc_aux_dev_init(card, codec, num);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001348 }
1349
Jarkko Nikula589c3562010-12-06 16:27:07 +02001350 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001351 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001352 return ret;
1353 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001354
Jarkko Nikula589c3562010-12-06 16:27:07 +02001355 /* register the rtd device */
Mark Brown36ae1a92012-01-06 17:12:45 -08001356 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1357 if (!rtd->dev)
1358 return -ENOMEM;
1359 device_initialize(rtd->dev);
1360 rtd->dev->parent = card->dev;
1361 rtd->dev->release = rtd_release;
1362 rtd->dev->init_name = name;
1363 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001364 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001365 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1366 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1367 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1368 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001369 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001370 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001371 /* calling put_device() here to free the rtd->dev */
1372 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001373 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001374 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001375 return ret;
1376 }
1377 rtd->dev_registered = 1;
1378
1379 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001380 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001381 if (ret < 0)
1382 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001383 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001384
1385 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001386 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001387 if (ret < 0)
1388 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001389 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001390
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001391#ifdef CONFIG_DEBUG_FS
1392 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001393 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001394 goto out;
1395
1396 ret = soc_dpcm_debugfs_add(rtd);
1397 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001398 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001399
1400out:
1401#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001402 return 0;
1403}
1404
Stephen Warren62ae68f2012-06-08 12:34:23 -06001405static int soc_probe_link_components(struct snd_soc_card *card, int num,
1406 int order)
1407{
1408 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1409 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1410 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1411 struct snd_soc_platform *platform = rtd->platform;
1412 int ret;
1413
1414 /* probe the CPU-side component, if it is a CODEC */
1415 if (cpu_dai->codec &&
1416 !cpu_dai->codec->probed &&
1417 cpu_dai->codec->driver->probe_order == order) {
1418 ret = soc_probe_codec(card, cpu_dai->codec);
1419 if (ret < 0)
1420 return ret;
1421 }
1422
1423 /* probe the CODEC-side component */
1424 if (!codec_dai->codec->probed &&
1425 codec_dai->codec->driver->probe_order == order) {
1426 ret = soc_probe_codec(card, codec_dai->codec);
1427 if (ret < 0)
1428 return ret;
1429 }
1430
1431 /* probe the platform */
1432 if (!platform->probed &&
1433 platform->driver->probe_order == order) {
1434 ret = soc_probe_platform(card, platform);
1435 if (ret < 0)
1436 return ret;
1437 }
1438
1439 return 0;
1440}
1441
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001442static int soc_probe_codec_dai(struct snd_soc_card *card,
1443 struct snd_soc_dai *codec_dai,
1444 int order)
1445{
1446 int ret;
1447
1448 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
1449 if (codec_dai->driver->probe) {
1450 ret = codec_dai->driver->probe(codec_dai);
1451 if (ret < 0) {
1452 dev_err(codec_dai->dev,
1453 "ASoC: failed to probe CODEC DAI %s: %d\n",
1454 codec_dai->name, ret);
1455 return ret;
1456 }
1457 }
1458
1459 /* mark codec_dai as probed and add to card dai list */
1460 codec_dai->probed = 1;
1461 list_add(&codec_dai->card_list, &card->dai_dev_list);
1462 }
1463
1464 return 0;
1465}
1466
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001467static int soc_link_dai_widgets(struct snd_soc_card *card,
1468 struct snd_soc_dai_link *dai_link,
1469 struct snd_soc_dai *cpu_dai,
1470 struct snd_soc_dai *codec_dai)
1471{
1472 struct snd_soc_dapm_widget *play_w, *capture_w;
1473 int ret;
1474
1475 /* link the DAI widgets */
1476 play_w = codec_dai->playback_widget;
1477 capture_w = cpu_dai->capture_widget;
1478 if (play_w && capture_w) {
1479 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1480 capture_w, play_w);
1481 if (ret != 0) {
1482 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1483 play_w->name, capture_w->name, ret);
1484 return ret;
1485 }
1486 }
1487
1488 play_w = cpu_dai->playback_widget;
1489 capture_w = codec_dai->capture_widget;
1490 if (play_w && capture_w) {
1491 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1492 capture_w, play_w);
1493 if (ret != 0) {
1494 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
1495 play_w->name, capture_w->name, ret);
1496 return ret;
1497 }
1498 }
1499
1500 return 0;
1501}
1502
Stephen Warren62ae68f2012-06-08 12:34:23 -06001503static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001504{
1505 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1506 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1507 struct snd_soc_codec *codec = rtd->codec;
1508 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001509 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1510 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001511 int ret;
1512
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001513 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001514 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001515
1516 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001517 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001518 codec_dai->card = card;
1519 cpu_dai->card = card;
1520
1521 /* set default power off timeout */
1522 rtd->pmdown_time = pmdown_time;
1523
1524 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001525 if (!cpu_dai->probed &&
1526 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001527 if (!cpu_dai->codec) {
1528 cpu_dai->dapm.card = card;
1529 if (!try_module_get(cpu_dai->dev->driver->owner))
1530 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001531
Stephen Warren18d75642012-06-08 12:34:21 -06001532 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001533 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001534
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001535 if (cpu_dai->driver->probe) {
1536 ret = cpu_dai->driver->probe(cpu_dai);
1537 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001538 dev_err(cpu_dai->dev,
1539 "ASoC: failed to probe CPU DAI %s: %d\n",
1540 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001541 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001542 return ret;
1543 }
1544 }
1545 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001546 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001547 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1548 }
1549
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001550 /* probe the CODEC DAI */
Misael Lopez Cruzb0aa88a2014-03-21 16:27:26 +01001551 ret = soc_probe_codec_dai(card, codec_dai, order);
1552 if (ret)
1553 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001554
Liam Girdwood0168bf02011-06-07 16:08:05 +01001555 /* complete DAI probe during last probe */
1556 if (order != SND_SOC_COMP_ORDER_LAST)
1557 return 0;
1558
Jarkko Nikula589c3562010-12-06 16:27:07 +02001559 ret = soc_post_component_init(card, codec, num, 0);
1560 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001561 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001562
Mark Brown36ae1a92012-01-06 17:12:45 -08001563 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001564 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001565 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1566 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001567
Namarta Kohli1245b702012-08-16 17:10:41 +05301568 if (cpu_dai->driver->compress_dai) {
1569 /*create compress_device"*/
1570 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001571 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001572 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301573 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001574 return ret;
1575 }
1576 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301577
1578 if (!dai_link->params) {
1579 /* create the pcm */
1580 ret = soc_new_pcm(rtd, num);
1581 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001582 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301583 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001584 return ret;
1585 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301586 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001587 INIT_DELAYED_WORK(&rtd->delayed_work,
1588 codec2codec_close_delayed_work);
1589
Namarta Kohli1245b702012-08-16 17:10:41 +05301590 /* link the DAI widgets */
Misael Lopez Cruz2436a722014-03-21 16:27:27 +01001591 ret = soc_link_dai_widgets(card, dai_link,
1592 cpu_dai, codec_dai);
1593 if (ret)
1594 return ret;
Mark Brownc74184e2012-04-04 22:12:09 +01001595 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001596 }
1597
1598 /* add platform data for AC97 devices */
1599 if (rtd->codec_dai->driver->ac97_control)
1600 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1601
1602 return 0;
1603}
1604
1605#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001606static int soc_register_ac97_codec(struct snd_soc_codec *codec,
1607 struct snd_soc_dai *codec_dai)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001608{
1609 int ret;
1610
1611 /* Only instantiate AC97 if not already done by the adaptor
1612 * for the generic AC97 subsystem.
1613 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001614 if (codec_dai->driver->ac97_control && !codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001615 /*
1616 * It is possible that the AC97 device is already registered to
1617 * the device subsystem. This happens when the device is created
1618 * via snd_ac97_mixer(). Currently only SoC codec that does so
1619 * is the generic AC97 glue but others migh emerge.
1620 *
1621 * In those cases we don't try to register the device again.
1622 */
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001623 if (!codec->ac97_created)
Mika Westerberg0562f782010-10-13 11:30:32 +03001624 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001625
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001626 ret = soc_ac97_dev_register(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001627 if (ret < 0) {
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001628 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001629 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001630 return ret;
1631 }
1632
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001633 codec->ac97_registered = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001634 }
1635 return 0;
1636}
1637
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001638static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1639{
1640 return soc_register_ac97_codec(rtd->codec, rtd->codec_dai);
1641}
1642
1643static void soc_unregister_ac97_codec(struct snd_soc_codec *codec)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001644{
1645 if (codec->ac97_registered) {
1646 soc_ac97_dev_unregister(codec);
1647 codec->ac97_registered = 0;
1648 }
1649}
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001650
1651static void soc_unregister_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1652{
1653 soc_unregister_ac97_codec(rtd->codec);
1654}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001655#endif
1656
Mark Brownb19e6e72012-03-14 21:18:39 +00001657static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1658{
1659 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1660 struct snd_soc_codec *codec;
1661
1662 /* find CODEC from registered CODECs*/
1663 list_for_each_entry(codec, &codec_list, list) {
1664 if (!strcmp(codec->name, aux_dev->codec_name))
1665 return 0;
1666 }
1667
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001668 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001669
Mark Brownb19e6e72012-03-14 21:18:39 +00001670 return -EPROBE_DEFER;
1671}
1672
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001673static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1674{
1675 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001676 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001677 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001678
1679 /* find CODEC from registered CODECs*/
1680 list_for_each_entry(codec, &codec_list, list) {
1681 if (!strcmp(codec->name, aux_dev->codec_name)) {
1682 if (codec->probed) {
1683 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001684 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001685 ret = -EBUSY;
1686 goto out;
1687 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001688 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001689 }
1690 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001691 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001692 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001693 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001694
Jarkko Nikula676ad982010-12-03 09:18:22 +02001695found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001696 ret = soc_probe_codec(card, codec);
1697 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001698 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001699
Jarkko Nikula589c3562010-12-06 16:27:07 +02001700 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001701
1702out:
1703 return ret;
1704}
1705
1706static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1707{
1708 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1709 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001710
1711 /* unregister the rtd device */
1712 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001713 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001714 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001715 rtd->dev_registered = 0;
1716 }
1717
Jarkko Nikula589c3562010-12-06 16:27:07 +02001718 if (codec && codec->probed)
1719 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001720}
1721
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001722static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001723{
1724 int ret;
1725
1726 if (codec->cache_init)
1727 return 0;
1728
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001729 ret = snd_soc_cache_init(codec);
1730 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001731 dev_err(codec->dev,
1732 "ASoC: Failed to set cache compression type: %d\n",
1733 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001734 return ret;
1735 }
1736 codec->cache_init = 1;
1737 return 0;
1738}
1739
Mark Brownb19e6e72012-03-14 21:18:39 +00001740static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001741{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001742 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001743 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001744 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001745
Liam Girdwood01b9d992012-03-07 10:38:25 +00001746 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001747
Mark Brownb19e6e72012-03-14 21:18:39 +00001748 /* bind DAIs */
1749 for (i = 0; i < card->num_links; i++) {
1750 ret = soc_bind_dai_link(card, i);
1751 if (ret != 0)
1752 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001753 }
1754
Mark Brownb19e6e72012-03-14 21:18:39 +00001755 /* check aux_devs too */
1756 for (i = 0; i < card->num_aux_devs; i++) {
1757 ret = soc_check_aux_dev(card, i);
1758 if (ret != 0)
1759 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001760 }
1761
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001762 /* initialize the register cache for each available codec */
1763 list_for_each_entry(codec, &codec_list, list) {
1764 if (codec->cache_init)
1765 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001766 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001767 if (ret < 0)
1768 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001769 }
1770
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001771 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001772 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001773 card->owner, 0, &card->snd_card);
1774 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001775 dev_err(card->dev,
1776 "ASoC: can't create sound card for card %s: %d\n",
1777 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001778 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001779 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001780
Mark Browne37a4972011-03-02 18:21:57 +00001781 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1782 card->dapm.dev = card->dev;
1783 card->dapm.card = card;
1784 list_add(&card->dapm.list, &card->dapm_list);
1785
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001786#ifdef CONFIG_DEBUG_FS
1787 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1788#endif
1789
Mark Brown88ee1c62011-02-02 10:43:26 +00001790#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001791 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001792 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001793#endif
Andy Green6ed25972008-06-13 16:24:05 +01001794
Mark Brown9a841eb2011-04-12 17:51:37 -07001795 if (card->dapm_widgets)
1796 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1797 card->num_dapm_widgets);
1798
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 /* initialise the sound card only once */
1800 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001801 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001802 if (ret < 0)
1803 goto card_probe_error;
1804 }
1805
Stephen Warren62ae68f2012-06-08 12:34:23 -06001806 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001807 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1808 order++) {
1809 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001810 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001811 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001812 dev_err(card->dev,
1813 "ASoC: failed to instantiate card %d\n",
1814 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001815 goto probe_dai_err;
1816 }
1817 }
1818 }
1819
1820 /* probe all DAI links on this card */
1821 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1822 order++) {
1823 for (i = 0; i < card->num_links; i++) {
1824 ret = soc_probe_link_dais(card, i, order);
1825 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001826 dev_err(card->dev,
1827 "ASoC: failed to instantiate card %d\n",
1828 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001829 goto probe_dai_err;
1830 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001831 }
1832 }
1833
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001834 for (i = 0; i < card->num_aux_devs; i++) {
1835 ret = soc_probe_aux_dev(card, i);
1836 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001837 dev_err(card->dev,
1838 "ASoC: failed to add auxiliary devices %d\n",
1839 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001840 goto probe_aux_dev_err;
1841 }
1842 }
1843
Mark Brown888df392012-02-16 19:37:51 -08001844 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001845 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001846
Mark Brownb7af1da2011-04-07 19:18:44 +09001847 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001848 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001849
Mark Brownb8ad29d2011-03-02 18:35:51 +00001850 if (card->dapm_routes)
1851 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1852 card->num_dapm_routes);
1853
Mark Brown75d9ac42011-09-27 16:41:01 +01001854 for (i = 0; i < card->num_links; i++) {
1855 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001856 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001857
Mark Brownf04209a2012-04-09 16:29:21 +01001858 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001859 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001860 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001861 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001862 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001863 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001864 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001865 }
1866
1867 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001868 if (dai_fmt &&
1869 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001870 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1871 dai_fmt);
1872 if (ret != 0 && ret != -ENOTSUPP)
1873 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001874 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001875 ret);
1876 } else if (dai_fmt) {
1877 /* Flip the polarity for the "CPU" end */
1878 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1879 switch (dai_link->dai_fmt &
1880 SND_SOC_DAIFMT_MASTER_MASK) {
1881 case SND_SOC_DAIFMT_CBM_CFM:
1882 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1883 break;
1884 case SND_SOC_DAIFMT_CBM_CFS:
1885 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1886 break;
1887 case SND_SOC_DAIFMT_CBS_CFM:
1888 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1889 break;
1890 case SND_SOC_DAIFMT_CBS_CFS:
1891 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1892 break;
1893 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001894
1895 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001896 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001897 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001898 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001899 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001900 ret);
1901 }
1902 }
1903
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001904 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001905 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001906 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1907 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001908 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1909 "%s", card->driver_name ? card->driver_name : card->name);
1910 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1911 switch (card->snd_card->driver[i]) {
1912 case '_':
1913 case '-':
1914 case '\0':
1915 break;
1916 default:
1917 if (!isalnum(card->snd_card->driver[i]))
1918 card->snd_card->driver[i] = '_';
1919 break;
1920 }
1921 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001922
Mark Brown28e9ad92011-03-02 18:36:34 +00001923 if (card->late_probe) {
1924 ret = card->late_probe(card);
1925 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001926 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001927 card->name, ret);
1928 goto probe_aux_dev_err;
1929 }
1930 }
1931
Stephen Warren16332812011-11-23 12:42:04 -07001932 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001933 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001934 snd_soc_dapm_auto_nc_codec_pins(codec);
1935
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001936 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001937
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001938 ret = snd_card_register(card->snd_card);
1939 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001940 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1941 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001942 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001943 }
1944
1945#ifdef CONFIG_SND_SOC_AC97_BUS
1946 /* register any AC97 codecs */
1947 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001948 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1949 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001950 dev_err(card->dev,
1951 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001952 while (--i >= 0)
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01001953 soc_unregister_ac97_dai_link(&card->rtd[i]);
Axel Lin6b3ed782010-12-07 16:12:29 +08001954 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001955 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001956 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001957#endif
1958
Mark Brown435c5e22008-12-04 15:32:53 +00001959 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001960 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001961 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001962
1963 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001964
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001965probe_aux_dev_err:
1966 for (i = 0; i < card->num_aux_devs; i++)
1967 soc_remove_aux_dev(card, i);
1968
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001969probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001970 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001971
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001972card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001973 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001974 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001975
1976 snd_card_free(card->snd_card);
1977
Mark Brownb19e6e72012-03-14 21:18:39 +00001978base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001979 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001980
Mark Brownb19e6e72012-03-14 21:18:39 +00001981 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001982}
1983
1984/* probes a new socdev */
1985static int soc_probe(struct platform_device *pdev)
1986{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001987 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001988
Vinod Koul70a7ca32011-01-14 19:22:48 +05301989 /*
1990 * no card, so machine driver should be registering card
1991 * we should not be here in that case so ret error
1992 */
1993 if (!card)
1994 return -EINVAL;
1995
Mark Brownfe4085e2012-03-02 13:07:41 +00001996 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001997 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001998 card->name);
1999
Mark Brown435c5e22008-12-04 15:32:53 +00002000 /* Bodge while we unpick instantiation */
2001 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002002
Mark Brown28d528c2012-08-09 18:45:23 +01002003 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002004}
2005
Vinod Koulb0e26482011-01-13 22:48:02 +05302006static int soc_cleanup_card_resources(struct snd_soc_card *card)
2007{
Vinod Koulb0e26482011-01-13 22:48:02 +05302008 int i;
2009
2010 /* make sure any delayed work runs */
2011 for (i = 0; i < card->num_rtd; i++) {
2012 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002013 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05302014 }
2015
2016 /* remove auxiliary devices */
2017 for (i = 0; i < card->num_aux_devs; i++)
2018 soc_remove_aux_dev(card, i);
2019
2020 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09002021 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302022
2023 soc_cleanup_card_debugfs(card);
2024
2025 /* remove the card */
2026 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00002027 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05302028
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02002029 snd_soc_dapm_free(&card->dapm);
2030
Vinod Koulb0e26482011-01-13 22:48:02 +05302031 snd_card_free(card->snd_card);
2032 return 0;
2033
2034}
2035
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002036/* removes a socdev */
2037static int soc_remove(struct platform_device *pdev)
2038{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002039 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002040
Mark Brownc5af3a22008-11-28 13:29:45 +00002041 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002042 return 0;
2043}
2044
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002045int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01002046{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002047 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002048 int i;
Mark Brown51737472009-06-22 13:16:51 +01002049
2050 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01002051 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002052
2053 /* Flush out pmdown_time work - we actually do want to run it
2054 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002055 for (i = 0; i < card->num_rtd; i++) {
2056 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07002057 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002058 }
Mark Brown51737472009-06-22 13:16:51 +01002059
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002060 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01002061
Nicolin Chen988e8cc2013-11-04 14:57:31 +08002062 /* deactivate pins to sleep state */
2063 for (i = 0; i < card->num_rtd; i++) {
2064 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
2065 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
2066 pinctrl_pm_select_sleep_state(codec_dai->dev);
2067 pinctrl_pm_select_sleep_state(cpu_dai->dev);
2068 }
2069
Mark Brown416356f2009-06-30 19:05:15 +01002070 return 0;
Mark Brown51737472009-06-22 13:16:51 +01002071}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002072EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01002073
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002074const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302075 .suspend = snd_soc_suspend,
2076 .resume = snd_soc_resume,
2077 .freeze = snd_soc_suspend,
2078 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002079 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05302080 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01002081};
Stephen Warrendeb26072011-04-05 19:35:30 -06002082EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01002083
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002084/* ASoC platform driver */
2085static struct platform_driver soc_driver = {
2086 .driver = {
2087 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02002088 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00002089 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002090 },
2091 .probe = soc_probe,
2092 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002093};
2094
Mark Brown096e49d2009-07-05 15:12:22 +01002095/**
2096 * snd_soc_codec_volatile_register: Report if a register is volatile.
2097 *
2098 * @codec: CODEC to query.
2099 * @reg: Register to query.
2100 *
2101 * Boolean function indiciating if a CODEC register is volatile.
2102 */
Mark Brown181e0552011-01-24 14:05:25 +00002103int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
2104 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01002105{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002106 if (codec->volatile_register)
2107 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002108 else
2109 return 0;
2110}
2111EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2112
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002113/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002114 * snd_soc_codec_readable_register: Report if a register is readable.
2115 *
2116 * @codec: CODEC to query.
2117 * @reg: Register to query.
2118 *
2119 * Boolean function indicating if a CODEC register is readable.
2120 */
2121int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2122 unsigned int reg)
2123{
2124 if (codec->readable_register)
2125 return codec->readable_register(codec, reg);
2126 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002127 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002128}
2129EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2130
2131/**
2132 * snd_soc_codec_writable_register: Report if a register is writable.
2133 *
2134 * @codec: CODEC to query.
2135 * @reg: Register to query.
2136 *
2137 * Boolean function indicating if a CODEC register is writable.
2138 */
2139int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2140 unsigned int reg)
2141{
2142 if (codec->writable_register)
2143 return codec->writable_register(codec, reg);
2144 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002145 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002146}
2147EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2148
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002149int snd_soc_platform_read(struct snd_soc_platform *platform,
2150 unsigned int reg)
2151{
2152 unsigned int ret;
2153
2154 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002155 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002156 return -1;
2157 }
2158
2159 ret = platform->driver->read(platform, reg);
2160 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002161 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002162
2163 return ret;
2164}
2165EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2166
2167int snd_soc_platform_write(struct snd_soc_platform *platform,
2168 unsigned int reg, unsigned int val)
2169{
2170 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002171 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002172 return -1;
2173 }
2174
2175 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002176 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002177 return platform->driver->write(platform, reg, val);
2178}
2179EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2180
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002181/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002182 * snd_soc_new_ac97_codec - initailise AC97 device
2183 * @codec: audio codec
2184 * @ops: AC97 bus operations
2185 * @num: AC97 codec number
2186 *
2187 * Initialises AC97 codec resources for use by ad-hoc devices only.
2188 */
2189int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2190 struct snd_ac97_bus_ops *ops, int num)
2191{
2192 mutex_lock(&codec->mutex);
2193
2194 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2195 if (codec->ac97 == NULL) {
2196 mutex_unlock(&codec->mutex);
2197 return -ENOMEM;
2198 }
2199
2200 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2201 if (codec->ac97->bus == NULL) {
2202 kfree(codec->ac97);
2203 codec->ac97 = NULL;
2204 mutex_unlock(&codec->mutex);
2205 return -ENOMEM;
2206 }
2207
2208 codec->ac97->bus->ops = ops;
2209 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002210
2211 /*
2212 * Mark the AC97 device to be created by us. This way we ensure that the
2213 * device will be registered with the device subsystem later on.
2214 */
2215 codec->ac97_created = 1;
2216
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002217 mutex_unlock(&codec->mutex);
2218 return 0;
2219}
2220EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2221
Markus Pargmann741a5092013-08-19 17:05:55 +02002222static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2223
2224static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2225{
2226 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2227
2228 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2229
2230 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2231
2232 udelay(10);
2233
2234 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2235
2236 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2237 msleep(2);
2238}
2239
2240static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2241{
2242 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2243
2244 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2245
2246 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2247 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2248 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2249
2250 udelay(10);
2251
2252 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2253
2254 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2255 msleep(2);
2256}
2257
2258static int snd_soc_ac97_parse_pinctl(struct device *dev,
2259 struct snd_ac97_reset_cfg *cfg)
2260{
2261 struct pinctrl *p;
2262 struct pinctrl_state *state;
2263 int gpio;
2264 int ret;
2265
2266 p = devm_pinctrl_get(dev);
2267 if (IS_ERR(p)) {
2268 dev_err(dev, "Failed to get pinctrl\n");
2269 return PTR_RET(p);
2270 }
2271 cfg->pctl = p;
2272
2273 state = pinctrl_lookup_state(p, "ac97-reset");
2274 if (IS_ERR(state)) {
2275 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2276 return PTR_RET(state);
2277 }
2278 cfg->pstate_reset = state;
2279
2280 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2281 if (IS_ERR(state)) {
2282 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2283 return PTR_RET(state);
2284 }
2285 cfg->pstate_warm_reset = state;
2286
2287 state = pinctrl_lookup_state(p, "ac97-running");
2288 if (IS_ERR(state)) {
2289 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2290 return PTR_RET(state);
2291 }
2292 cfg->pstate_run = state;
2293
2294 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2295 if (gpio < 0) {
2296 dev_err(dev, "Can't find ac97-sync gpio\n");
2297 return gpio;
2298 }
2299 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2300 if (ret) {
2301 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2302 return ret;
2303 }
2304 cfg->gpio_sync = gpio;
2305
2306 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2307 if (gpio < 0) {
2308 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2309 return gpio;
2310 }
2311 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2312 if (ret) {
2313 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2314 return ret;
2315 }
2316 cfg->gpio_sdata = gpio;
2317
2318 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2319 if (gpio < 0) {
2320 dev_err(dev, "Can't find ac97-reset gpio\n");
2321 return gpio;
2322 }
2323 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2324 if (ret) {
2325 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2326 return ret;
2327 }
2328 cfg->gpio_reset = gpio;
2329
2330 return 0;
2331}
2332
Mark Brownb047e1c2013-06-26 12:45:59 +01002333struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002334EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002335
2336int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2337{
2338 if (ops == soc_ac97_ops)
2339 return 0;
2340
2341 if (soc_ac97_ops && ops)
2342 return -EBUSY;
2343
2344 soc_ac97_ops = ops;
2345
2346 return 0;
2347}
2348EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2349
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002351 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2352 *
2353 * This function sets the reset and warm_reset properties of ops and parses
2354 * the device node of pdev to get pinctrl states and gpio numbers to use.
2355 */
2356int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2357 struct platform_device *pdev)
2358{
2359 struct device *dev = &pdev->dev;
2360 struct snd_ac97_reset_cfg cfg;
2361 int ret;
2362
2363 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2364 if (ret)
2365 return ret;
2366
2367 ret = snd_soc_set_ac97_ops(ops);
2368 if (ret)
2369 return ret;
2370
2371 ops->warm_reset = snd_soc_ac97_warm_reset;
2372 ops->reset = snd_soc_ac97_reset;
2373
2374 snd_ac97_rst_cfg = cfg;
2375 return 0;
2376}
2377EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2378
2379/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 * snd_soc_free_ac97_codec - free AC97 codec device
2381 * @codec: audio codec
2382 *
2383 * Frees AC97 codec device resources.
2384 */
2385void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2386{
2387 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002388#ifdef CONFIG_SND_SOC_AC97_BUS
Misael Lopez Cruz02c9c7b2014-03-21 16:27:28 +01002389 soc_unregister_ac97_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002390#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391 kfree(codec->ac97->bus);
2392 kfree(codec->ac97);
2393 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002394 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002395 mutex_unlock(&codec->mutex);
2396}
2397EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2398
Mark Brownc3753702010-11-01 15:41:57 -04002399unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2400{
2401 unsigned int ret;
2402
Mark Brownc3acec22010-12-02 16:15:29 +00002403 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002404 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002405 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002406
2407 return ret;
2408}
2409EXPORT_SYMBOL_GPL(snd_soc_read);
2410
2411unsigned int snd_soc_write(struct snd_soc_codec *codec,
2412 unsigned int reg, unsigned int val)
2413{
2414 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002415 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002416 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002417}
2418EXPORT_SYMBOL_GPL(snd_soc_write);
2419
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002420/**
2421 * snd_soc_update_bits - update codec register bits
2422 * @codec: audio codec
2423 * @reg: codec register
2424 * @mask: register mask
2425 * @value: new value
2426 *
2427 * Writes new register value.
2428 *
Timur Tabi180c3292011-01-10 15:58:13 -06002429 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002430 */
2431int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002432 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002433{
Mark Brown8a713da2011-12-03 12:33:55 +00002434 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002435 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002436 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002437
Mark Brown8a713da2011-12-03 12:33:55 +00002438 if (codec->using_regmap) {
2439 ret = regmap_update_bits_check(codec->control_data, reg,
2440 mask, value, &change);
2441 } else {
2442 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002443 if (ret < 0)
2444 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002445
2446 old = ret;
2447 new = (old & ~mask) | (value & mask);
2448 change = old != new;
2449 if (change)
2450 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002451 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002452
Mark Brown8a713da2011-12-03 12:33:55 +00002453 if (ret < 0)
2454 return ret;
2455
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002456 return change;
2457}
2458EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2459
2460/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002461 * snd_soc_update_bits_locked - update codec register bits
2462 * @codec: audio codec
2463 * @reg: codec register
2464 * @mask: register mask
2465 * @value: new value
2466 *
2467 * Writes new register value, and takes the codec mutex.
2468 *
2469 * Returns 1 for change else 0.
2470 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002471int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2472 unsigned short reg, unsigned int mask,
2473 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002474{
2475 int change;
2476
2477 mutex_lock(&codec->mutex);
2478 change = snd_soc_update_bits(codec, reg, mask, value);
2479 mutex_unlock(&codec->mutex);
2480
2481 return change;
2482}
Mark Browndd1b3d52009-12-04 14:22:03 +00002483EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002484
2485/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002486 * snd_soc_test_bits - test register for change
2487 * @codec: audio codec
2488 * @reg: codec register
2489 * @mask: register mask
2490 * @value: new value
2491 *
2492 * Tests a register with a new value and checks if the new value is
2493 * different from the old value.
2494 *
2495 * Returns 1 for change else 0.
2496 */
2497int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002498 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002499{
2500 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002501 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002502
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002503 old = snd_soc_read(codec, reg);
2504 new = (old & ~mask) | value;
2505 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002506
2507 return change;
2508}
2509EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2510
2511/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002512 * snd_soc_cnew - create new control
2513 * @_template: control template
2514 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002515 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002516 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002517 *
2518 * Create a new mixer control from a template control.
2519 *
2520 * Returns 0 for success, else error.
2521 */
2522struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002523 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002524 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002525{
2526 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002527 struct snd_kcontrol *kcontrol;
2528 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529
2530 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002531 template.index = 0;
2532
Mark Brownefb7ac32011-03-08 17:23:24 +00002533 if (!long_name)
2534 long_name = template.name;
2535
2536 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002537 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002538 if (!name)
2539 return NULL;
2540
Mark Brownefb7ac32011-03-08 17:23:24 +00002541 template.name = name;
2542 } else {
2543 template.name = long_name;
2544 }
2545
2546 kcontrol = snd_ctl_new1(&template, data);
2547
2548 kfree(name);
2549
2550 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002551}
2552EXPORT_SYMBOL_GPL(snd_soc_cnew);
2553
Liam Girdwood022658b2012-02-03 17:43:09 +00002554static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2555 const struct snd_kcontrol_new *controls, int num_controls,
2556 const char *prefix, void *data)
2557{
2558 int err, i;
2559
2560 for (i = 0; i < num_controls; i++) {
2561 const struct snd_kcontrol_new *control = &controls[i];
2562 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2563 control->name, prefix));
2564 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002565 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2566 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002567 return err;
2568 }
2569 }
2570
2571 return 0;
2572}
2573
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002574struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2575 const char *name)
2576{
2577 struct snd_card *card = soc_card->snd_card;
2578 struct snd_kcontrol *kctl;
2579
2580 if (unlikely(!name))
2581 return NULL;
2582
2583 list_for_each_entry(kctl, &card->controls, list)
2584 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2585 return kctl;
2586 return NULL;
2587}
2588EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2589
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002590/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002591 * snd_soc_add_codec_controls - add an array of controls to a codec.
2592 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002593 * duplicating this code.
2594 *
2595 * @codec: codec to add controls to
2596 * @controls: array of controls to add
2597 * @num_controls: number of elements in the array
2598 *
2599 * Return 0 for success, else error.
2600 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002601int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002602 const struct snd_kcontrol_new *controls, int num_controls)
2603{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002604 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002605
Liam Girdwood022658b2012-02-03 17:43:09 +00002606 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2607 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002608}
Liam Girdwood022658b2012-02-03 17:43:09 +00002609EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002610
2611/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002612 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002613 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002614 *
2615 * @platform: platform to add controls to
2616 * @controls: array of controls to add
2617 * @num_controls: number of elements in the array
2618 *
2619 * Return 0 for success, else error.
2620 */
2621int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2622 const struct snd_kcontrol_new *controls, int num_controls)
2623{
2624 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002625
Liam Girdwood022658b2012-02-03 17:43:09 +00002626 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2627 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002628}
2629EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2630
2631/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002632 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2633 * Convenience function to add a list of controls.
2634 *
2635 * @soc_card: SoC card to add controls to
2636 * @controls: array of controls to add
2637 * @num_controls: number of elements in the array
2638 *
2639 * Return 0 for success, else error.
2640 */
2641int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2642 const struct snd_kcontrol_new *controls, int num_controls)
2643{
2644 struct snd_card *card = soc_card->snd_card;
2645
2646 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2647 NULL, soc_card);
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2650
2651/**
2652 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2653 * Convienience function to add a list of controls.
2654 *
2655 * @dai: DAI to add controls to
2656 * @controls: array of controls to add
2657 * @num_controls: number of elements in the array
2658 *
2659 * Return 0 for success, else error.
2660 */
2661int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2662 const struct snd_kcontrol_new *controls, int num_controls)
2663{
2664 struct snd_card *card = dai->card->snd_card;
2665
2666 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2667 NULL, dai);
2668}
2669EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2670
2671/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002672 * snd_soc_info_enum_double - enumerated double mixer info callback
2673 * @kcontrol: mixer control
2674 * @uinfo: control element information
2675 *
2676 * Callback to provide information about a double enumerated
2677 * mixer control.
2678 *
2679 * Returns 0 for success.
2680 */
2681int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2682 struct snd_ctl_elem_info *uinfo)
2683{
2684 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2685
2686 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2687 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002688 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002689
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002690 if (uinfo->value.enumerated.item >= e->items)
2691 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002692 strlcpy(uinfo->value.enumerated.name,
2693 e->texts[uinfo->value.enumerated.item],
2694 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002695 return 0;
2696}
2697EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2698
2699/**
2700 * snd_soc_get_enum_double - enumerated double mixer get callback
2701 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002702 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002703 *
2704 * Callback to get the value of a double enumerated mixer.
2705 *
2706 * Returns 0 for success.
2707 */
2708int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2709 struct snd_ctl_elem_value *ucontrol)
2710{
2711 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2712 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002713 unsigned int val, item;
2714 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002716 reg_val = snd_soc_read(codec, e->reg);
2717 val = (reg_val >> e->shift_l) & e->mask;
2718 item = snd_soc_enum_val_to_item(e, val);
2719 ucontrol->value.enumerated.item[0] = item;
2720 if (e->shift_l != e->shift_r) {
2721 val = (reg_val >> e->shift_l) & e->mask;
2722 item = snd_soc_enum_val_to_item(e, val);
2723 ucontrol->value.enumerated.item[1] = item;
2724 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002725
2726 return 0;
2727}
2728EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2729
2730/**
2731 * snd_soc_put_enum_double - enumerated double mixer put callback
2732 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002733 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002734 *
2735 * Callback to set the value of a double enumerated mixer.
2736 *
2737 * Returns 0 for success.
2738 */
2739int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2740 struct snd_ctl_elem_value *ucontrol)
2741{
2742 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2743 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002744 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002745 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002746 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002748 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002749 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002750 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002751 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002752 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002753 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002754 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002755 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002756 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002757 }
2758
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002759 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002760}
2761EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2762
2763/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002764 * snd_soc_read_signed - Read a codec register and interprete as signed value
2765 * @codec: codec
2766 * @reg: Register to read
2767 * @mask: Mask to use after shifting the register value
2768 * @shift: Right shift of register value
2769 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002770 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002771 * This functions reads a codec register. The register value is shifted right
2772 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2773 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002774 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002775 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002776 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002777static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2778 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002779{
Markus Pargmannf227b882014-01-16 16:02:10 +01002780 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002781 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002782
Markus Pargmannf227b882014-01-16 16:02:10 +01002783 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002784
Markus Pargmannf227b882014-01-16 16:02:10 +01002785 if (!sign_bit)
2786 return val;
2787
2788 /* non-negative number */
2789 if (!(val & BIT(sign_bit)))
2790 return val;
2791
2792 ret = val;
2793
2794 /*
2795 * The register most probably does not contain a full-sized int.
2796 * Instead we have an arbitrary number of bits in a signed
2797 * representation which has to be translated into a full-sized int.
2798 * This is done by filling up all bits above the sign-bit.
2799 */
2800 ret |= ~((int)(BIT(sign_bit) - 1));
2801
2802 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002803}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002804
2805/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002806 * snd_soc_info_volsw - single mixer info callback
2807 * @kcontrol: mixer control
2808 * @uinfo: control element information
2809 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002810 * Callback to provide information about a single mixer control, or a double
2811 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002812 *
2813 * Returns 0 for success.
2814 */
2815int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2816 struct snd_ctl_elem_info *uinfo)
2817{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002818 struct soc_mixer_control *mc =
2819 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002820 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002821
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002822 if (!mc->platform_max)
2823 mc->platform_max = mc->max;
2824 platform_max = mc->platform_max;
2825
2826 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002827 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2828 else
2829 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2830
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002831 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002832 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002833 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2837
2838/**
2839 * snd_soc_get_volsw - single mixer get callback
2840 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002841 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002842 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002843 * Callback to get the value of a single mixer control, or a double mixer
2844 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002845 *
2846 * Returns 0 for success.
2847 */
2848int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2849 struct snd_ctl_elem_value *ucontrol)
2850{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002851 struct soc_mixer_control *mc =
2852 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002853 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002854 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002855 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002856 unsigned int shift = mc->shift;
2857 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002858 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002859 int min = mc->min;
2860 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002861 unsigned int mask = (1 << fls(max)) - 1;
2862 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002863
Markus Pargmannf227b882014-01-16 16:02:10 +01002864 if (sign_bit)
2865 mask = BIT(sign_bit + 1) - 1;
2866
2867 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2868 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002869 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002870 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002871 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002872
2873 if (snd_soc_volsw_is_stereo(mc)) {
2874 if (reg == reg2)
2875 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002876 snd_soc_read_signed(codec, reg, mask, rshift,
2877 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002878 else
2879 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002880 snd_soc_read_signed(codec, reg2, mask, shift,
2881 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002882 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002883 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002884 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002885 }
2886
2887 return 0;
2888}
2889EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2890
2891/**
2892 * snd_soc_put_volsw - single mixer put callback
2893 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002894 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002895 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002896 * Callback to set the value of a single mixer control, or a double mixer
2897 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002898 *
2899 * Returns 0 for success.
2900 */
2901int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2902 struct snd_ctl_elem_value *ucontrol)
2903{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002904 struct soc_mixer_control *mc =
2905 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002906 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002907 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002908 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002909 unsigned int shift = mc->shift;
2910 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002911 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002912 int min = mc->min;
2913 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002914 unsigned int mask = (1 << fls(max)) - 1;
2915 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002916 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002917 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002918 unsigned int val2 = 0;
2919 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002920
Markus Pargmannf227b882014-01-16 16:02:10 +01002921 if (sign_bit)
2922 mask = BIT(sign_bit + 1) - 1;
2923
2924 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002925 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002926 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002927 val_mask = mask << shift;
2928 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002929 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002930 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002931 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002932 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002933 if (reg == reg2) {
2934 val_mask |= mask << rshift;
2935 val |= val2 << rshift;
2936 } else {
2937 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002938 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002939 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002940 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002941 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002942 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002943 return err;
2944
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002945 if (type_2r)
2946 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2947
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002948 return err;
2949}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002950EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002951
Mark Browne13ac2e2008-05-28 17:58:05 +01002952/**
Brian Austin1d99f242012-03-30 10:43:55 -05002953 * snd_soc_get_volsw_sx - single mixer get callback
2954 * @kcontrol: mixer control
2955 * @ucontrol: control element information
2956 *
2957 * Callback to get the value of a single mixer control, or a double mixer
2958 * control that spans 2 registers.
2959 *
2960 * Returns 0 for success.
2961 */
2962int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2963 struct snd_ctl_elem_value *ucontrol)
2964{
2965 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2966 struct soc_mixer_control *mc =
2967 (struct soc_mixer_control *)kcontrol->private_value;
2968
2969 unsigned int reg = mc->reg;
2970 unsigned int reg2 = mc->rreg;
2971 unsigned int shift = mc->shift;
2972 unsigned int rshift = mc->rshift;
2973 int max = mc->max;
2974 int min = mc->min;
2975 int mask = (1 << (fls(min + max) - 1)) - 1;
2976
2977 ucontrol->value.integer.value[0] =
2978 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2979
2980 if (snd_soc_volsw_is_stereo(mc))
2981 ucontrol->value.integer.value[1] =
2982 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2983
2984 return 0;
2985}
2986EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2987
2988/**
2989 * snd_soc_put_volsw_sx - double mixer set callback
2990 * @kcontrol: mixer control
2991 * @uinfo: control element information
2992 *
2993 * Callback to set the value of a double mixer control that spans 2 registers.
2994 *
2995 * Returns 0 for success.
2996 */
2997int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2998 struct snd_ctl_elem_value *ucontrol)
2999{
3000 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3001 struct soc_mixer_control *mc =
3002 (struct soc_mixer_control *)kcontrol->private_value;
3003
3004 unsigned int reg = mc->reg;
3005 unsigned int reg2 = mc->rreg;
3006 unsigned int shift = mc->shift;
3007 unsigned int rshift = mc->rshift;
3008 int max = mc->max;
3009 int min = mc->min;
3010 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05003011 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05003012 unsigned short val, val_mask, val2 = 0;
3013
3014 val_mask = mask << shift;
3015 val = (ucontrol->value.integer.value[0] + min) & mask;
3016 val = val << shift;
3017
Mukund Navadad0558522012-11-09 11:53:40 +05303018 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
3019 if (err < 0)
3020 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05003021
3022 if (snd_soc_volsw_is_stereo(mc)) {
3023 val_mask = mask << rshift;
3024 val2 = (ucontrol->value.integer.value[1] + min) & mask;
3025 val2 = val2 << rshift;
3026
3027 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
3028 return err;
3029 }
3030 return 0;
3031}
3032EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
3033
3034/**
Mark Browne13ac2e2008-05-28 17:58:05 +01003035 * snd_soc_info_volsw_s8 - signed mixer info callback
3036 * @kcontrol: mixer control
3037 * @uinfo: control element information
3038 *
3039 * Callback to provide information about a signed mixer control.
3040 *
3041 * Returns 0 for success.
3042 */
3043int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
3044 struct snd_ctl_elem_info *uinfo)
3045{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003046 struct soc_mixer_control *mc =
3047 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003048 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003049 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003050
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003051 if (!mc->platform_max)
3052 mc->platform_max = mc->max;
3053 platform_max = mc->platform_max;
3054
Mark Browne13ac2e2008-05-28 17:58:05 +01003055 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3056 uinfo->count = 2;
3057 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003058 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003059 return 0;
3060}
3061EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
3062
3063/**
3064 * snd_soc_get_volsw_s8 - signed mixer get callback
3065 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003066 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003067 *
3068 * Callback to get the value of a signed mixer control.
3069 *
3070 * Returns 0 for success.
3071 */
3072int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
3073 struct snd_ctl_elem_value *ucontrol)
3074{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003075 struct soc_mixer_control *mc =
3076 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003077 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003078 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003079 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01003080 int val = snd_soc_read(codec, reg);
3081
3082 ucontrol->value.integer.value[0] =
3083 ((signed char)(val & 0xff))-min;
3084 ucontrol->value.integer.value[1] =
3085 ((signed char)((val >> 8) & 0xff))-min;
3086 return 0;
3087}
3088EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
3089
3090/**
3091 * snd_soc_put_volsw_sgn - signed mixer put callback
3092 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00003093 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01003094 *
3095 * Callback to set the value of a signed mixer control.
3096 *
3097 * Returns 0 for success.
3098 */
3099int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
3100 struct snd_ctl_elem_value *ucontrol)
3101{
Jon Smirl4eaa9812008-07-29 11:42:26 +01003102 struct soc_mixer_control *mc =
3103 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01003104 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04003105 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003106 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003107 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01003108
3109 val = (ucontrol->value.integer.value[0]+min) & 0xff;
3110 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3111
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003112 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01003113}
3114EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3115
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003116/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003117 * snd_soc_info_volsw_range - single mixer info callback with range.
3118 * @kcontrol: mixer control
3119 * @uinfo: control element information
3120 *
3121 * Callback to provide information, within a range, about a single
3122 * mixer control.
3123 *
3124 * returns 0 for success.
3125 */
3126int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
3127 struct snd_ctl_elem_info *uinfo)
3128{
3129 struct soc_mixer_control *mc =
3130 (struct soc_mixer_control *)kcontrol->private_value;
3131 int platform_max;
3132 int min = mc->min;
3133
3134 if (!mc->platform_max)
3135 mc->platform_max = mc->max;
3136 platform_max = mc->platform_max;
3137
3138 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00003139 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003140 uinfo->value.integer.min = 0;
3141 uinfo->value.integer.max = platform_max - min;
3142
3143 return 0;
3144}
3145EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3146
3147/**
3148 * snd_soc_put_volsw_range - single mixer put value callback with range.
3149 * @kcontrol: mixer control
3150 * @ucontrol: control element information
3151 *
3152 * Callback to set the value, within a range, for a single mixer control.
3153 *
3154 * Returns 0 for success.
3155 */
3156int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3157 struct snd_ctl_elem_value *ucontrol)
3158{
3159 struct soc_mixer_control *mc =
3160 (struct soc_mixer_control *)kcontrol->private_value;
3161 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3162 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003163 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003164 unsigned int shift = mc->shift;
3165 int min = mc->min;
3166 int max = mc->max;
3167 unsigned int mask = (1 << fls(max)) - 1;
3168 unsigned int invert = mc->invert;
3169 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003170 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003171
3172 val = ((ucontrol->value.integer.value[0] + min) & mask);
3173 if (invert)
3174 val = max - val;
3175 val_mask = mask << shift;
3176 val = val << shift;
3177
Mark Brown9bde4f02012-12-19 16:05:00 +00003178 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003179 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003180 return ret;
3181
3182 if (snd_soc_volsw_is_stereo(mc)) {
3183 val = ((ucontrol->value.integer.value[1] + min) & mask);
3184 if (invert)
3185 val = max - val;
3186 val_mask = mask << shift;
3187 val = val << shift;
3188
3189 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
3190 }
3191
3192 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003193}
3194EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3195
3196/**
3197 * snd_soc_get_volsw_range - single mixer get callback with range
3198 * @kcontrol: mixer control
3199 * @ucontrol: control element information
3200 *
3201 * Callback to get the value, within a range, of a single mixer control.
3202 *
3203 * Returns 0 for success.
3204 */
3205int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3206 struct snd_ctl_elem_value *ucontrol)
3207{
3208 struct soc_mixer_control *mc =
3209 (struct soc_mixer_control *)kcontrol->private_value;
3210 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3211 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003212 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003213 unsigned int shift = mc->shift;
3214 int min = mc->min;
3215 int max = mc->max;
3216 unsigned int mask = (1 << fls(max)) - 1;
3217 unsigned int invert = mc->invert;
3218
3219 ucontrol->value.integer.value[0] =
3220 (snd_soc_read(codec, reg) >> shift) & mask;
3221 if (invert)
3222 ucontrol->value.integer.value[0] =
3223 max - ucontrol->value.integer.value[0];
3224 ucontrol->value.integer.value[0] =
3225 ucontrol->value.integer.value[0] - min;
3226
Mark Brown9bde4f02012-12-19 16:05:00 +00003227 if (snd_soc_volsw_is_stereo(mc)) {
3228 ucontrol->value.integer.value[1] =
3229 (snd_soc_read(codec, rreg) >> shift) & mask;
3230 if (invert)
3231 ucontrol->value.integer.value[1] =
3232 max - ucontrol->value.integer.value[1];
3233 ucontrol->value.integer.value[1] =
3234 ucontrol->value.integer.value[1] - min;
3235 }
3236
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003237 return 0;
3238}
3239EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3240
3241/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003242 * snd_soc_limit_volume - Set new limit to an existing volume control.
3243 *
3244 * @codec: where to look for the control
3245 * @name: Name of the control
3246 * @max: new maximum limit
3247 *
3248 * Return 0 for success, else error.
3249 */
3250int snd_soc_limit_volume(struct snd_soc_codec *codec,
3251 const char *name, int max)
3252{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003253 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003254 struct snd_kcontrol *kctl;
3255 struct soc_mixer_control *mc;
3256 int found = 0;
3257 int ret = -EINVAL;
3258
3259 /* Sanity check for name and max */
3260 if (unlikely(!name || max <= 0))
3261 return -EINVAL;
3262
3263 list_for_each_entry(kctl, &card->controls, list) {
3264 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3265 found = 1;
3266 break;
3267 }
3268 }
3269 if (found) {
3270 mc = (struct soc_mixer_control *)kctl->private_value;
3271 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003272 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003273 ret = 0;
3274 }
3275 }
3276 return ret;
3277}
3278EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3279
Mark Brown71d08512011-10-10 18:31:26 +01003280int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3281 struct snd_ctl_elem_info *uinfo)
3282{
3283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3284 struct soc_bytes *params = (void *)kcontrol->private_value;
3285
3286 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3287 uinfo->count = params->num_regs * codec->val_bytes;
3288
3289 return 0;
3290}
3291EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3292
3293int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3294 struct snd_ctl_elem_value *ucontrol)
3295{
3296 struct soc_bytes *params = (void *)kcontrol->private_value;
3297 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3298 int ret;
3299
3300 if (codec->using_regmap)
3301 ret = regmap_raw_read(codec->control_data, params->base,
3302 ucontrol->value.bytes.data,
3303 params->num_regs * codec->val_bytes);
3304 else
3305 ret = -EINVAL;
3306
Mark Brownf831b052012-02-17 16:20:33 -08003307 /* Hide any masked bytes to ensure consistent data reporting */
3308 if (ret == 0 && params->mask) {
3309 switch (codec->val_bytes) {
3310 case 1:
3311 ucontrol->value.bytes.data[0] &= ~params->mask;
3312 break;
3313 case 2:
3314 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003315 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003316 break;
3317 case 4:
3318 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003319 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003320 break;
3321 default:
3322 return -EINVAL;
3323 }
3324 }
3325
Mark Brown71d08512011-10-10 18:31:26 +01003326 return ret;
3327}
3328EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3329
3330int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3331 struct snd_ctl_elem_value *ucontrol)
3332{
3333 struct soc_bytes *params = (void *)kcontrol->private_value;
3334 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003335 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003336 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003337 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003338
Mark Brownf831b052012-02-17 16:20:33 -08003339 if (!codec->using_regmap)
3340 return -EINVAL;
3341
Mark Brownf831b052012-02-17 16:20:33 -08003342 len = params->num_regs * codec->val_bytes;
3343
Mark Brownb5a8fe42013-01-20 21:42:22 +09003344 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3345 if (!data)
3346 return -ENOMEM;
3347
Mark Brownf831b052012-02-17 16:20:33 -08003348 /*
3349 * If we've got a mask then we need to preserve the register
3350 * bits. We shouldn't modify the incoming data so take a
3351 * copy.
3352 */
3353 if (params->mask) {
3354 ret = regmap_read(codec->control_data, params->base, &val);
3355 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003356 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003357
3358 val &= params->mask;
3359
Mark Brownf831b052012-02-17 16:20:33 -08003360 switch (codec->val_bytes) {
3361 case 1:
3362 ((u8 *)data)[0] &= ~params->mask;
3363 ((u8 *)data)[0] |= val;
3364 break;
3365 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003366 mask = ~params->mask;
3367 ret = regmap_parse_val(codec->control_data,
3368 &mask, &mask);
3369 if (ret != 0)
3370 goto out;
3371
3372 ((u16 *)data)[0] &= mask;
3373
3374 ret = regmap_parse_val(codec->control_data,
3375 &val, &val);
3376 if (ret != 0)
3377 goto out;
3378
3379 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003380 break;
3381 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003382 mask = ~params->mask;
3383 ret = regmap_parse_val(codec->control_data,
3384 &mask, &mask);
3385 if (ret != 0)
3386 goto out;
3387
3388 ((u32 *)data)[0] &= mask;
3389
3390 ret = regmap_parse_val(codec->control_data,
3391 &val, &val);
3392 if (ret != 0)
3393 goto out;
3394
3395 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003396 break;
3397 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003398 ret = -EINVAL;
3399 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003400 }
3401 }
3402
3403 ret = regmap_raw_write(codec->control_data, params->base,
3404 data, len);
3405
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003406out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003407 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003408
3409 return ret;
3410}
3411EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3412
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003413/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003414 * snd_soc_info_xr_sx - signed multi register info callback
3415 * @kcontrol: mreg control
3416 * @uinfo: control element information
3417 *
3418 * Callback to provide information of a control that can
3419 * span multiple codec registers which together
3420 * forms a single signed value in a MSB/LSB manner.
3421 *
3422 * Returns 0 for success.
3423 */
3424int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3425 struct snd_ctl_elem_info *uinfo)
3426{
3427 struct soc_mreg_control *mc =
3428 (struct soc_mreg_control *)kcontrol->private_value;
3429 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3430 uinfo->count = 1;
3431 uinfo->value.integer.min = mc->min;
3432 uinfo->value.integer.max = mc->max;
3433
3434 return 0;
3435}
3436EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3437
3438/**
3439 * snd_soc_get_xr_sx - signed multi register get callback
3440 * @kcontrol: mreg control
3441 * @ucontrol: control element information
3442 *
3443 * Callback to get the value of a control that can span
3444 * multiple codec registers which together forms a single
3445 * signed value in a MSB/LSB manner. The control supports
3446 * specifying total no of bits used to allow for bitfields
3447 * across the multiple codec registers.
3448 *
3449 * Returns 0 for success.
3450 */
3451int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3452 struct snd_ctl_elem_value *ucontrol)
3453{
3454 struct soc_mreg_control *mc =
3455 (struct soc_mreg_control *)kcontrol->private_value;
3456 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3457 unsigned int regbase = mc->regbase;
3458 unsigned int regcount = mc->regcount;
3459 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3460 unsigned int regwmask = (1<<regwshift)-1;
3461 unsigned int invert = mc->invert;
3462 unsigned long mask = (1UL<<mc->nbits)-1;
3463 long min = mc->min;
3464 long max = mc->max;
3465 long val = 0;
3466 unsigned long regval;
3467 unsigned int i;
3468
3469 for (i = 0; i < regcount; i++) {
3470 regval = snd_soc_read(codec, regbase+i) & regwmask;
3471 val |= regval << (regwshift*(regcount-i-1));
3472 }
3473 val &= mask;
3474 if (min < 0 && val > max)
3475 val |= ~mask;
3476 if (invert)
3477 val = max - val;
3478 ucontrol->value.integer.value[0] = val;
3479
3480 return 0;
3481}
3482EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3483
3484/**
3485 * snd_soc_put_xr_sx - signed multi register get callback
3486 * @kcontrol: mreg control
3487 * @ucontrol: control element information
3488 *
3489 * Callback to set the value of a control that can span
3490 * multiple codec registers which together forms a single
3491 * signed value in a MSB/LSB manner. The control supports
3492 * specifying total no of bits used to allow for bitfields
3493 * across the multiple codec registers.
3494 *
3495 * Returns 0 for success.
3496 */
3497int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3498 struct snd_ctl_elem_value *ucontrol)
3499{
3500 struct soc_mreg_control *mc =
3501 (struct soc_mreg_control *)kcontrol->private_value;
3502 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3503 unsigned int regbase = mc->regbase;
3504 unsigned int regcount = mc->regcount;
3505 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3506 unsigned int regwmask = (1<<regwshift)-1;
3507 unsigned int invert = mc->invert;
3508 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003509 long max = mc->max;
3510 long val = ucontrol->value.integer.value[0];
3511 unsigned int i, regval, regmask;
3512 int err;
3513
3514 if (invert)
3515 val = max - val;
3516 val &= mask;
3517 for (i = 0; i < regcount; i++) {
3518 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3519 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3520 err = snd_soc_update_bits_locked(codec, regbase+i,
3521 regmask, regval);
3522 if (err < 0)
3523 return err;
3524 }
3525
3526 return 0;
3527}
3528EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3529
3530/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003531 * snd_soc_get_strobe - strobe get callback
3532 * @kcontrol: mixer control
3533 * @ucontrol: control element information
3534 *
3535 * Callback get the value of a strobe mixer control.
3536 *
3537 * Returns 0 for success.
3538 */
3539int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3540 struct snd_ctl_elem_value *ucontrol)
3541{
3542 struct soc_mixer_control *mc =
3543 (struct soc_mixer_control *)kcontrol->private_value;
3544 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3545 unsigned int reg = mc->reg;
3546 unsigned int shift = mc->shift;
3547 unsigned int mask = 1 << shift;
3548 unsigned int invert = mc->invert != 0;
3549 unsigned int val = snd_soc_read(codec, reg) & mask;
3550
3551 if (shift != 0 && val != 0)
3552 val = val >> shift;
3553 ucontrol->value.enumerated.item[0] = val ^ invert;
3554
3555 return 0;
3556}
3557EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3558
3559/**
3560 * snd_soc_put_strobe - strobe put callback
3561 * @kcontrol: mixer control
3562 * @ucontrol: control element information
3563 *
3564 * Callback strobe a register bit to high then low (or the inverse)
3565 * in one pass of a single mixer enum control.
3566 *
3567 * Returns 1 for success.
3568 */
3569int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3570 struct snd_ctl_elem_value *ucontrol)
3571{
3572 struct soc_mixer_control *mc =
3573 (struct soc_mixer_control *)kcontrol->private_value;
3574 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3575 unsigned int reg = mc->reg;
3576 unsigned int shift = mc->shift;
3577 unsigned int mask = 1 << shift;
3578 unsigned int invert = mc->invert != 0;
3579 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3580 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3581 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3582 int err;
3583
3584 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3585 if (err < 0)
3586 return err;
3587
3588 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3589 return err;
3590}
3591EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3592
3593/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003594 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3595 * @dai: DAI
3596 * @clk_id: DAI specific clock ID
3597 * @freq: new clock frequency in Hz
3598 * @dir: new clock direction - input/output.
3599 *
3600 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3601 */
3602int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3603 unsigned int freq, int dir)
3604{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003605 if (dai->driver && dai->driver->ops->set_sysclk)
3606 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003607 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003608 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003609 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003610 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003611 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003612}
3613EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3614
3615/**
Mark Brownec4ee522011-03-07 20:58:11 +00003616 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3617 * @codec: CODEC
3618 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003619 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003620 * @freq: new clock frequency in Hz
3621 * @dir: new clock direction - input/output.
3622 *
3623 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3624 */
3625int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003626 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003627{
3628 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003629 return codec->driver->set_sysclk(codec, clk_id, source,
3630 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003631 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003632 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003633}
3634EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3635
3636/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003637 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3638 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003639 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003640 * @div: new clock divisor.
3641 *
3642 * Configures the clock dividers. This is used to derive the best DAI bit and
3643 * frame clocks from the system or master clock. It's best to set the DAI bit
3644 * and frame clocks as low as possible to save system power.
3645 */
3646int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3647 int div_id, int div)
3648{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003649 if (dai->driver && dai->driver->ops->set_clkdiv)
3650 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003651 else
3652 return -EINVAL;
3653}
3654EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3655
3656/**
3657 * snd_soc_dai_set_pll - configure DAI PLL.
3658 * @dai: DAI
3659 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003660 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003661 * @freq_in: PLL input clock frequency in Hz
3662 * @freq_out: requested PLL output clock frequency in Hz
3663 *
3664 * Configures and enables PLL to generate output clock based on input clock.
3665 */
Mark Brown85488032009-09-05 18:52:16 +01003666int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3667 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003668{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003669 if (dai->driver && dai->driver->ops->set_pll)
3670 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003671 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003672 else if (dai->codec && dai->codec->driver->set_pll)
3673 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3674 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003675 else
3676 return -EINVAL;
3677}
3678EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3679
Mark Brownec4ee522011-03-07 20:58:11 +00003680/*
3681 * snd_soc_codec_set_pll - configure codec PLL.
3682 * @codec: CODEC
3683 * @pll_id: DAI specific PLL ID
3684 * @source: DAI specific source for the PLL
3685 * @freq_in: PLL input clock frequency in Hz
3686 * @freq_out: requested PLL output clock frequency in Hz
3687 *
3688 * Configures and enables PLL to generate output clock based on input clock.
3689 */
3690int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3691 unsigned int freq_in, unsigned int freq_out)
3692{
3693 if (codec->driver->set_pll)
3694 return codec->driver->set_pll(codec, pll_id, source,
3695 freq_in, freq_out);
3696 else
3697 return -EINVAL;
3698}
3699EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3700
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003701/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003702 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3703 * @dai: DAI
3704 * @ratio Ratio of BCLK to Sample rate.
3705 *
3706 * Configures the DAI for a preset BCLK to sample rate ratio.
3707 */
3708int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3709{
3710 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3711 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3712 else
3713 return -EINVAL;
3714}
3715EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3716
3717/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003718 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3719 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003720 * @fmt: SND_SOC_DAIFMT_ format value.
3721 *
3722 * Configures the DAI hardware format and clocking.
3723 */
3724int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3725{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003726 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003727 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003728 if (dai->driver->ops->set_fmt == NULL)
3729 return -ENOTSUPP;
3730 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003731}
3732EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3733
3734/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003735 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003736 * @slots: Number of slots in use.
3737 * @tx_mask: bitmask representing active TX slots.
3738 * @rx_mask: bitmask representing active RX slots.
3739 *
3740 * Generates the TDM tx and rx slot default masks for DAI.
3741 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003742static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003743 unsigned int *tx_mask,
3744 unsigned int *rx_mask)
3745{
3746 if (*tx_mask || *rx_mask)
3747 return 0;
3748
3749 if (!slots)
3750 return -EINVAL;
3751
3752 *tx_mask = (1 << slots) - 1;
3753 *rx_mask = (1 << slots) - 1;
3754
3755 return 0;
3756}
3757
3758/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003759 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3760 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003761 * @tx_mask: bitmask representing active TX slots.
3762 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003763 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003764 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003765 *
3766 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3767 * specific.
3768 */
3769int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003770 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003771{
Xiubo Lie5c21512014-03-21 14:17:12 +08003772 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3773 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003774 &tx_mask, &rx_mask);
3775 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003776 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003777
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003778 if (dai->driver && dai->driver->ops->set_tdm_slot)
3779 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003780 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003781 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003782 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003783}
3784EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3785
3786/**
Barry Song472df3c2009-09-12 01:16:29 +08003787 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3788 * @dai: DAI
3789 * @tx_num: how many TX channels
3790 * @tx_slot: pointer to an array which imply the TX slot number channel
3791 * 0~num-1 uses
3792 * @rx_num: how many RX channels
3793 * @rx_slot: pointer to an array which imply the RX slot number channel
3794 * 0~num-1 uses
3795 *
3796 * configure the relationship between channel number and TDM slot number.
3797 */
3798int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3799 unsigned int tx_num, unsigned int *tx_slot,
3800 unsigned int rx_num, unsigned int *rx_slot)
3801{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003802 if (dai->driver && dai->driver->ops->set_channel_map)
3803 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003804 rx_num, rx_slot);
3805 else
3806 return -EINVAL;
3807}
3808EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3809
3810/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003811 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3812 * @dai: DAI
3813 * @tristate: tristate enable
3814 *
3815 * Tristates the DAI so that others can use it.
3816 */
3817int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3818{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003819 if (dai->driver && dai->driver->ops->set_tristate)
3820 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003821 else
3822 return -EINVAL;
3823}
3824EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3825
3826/**
3827 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3828 * @dai: DAI
3829 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003830 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003831 *
3832 * Mutes the DAI DAC.
3833 */
Mark Brownda183962013-02-06 15:44:07 +00003834int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3835 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003836{
Mark Brownda183962013-02-06 15:44:07 +00003837 if (!dai->driver)
3838 return -ENOTSUPP;
3839
3840 if (dai->driver->ops->mute_stream)
3841 return dai->driver->ops->mute_stream(dai, mute, direction);
3842 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3843 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003844 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003845 else
Mark Brown04570c62012-04-13 19:16:03 +01003846 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003847}
3848EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3849
Mark Brownc5af3a22008-11-28 13:29:45 +00003850/**
3851 * snd_soc_register_card - Register a card with the ASoC core
3852 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003853 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003854 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003855 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303856int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003857{
Mark Brownb19e6e72012-03-14 21:18:39 +00003858 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003859
Mark Brownc5af3a22008-11-28 13:29:45 +00003860 if (!card->name || !card->dev)
3861 return -EINVAL;
3862
Stephen Warren5a504962011-12-21 10:40:59 -07003863 for (i = 0; i < card->num_links; i++) {
3864 struct snd_soc_dai_link *link = &card->dai_link[i];
3865
3866 /*
3867 * Codec must be specified by 1 of name or OF node,
3868 * not both or neither.
3869 */
3870 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003871 dev_err(card->dev,
3872 "ASoC: Neither/both codec name/of_node are set for %s\n",
3873 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003874 return -EINVAL;
3875 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003876 /* Codec DAI name must be specified */
3877 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003878 dev_err(card->dev,
3879 "ASoC: codec_dai_name not set for %s\n",
3880 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003881 return -EINVAL;
3882 }
Stephen Warren5a504962011-12-21 10:40:59 -07003883
3884 /*
3885 * Platform may be specified by either name or OF node, but
3886 * can be left unspecified, and a dummy platform will be used.
3887 */
3888 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003889 dev_err(card->dev,
3890 "ASoC: Both platform name/of_node are set for %s\n",
3891 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003892 return -EINVAL;
3893 }
3894
3895 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003896 * CPU device may be specified by either name or OF node, but
3897 * can be left unspecified, and will be matched based on DAI
3898 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003899 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003900 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003901 dev_err(card->dev,
3902 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3903 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003904 return -EINVAL;
3905 }
3906 /*
3907 * At least one of CPU DAI name or CPU device name/node must be
3908 * specified
3909 */
3910 if (!link->cpu_dai_name &&
3911 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003912 dev_err(card->dev,
3913 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3914 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003915 return -EINVAL;
3916 }
3917 }
3918
Mark Browned77cc12011-05-03 18:25:34 +01003919 dev_set_drvdata(card->dev, card);
3920
Stephen Warren111c6412011-01-28 14:26:35 -07003921 snd_soc_initialize_card_lists(card);
3922
Vinod Koul150dd2f2011-01-13 22:48:32 +05303923 soc_init_card_debugfs(card);
3924
Mark Brown181a6892012-03-14 20:18:49 +00003925 card->rtd = devm_kzalloc(card->dev,
3926 sizeof(struct snd_soc_pcm_runtime) *
3927 (card->num_links + card->num_aux_devs),
3928 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003929 if (card->rtd == NULL)
3930 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003931 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003932 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003933
3934 for (i = 0; i < card->num_links; i++)
3935 card->rtd[i].dai_link = &card->dai_link[i];
3936
Mark Brownc5af3a22008-11-28 13:29:45 +00003937 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003938 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003939 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003940 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003941 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003942
Mark Brownb19e6e72012-03-14 21:18:39 +00003943 ret = snd_soc_instantiate_card(card);
3944 if (ret != 0)
3945 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003946
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003947 /* deactivate pins to sleep state */
3948 for (i = 0; i < card->num_rtd; i++) {
3949 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3950 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3951 if (!codec_dai->active)
3952 pinctrl_pm_select_sleep_state(codec_dai->dev);
3953 if (!cpu_dai->active)
3954 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3955 }
3956
Mark Brownb19e6e72012-03-14 21:18:39 +00003957 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003958}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303959EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003960
3961/**
3962 * snd_soc_unregister_card - Unregister a card with the ASoC core
3963 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003964 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003965 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003966 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303967int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003968{
Vinod Koulb0e26482011-01-13 22:48:02 +05303969 if (card->instantiated)
3970 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003971 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003972
3973 return 0;
3974}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303975EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003976
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003977/*
3978 * Simplify DAI link configuration by removing ".-1" from device names
3979 * and sanitizing names.
3980 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003981static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003982{
3983 char *found, name[NAME_SIZE];
3984 int id1, id2;
3985
3986 if (dev_name(dev) == NULL)
3987 return NULL;
3988
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003989 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003990
3991 /* are we a "%s.%d" name (platform and SPI components) */
3992 found = strstr(name, dev->driver->name);
3993 if (found) {
3994 /* get ID */
3995 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3996
3997 /* discard ID from name if ID == -1 */
3998 if (*id == -1)
3999 found[strlen(dev->driver->name)] = '\0';
4000 }
4001
4002 } else {
4003 /* I2C component devices are named "bus-addr" */
4004 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
4005 char tmp[NAME_SIZE];
4006
4007 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03004008 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004009
4010 /* sanitize component name for DAI link creation */
4011 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00004012 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004013 } else
4014 *id = 0;
4015 }
4016
4017 return kstrdup(name, GFP_KERNEL);
4018}
4019
4020/*
4021 * Simplify DAI link naming for single devices with multiple DAIs by removing
4022 * any ".-1" and using the DAI name (instead of device name).
4023 */
4024static inline char *fmt_multiple_name(struct device *dev,
4025 struct snd_soc_dai_driver *dai_drv)
4026{
4027 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004028 dev_err(dev,
4029 "ASoC: error - multiple DAI %s registered with no name\n",
4030 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004031 return NULL;
4032 }
4033
4034 return kstrdup(dai_drv->name, GFP_KERNEL);
4035}
4036
Mark Brown91151712008-11-30 23:31:24 +00004037/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004038 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00004039 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004040 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00004041 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004042static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00004043{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01004044 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00004045
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01004046 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004047 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
4048 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01004049 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004050 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004051 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004052 }
Mark Brown91151712008-11-30 23:31:24 +00004053}
Mark Brown91151712008-11-30 23:31:24 +00004054
4055/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004056 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00004057 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004058 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004059 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
4060 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004061 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00004062 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004063 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
4064 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00004065 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004066static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004067 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
4068 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00004069{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004070 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004071 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004072 unsigned int i;
4073 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004074
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004075 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00004076
4077 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004078
4079 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08004080 if (dai == NULL) {
4081 ret = -ENOMEM;
4082 goto err;
4083 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004084
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004085 /*
4086 * Back in the old days when we still had component-less DAIs,
4087 * instead of having a static name, component-less DAIs would
4088 * inherit the name of the parent device so it is possible to
4089 * register multiple instances of the DAI. We still need to keep
4090 * the same naming style even though those DAIs are not
4091 * component-less anymore.
4092 */
4093 if (count == 1 && legacy_dai_naming) {
4094 dai->name = fmt_single_name(dev, &dai->id);
4095 } else {
4096 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
4097 if (dai_drv[i].id)
4098 dai->id = dai_drv[i].id;
4099 else
4100 dai->id = i;
4101 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004102 if (dai->name == NULL) {
4103 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004104 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00004105 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004106 }
4107
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004108 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004109 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004110 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004111 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00004112 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004113 if (!dai->driver->ops)
4114 dai->driver->ops = &null_dai_ops;
4115
Peter Ujfalusi5f800082012-08-07 10:24:13 +03004116 if (!dai->codec)
4117 dai->dapm.idle_bias_off = 1;
4118
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004119 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004120
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004121 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004122 }
4123
4124 return 0;
4125
4126err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004127 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004128
4129 return ret;
4130}
Mark Brown91151712008-11-30 23:31:24 +00004131
4132/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004133 * snd_soc_register_component - Register a component with the ASoC core
4134 *
4135 */
4136static int
4137__snd_soc_register_component(struct device *dev,
4138 struct snd_soc_component *cmpnt,
4139 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004140 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004141 struct snd_soc_dai_driver *dai_drv,
4142 int num_dai, bool allow_single_dai)
4143{
4144 int ret;
4145
4146 dev_dbg(dev, "component register %s\n", dev_name(dev));
4147
4148 if (!cmpnt) {
4149 dev_err(dev, "ASoC: Failed to connecting component\n");
4150 return -ENOMEM;
4151 }
4152
4153 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4154 if (!cmpnt->name) {
4155 dev_err(dev, "ASoC: Failed to simplifying name\n");
4156 return -ENOMEM;
4157 }
4158
4159 cmpnt->dev = dev;
4160 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004161 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004162 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004163 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004164
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004165 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4166 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004167 if (ret < 0) {
4168 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4169 goto error_component_name;
4170 }
4171
4172 mutex_lock(&client_mutex);
4173 list_add(&cmpnt->list, &component_list);
4174 mutex_unlock(&client_mutex);
4175
4176 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4177
4178 return ret;
4179
4180error_component_name:
4181 kfree(cmpnt->name);
4182
4183 return ret;
4184}
4185
4186int snd_soc_register_component(struct device *dev,
4187 const struct snd_soc_component_driver *cmpnt_drv,
4188 struct snd_soc_dai_driver *dai_drv,
4189 int num_dai)
4190{
4191 struct snd_soc_component *cmpnt;
4192
4193 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4194 if (!cmpnt) {
4195 dev_err(dev, "ASoC: Failed to allocate memory\n");
4196 return -ENOMEM;
4197 }
4198
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004199 cmpnt->ignore_pmdown_time = true;
4200
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004201 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004202 dai_drv, num_dai, true);
4203}
4204EXPORT_SYMBOL_GPL(snd_soc_register_component);
4205
4206/**
4207 * snd_soc_unregister_component - Unregister a component from the ASoC core
4208 *
4209 */
4210void snd_soc_unregister_component(struct device *dev)
4211{
4212 struct snd_soc_component *cmpnt;
4213
4214 list_for_each_entry(cmpnt, &component_list, list) {
4215 if (dev == cmpnt->dev)
4216 goto found;
4217 }
4218 return;
4219
4220found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004221 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004222
4223 mutex_lock(&client_mutex);
4224 list_del(&cmpnt->list);
4225 mutex_unlock(&client_mutex);
4226
4227 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4228 kfree(cmpnt->name);
4229}
4230EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4231
4232/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004233 * snd_soc_add_platform - Add a platform to the ASoC core
4234 * @dev: The parent device for the platform
4235 * @platform: The platform to add
4236 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004237 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004238int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004239 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004240{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004241 /* create platform component name */
4242 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03004243 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004244 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004245
4246 platform->dev = dev;
4247 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01004248 platform->dapm.dev = dev;
4249 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004250 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00004251 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004252
4253 mutex_lock(&client_mutex);
4254 list_add(&platform->list, &platform_list);
4255 mutex_unlock(&client_mutex);
4256
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004257 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004258
4259 return 0;
4260}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004261EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4262
4263/**
4264 * snd_soc_register_platform - Register a platform with the ASoC core
4265 *
4266 * @platform: platform to register
4267 */
4268int snd_soc_register_platform(struct device *dev,
4269 const struct snd_soc_platform_driver *platform_drv)
4270{
4271 struct snd_soc_platform *platform;
4272 int ret;
4273
4274 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4275
4276 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4277 if (platform == NULL)
4278 return -ENOMEM;
4279
4280 ret = snd_soc_add_platform(dev, platform, platform_drv);
4281 if (ret)
4282 kfree(platform);
4283
4284 return ret;
4285}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004286EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4287
4288/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004289 * snd_soc_remove_platform - Remove a platform from the ASoC core
4290 * @platform: the platform to remove
4291 */
4292void snd_soc_remove_platform(struct snd_soc_platform *platform)
4293{
4294 mutex_lock(&client_mutex);
4295 list_del(&platform->list);
4296 mutex_unlock(&client_mutex);
4297
4298 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4299 platform->name);
4300 kfree(platform->name);
4301}
4302EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4303
4304struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4305{
4306 struct snd_soc_platform *platform;
4307
4308 list_for_each_entry(platform, &platform_list, list) {
4309 if (dev == platform->dev)
4310 return platform;
4311 }
4312
4313 return NULL;
4314}
4315EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4316
4317/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004318 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4319 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004320 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004321 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004322void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004323{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004324 struct snd_soc_platform *platform;
4325
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004326 platform = snd_soc_lookup_platform(dev);
4327 if (!platform)
4328 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004329
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004330 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004331 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004332}
4333EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4334
Mark Brown151ab222009-05-09 16:22:58 +01004335static u64 codec_format_map[] = {
4336 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4337 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4338 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4339 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4340 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4341 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4342 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4343 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4344 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4345 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4346 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4347 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4348 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4349 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4350 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4351 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4352};
4353
4354/* Fix up the DAI formats for endianness: codecs don't actually see
4355 * the endianness of the data but we're using the CPU format
4356 * definitions which do need to include endianness so we ensure that
4357 * codec DAIs always have both big and little endian variants set.
4358 */
4359static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4360{
4361 int i;
4362
4363 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4364 if (stream->formats & codec_format_map[i])
4365 stream->formats |= codec_format_map[i];
4366}
4367
Mark Brown0d0cf002008-12-10 14:32:45 +00004368/**
4369 * snd_soc_register_codec - Register a codec with the ASoC core
4370 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004371 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004372 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004373int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004374 const struct snd_soc_codec_driver *codec_drv,
4375 struct snd_soc_dai_driver *dai_drv,
4376 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004377{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004378 struct snd_soc_codec *codec;
4379 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004380
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004381 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004382
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004383 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4384 if (codec == NULL)
4385 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004386
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004387 /* create CODEC component name */
4388 codec->name = fmt_single_name(dev, &codec->id);
4389 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004390 ret = -ENOMEM;
4391 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004392 }
4393
Mark Brownc3acec22010-12-02 16:15:29 +00004394 codec->write = codec_drv->write;
4395 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004396 codec->volatile_register = codec_drv->volatile_register;
4397 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004398 codec->writable_register = codec_drv->writable_register;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004399 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004400 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4401 codec->dapm.dev = dev;
4402 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004403 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004404 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004405 codec->dev = dev;
4406 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004407 codec->num_dai = num_dai;
4408 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004409
4410 for (i = 0; i < num_dai; i++) {
4411 fixup_codec_formats(&dai_drv[i].playback);
4412 fixup_codec_formats(&dai_drv[i].capture);
4413 }
4414
Mark Brown054880f2012-04-09 17:29:19 +01004415 mutex_lock(&client_mutex);
4416 list_add(&codec->list, &codec_list);
4417 mutex_unlock(&client_mutex);
4418
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004419 /* register component */
4420 ret = __snd_soc_register_component(dev, &codec->component,
4421 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004422 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004423 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004424 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004425 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004426 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004427
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004428 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004429 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004430
Kuninori Morimotof790b942013-02-25 00:40:09 -08004431fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004432 mutex_lock(&client_mutex);
4433 list_del(&codec->list);
4434 mutex_unlock(&client_mutex);
4435
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004436 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004437fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004438 kfree(codec);
4439 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004440}
4441EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4442
4443/**
4444 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4445 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004446 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004447 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004448void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004449{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004450 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004451
4452 list_for_each_entry(codec, &codec_list, list) {
4453 if (dev == codec->dev)
4454 goto found;
4455 }
4456 return;
4457
4458found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004459 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004460
Mark Brown0d0cf002008-12-10 14:32:45 +00004461 mutex_lock(&client_mutex);
4462 list_del(&codec->list);
4463 mutex_unlock(&client_mutex);
4464
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004465 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004466
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004467 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004468 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004469 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004470}
4471EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4472
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004473/* Retrieve a card's name from device tree */
4474int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4475 const char *propname)
4476{
4477 struct device_node *np = card->dev->of_node;
4478 int ret;
4479
4480 ret = of_property_read_string_index(np, propname, 0, &card->name);
4481 /*
4482 * EINVAL means the property does not exist. This is fine providing
4483 * card->name was previously set, which is checked later in
4484 * snd_soc_register_card.
4485 */
4486 if (ret < 0 && ret != -EINVAL) {
4487 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004488 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004489 propname, ret);
4490 return ret;
4491 }
4492
4493 return 0;
4494}
4495EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4496
Xiubo Li9a6d4862014-02-08 15:59:52 +08004497static const struct snd_soc_dapm_widget simple_widgets[] = {
4498 SND_SOC_DAPM_MIC("Microphone", NULL),
4499 SND_SOC_DAPM_LINE("Line", NULL),
4500 SND_SOC_DAPM_HP("Headphone", NULL),
4501 SND_SOC_DAPM_SPK("Speaker", NULL),
4502};
4503
4504int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4505 const char *propname)
4506{
4507 struct device_node *np = card->dev->of_node;
4508 struct snd_soc_dapm_widget *widgets;
4509 const char *template, *wname;
4510 int i, j, num_widgets, ret;
4511
4512 num_widgets = of_property_count_strings(np, propname);
4513 if (num_widgets < 0) {
4514 dev_err(card->dev,
4515 "ASoC: Property '%s' does not exist\n", propname);
4516 return -EINVAL;
4517 }
4518 if (num_widgets & 1) {
4519 dev_err(card->dev,
4520 "ASoC: Property '%s' length is not even\n", propname);
4521 return -EINVAL;
4522 }
4523
4524 num_widgets /= 2;
4525 if (!num_widgets) {
4526 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4527 propname);
4528 return -EINVAL;
4529 }
4530
4531 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4532 GFP_KERNEL);
4533 if (!widgets) {
4534 dev_err(card->dev,
4535 "ASoC: Could not allocate memory for widgets\n");
4536 return -ENOMEM;
4537 }
4538
4539 for (i = 0; i < num_widgets; i++) {
4540 ret = of_property_read_string_index(np, propname,
4541 2 * i, &template);
4542 if (ret) {
4543 dev_err(card->dev,
4544 "ASoC: Property '%s' index %d read error:%d\n",
4545 propname, 2 * i, ret);
4546 return -EINVAL;
4547 }
4548
4549 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4550 if (!strncmp(template, simple_widgets[j].name,
4551 strlen(simple_widgets[j].name))) {
4552 widgets[i] = simple_widgets[j];
4553 break;
4554 }
4555 }
4556
4557 if (j >= ARRAY_SIZE(simple_widgets)) {
4558 dev_err(card->dev,
4559 "ASoC: DAPM widget '%s' is not supported\n",
4560 template);
4561 return -EINVAL;
4562 }
4563
4564 ret = of_property_read_string_index(np, propname,
4565 (2 * i) + 1,
4566 &wname);
4567 if (ret) {
4568 dev_err(card->dev,
4569 "ASoC: Property '%s' index %d read error:%d\n",
4570 propname, (2 * i) + 1, ret);
4571 return -EINVAL;
4572 }
4573
4574 widgets[i].name = wname;
4575 }
4576
4577 card->dapm_widgets = widgets;
4578 card->num_dapm_widgets = num_widgets;
4579
4580 return 0;
4581}
4582EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4583
Xiubo Li89c67852014-02-14 09:34:35 +08004584int snd_soc_of_parse_tdm_slot(struct device_node *np,
4585 unsigned int *slots,
4586 unsigned int *slot_width)
4587{
4588 u32 val;
4589 int ret;
4590
4591 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4592 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4593 if (ret)
4594 return ret;
4595
4596 if (slots)
4597 *slots = val;
4598 }
4599
4600 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4601 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4602 if (ret)
4603 return ret;
4604
4605 if (slot_width)
4606 *slot_width = val;
4607 }
4608
4609 return 0;
4610}
4611EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4612
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004613int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4614 const char *propname)
4615{
4616 struct device_node *np = card->dev->of_node;
4617 int num_routes;
4618 struct snd_soc_dapm_route *routes;
4619 int i, ret;
4620
4621 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004622 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004623 dev_err(card->dev,
4624 "ASoC: Property '%s' does not exist or its length is not even\n",
4625 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004626 return -EINVAL;
4627 }
4628 num_routes /= 2;
4629 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004630 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004631 propname);
4632 return -EINVAL;
4633 }
4634
4635 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4636 GFP_KERNEL);
4637 if (!routes) {
4638 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004639 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004640 return -EINVAL;
4641 }
4642
4643 for (i = 0; i < num_routes; i++) {
4644 ret = of_property_read_string_index(np, propname,
4645 2 * i, &routes[i].sink);
4646 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004647 dev_err(card->dev,
4648 "ASoC: Property '%s' index %d could not be read: %d\n",
4649 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004650 return -EINVAL;
4651 }
4652 ret = of_property_read_string_index(np, propname,
4653 (2 * i) + 1, &routes[i].source);
4654 if (ret) {
4655 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004656 "ASoC: Property '%s' index %d could not be read: %d\n",
4657 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004658 return -EINVAL;
4659 }
4660 }
4661
4662 card->num_dapm_routes = num_routes;
4663 card->dapm_routes = routes;
4664
4665 return 0;
4666}
4667EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4668
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004669unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4670 const char *prefix)
4671{
4672 int ret, i;
4673 char prop[128];
4674 unsigned int format = 0;
4675 int bit, frame;
4676 const char *str;
4677 struct {
4678 char *name;
4679 unsigned int val;
4680 } of_fmt_table[] = {
4681 { "i2s", SND_SOC_DAIFMT_I2S },
4682 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4683 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4684 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4685 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4686 { "ac97", SND_SOC_DAIFMT_AC97 },
4687 { "pdm", SND_SOC_DAIFMT_PDM},
4688 { "msb", SND_SOC_DAIFMT_MSB },
4689 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004690 };
4691
4692 if (!prefix)
4693 prefix = "";
4694
4695 /*
4696 * check "[prefix]format = xxx"
4697 * SND_SOC_DAIFMT_FORMAT_MASK area
4698 */
4699 snprintf(prop, sizeof(prop), "%sformat", prefix);
4700 ret = of_property_read_string(np, prop, &str);
4701 if (ret == 0) {
4702 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4703 if (strcmp(str, of_fmt_table[i].name) == 0) {
4704 format |= of_fmt_table[i].val;
4705 break;
4706 }
4707 }
4708 }
4709
4710 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004711 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004712 * SND_SOC_DAIFMT_CLOCK_MASK area
4713 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004714 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4715 if (of_get_property(np, prop, NULL))
4716 format |= SND_SOC_DAIFMT_CONT;
4717 else
4718 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004719
4720 /*
4721 * check "[prefix]bitclock-inversion"
4722 * check "[prefix]frame-inversion"
4723 * SND_SOC_DAIFMT_INV_MASK area
4724 */
4725 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4726 bit = !!of_get_property(np, prop, NULL);
4727
4728 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4729 frame = !!of_get_property(np, prop, NULL);
4730
4731 switch ((bit << 4) + frame) {
4732 case 0x11:
4733 format |= SND_SOC_DAIFMT_IB_IF;
4734 break;
4735 case 0x10:
4736 format |= SND_SOC_DAIFMT_IB_NF;
4737 break;
4738 case 0x01:
4739 format |= SND_SOC_DAIFMT_NB_IF;
4740 break;
4741 default:
4742 /* SND_SOC_DAIFMT_NB_NF is default */
4743 break;
4744 }
4745
4746 /*
4747 * check "[prefix]bitclock-master"
4748 * check "[prefix]frame-master"
4749 * SND_SOC_DAIFMT_MASTER_MASK area
4750 */
4751 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4752 bit = !!of_get_property(np, prop, NULL);
4753
4754 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4755 frame = !!of_get_property(np, prop, NULL);
4756
4757 switch ((bit << 4) + frame) {
4758 case 0x11:
4759 format |= SND_SOC_DAIFMT_CBM_CFM;
4760 break;
4761 case 0x10:
4762 format |= SND_SOC_DAIFMT_CBM_CFS;
4763 break;
4764 case 0x01:
4765 format |= SND_SOC_DAIFMT_CBS_CFM;
4766 break;
4767 default:
4768 format |= SND_SOC_DAIFMT_CBS_CFS;
4769 break;
4770 }
4771
4772 return format;
4773}
4774EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4775
Kuninori Morimotocb470082013-09-10 17:39:56 -07004776int snd_soc_of_get_dai_name(struct device_node *of_node,
4777 const char **dai_name)
4778{
4779 struct snd_soc_component *pos;
4780 struct of_phandle_args args;
4781 int ret;
4782
4783 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4784 "#sound-dai-cells", 0, &args);
4785 if (ret)
4786 return ret;
4787
4788 ret = -EPROBE_DEFER;
4789
4790 mutex_lock(&client_mutex);
4791 list_for_each_entry(pos, &component_list, list) {
4792 if (pos->dev->of_node != args.np)
4793 continue;
4794
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004795 if (pos->driver->of_xlate_dai_name) {
4796 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4797 } else {
4798 int id = -1;
4799
4800 switch (args.args_count) {
4801 case 0:
4802 id = 0; /* same as dai_drv[0] */
4803 break;
4804 case 1:
4805 id = args.args[0];
4806 break;
4807 default:
4808 /* not supported */
4809 break;
4810 }
4811
4812 if (id < 0 || id >= pos->num_dai) {
4813 ret = -EINVAL;
Xiubo Lie41975e2013-12-20 14:39:51 +08004814 break;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004815 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004816
4817 ret = 0;
4818
4819 *dai_name = pos->dai_drv[id].name;
4820 if (!*dai_name)
4821 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004822 }
4823
Kuninori Morimotocb470082013-09-10 17:39:56 -07004824 break;
4825 }
4826 mutex_unlock(&client_mutex);
4827
4828 of_node_put(args.np);
4829
4830 return ret;
4831}
4832EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4833
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004834static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004835{
Mark Brown384c89e2008-12-03 17:34:03 +00004836#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004837 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4838 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004839 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004840 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004841 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004842
Mark Brown8a9dab12011-01-10 22:25:21 +00004843 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004844 &codec_list_fops))
4845 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4846
Mark Brown8a9dab12011-01-10 22:25:21 +00004847 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004848 &dai_list_fops))
4849 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004850
Mark Brown8a9dab12011-01-10 22:25:21 +00004851 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004852 &platform_list_fops))
4853 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004854#endif
4855
Mark Brownfb257892011-04-28 10:57:54 +01004856 snd_soc_util_init();
4857
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004858 return platform_driver_register(&soc_driver);
4859}
Mark Brown4abe8e12010-10-12 17:41:03 +01004860module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004861
Mark Brown7d8c16a2008-11-30 22:11:24 +00004862static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004863{
Mark Brownfb257892011-04-28 10:57:54 +01004864 snd_soc_util_exit();
4865
Mark Brown384c89e2008-12-03 17:34:03 +00004866#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004867 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004868#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004869 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004870}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004871module_exit(snd_soc_exit);
4872
4873/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004874MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004875MODULE_DESCRIPTION("ALSA SoC Core");
4876MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004877MODULE_ALIAS("platform:soc-audio");