blob: d5710fc79b4998c185402f6811f8dca48a3c35b9 [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 Brown9949788b2010-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 Brown9949788b2010-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
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000857static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200858{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000859 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
860 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100861 struct snd_soc_component *component;
Mark Brownfe3e78e2009-11-03 22:13:13 +0000862 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000863 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000864 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100865 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200866
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000867 dev_dbg(card->dev, "ASoC: binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000868
Mark Brownb19e6e72012-03-14 21:18:39 +0000869 /* Find CPU DAI from registered DAIs*/
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100870 list_for_each_entry(component, &component_list, list) {
Stephen Warrenbc926572012-05-25 18:22:11 -0600871 if (dai_link->cpu_of_node &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100872 component->dev->of_node != dai_link->cpu_of_node)
Stephen Warrenbc926572012-05-25 18:22:11 -0600873 continue;
874 if (dai_link->cpu_name &&
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100875 strcmp(dev_name(component->dev), dai_link->cpu_name))
Stephen Warrenbc926572012-05-25 18:22:11 -0600876 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100877 list_for_each_entry(cpu_dai, &component->dai_list, list) {
878 if (dai_link->cpu_dai_name &&
879 strcmp(cpu_dai->name, dai_link->cpu_dai_name))
880 continue;
Stephen Warren2610ab72011-12-07 13:58:27 -0700881
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100882 rtd->cpu_dai = cpu_dai;
883 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000884 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000885
886 if (!rtd->cpu_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000887 dev_err(card->dev, "ASoC: CPU DAI %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000888 dai_link->cpu_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000889 return -EPROBE_DEFER;
Mark Brownc5af3a22008-11-28 13:29:45 +0000890 }
891
Mark Brownb19e6e72012-03-14 21:18:39 +0000892 /* Find CODEC from registered CODECs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000893 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700894 if (dai_link->codec_of_node) {
895 if (codec->dev->of_node != dai_link->codec_of_node)
896 continue;
897 } else {
898 if (strcmp(codec->name, dai_link->codec_name))
899 continue;
900 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000901
Stephen Warren2610ab72011-12-07 13:58:27 -0700902 rtd->codec = codec;
903
904 /*
905 * CODEC found, so find CODEC DAI from registered DAIs from
906 * this CODEC
907 */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100908 list_for_each_entry(codec_dai, &codec->component.dai_list, list) {
909 if (!strcmp(codec_dai->name, dai_link->codec_dai_name)) {
Stephen Warren2610ab72011-12-07 13:58:27 -0700910 rtd->codec_dai = codec_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +0100911 break;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000912 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000913 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000914
915 if (!rtd->codec_dai) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000916 dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n",
Stephen Warren2610ab72011-12-07 13:58:27 -0700917 dai_link->codec_dai_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000918 return -EPROBE_DEFER;
919 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000920 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000921
Mark Brownb19e6e72012-03-14 21:18:39 +0000922 if (!rtd->codec) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000923 dev_err(card->dev, "ASoC: CODEC %s not registered\n",
Mark Brownb19e6e72012-03-14 21:18:39 +0000924 dai_link->codec_name);
925 return -EPROBE_DEFER;
926 }
Mark Brown848dd8b2011-04-27 18:16:32 +0100927
928 /* if there's no platform we match on the empty platform */
929 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700930 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100931 platform_name = "snd-soc-dummy";
932
Mark Brownb19e6e72012-03-14 21:18:39 +0000933 /* find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000934 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700935 if (dai_link->platform_of_node) {
936 if (platform->dev->of_node !=
937 dai_link->platform_of_node)
938 continue;
939 } else {
940 if (strcmp(platform->name, platform_name))
941 continue;
942 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700943
944 rtd->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000945 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000946 if (!rtd->platform) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000947 dev_err(card->dev, "ASoC: platform %s not registered\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000948 dai_link->platform_name);
Mark Brownb19e6e72012-03-14 21:18:39 +0000949 return -EPROBE_DEFER;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000950 }
Mark Brownb19e6e72012-03-14 21:18:39 +0000951
952 card->num_rtd++;
953
954 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000955}
956
Stephen Warrend12cd192012-06-08 12:34:22 -0600957static int soc_remove_platform(struct snd_soc_platform *platform)
958{
959 int ret;
960
961 if (platform->driver->remove) {
962 ret = platform->driver->remove(platform);
963 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000964 dev_err(platform->dev, "ASoC: failed to remove %d\n",
965 ret);
Stephen Warrend12cd192012-06-08 12:34:22 -0600966 }
967
968 /* Make sure all DAPM widgets are freed */
969 snd_soc_dapm_free(&platform->dapm);
970
971 soc_cleanup_platform_debugfs(platform);
972 platform->probed = 0;
973 list_del(&platform->card_list);
974 module_put(platform->dev->driver->owner);
975
976 return 0;
977}
978
Jarkko Nikula589c3562010-12-06 16:27:07 +0200979static void soc_remove_codec(struct snd_soc_codec *codec)
980{
981 int err;
982
983 if (codec->driver->remove) {
984 err = codec->driver->remove(codec);
985 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +0000986 dev_err(codec->dev, "ASoC: failed to remove %d\n", err);
Jarkko Nikula589c3562010-12-06 16:27:07 +0200987 }
988
989 /* Make sure all DAPM widgets are freed */
990 snd_soc_dapm_free(&codec->dapm);
991
992 soc_cleanup_codec_debugfs(codec);
993 codec->probed = 0;
994 list_del(&codec->card_list);
995 module_put(codec->dev->driver->owner);
996}
997
Stephen Warren62ae68f2012-06-08 12:34:23 -0600998static void soc_remove_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000999{
1000 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001001 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1002 int err;
1003
1004 /* unregister the rtd device */
1005 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001006 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
1007 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1008 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001009 rtd->dev_registered = 0;
1010 }
1011
1012 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001013 if (codec_dai && codec_dai->probed &&
1014 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001015 if (codec_dai->driver->remove) {
1016 err = codec_dai->driver->remove(codec_dai);
1017 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001018 dev_err(codec_dai->dev,
1019 "ASoC: failed to remove %s: %d\n",
1020 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001021 }
1022 codec_dai->probed = 0;
1023 list_del(&codec_dai->card_list);
1024 }
1025
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001026 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001027 if (cpu_dai && cpu_dai->probed &&
1028 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001029 if (cpu_dai->driver->remove) {
1030 err = cpu_dai->driver->remove(cpu_dai);
1031 if (err < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001032 dev_err(cpu_dai->dev,
1033 "ASoC: failed to remove %s: %d\n",
1034 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001035 }
1036 cpu_dai->probed = 0;
1037 list_del(&cpu_dai->card_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001038
Stephen Warren18d75642012-06-08 12:34:21 -06001039 if (!cpu_dai->codec) {
1040 snd_soc_dapm_free(&cpu_dai->dapm);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001041 module_put(cpu_dai->dev->driver->owner);
Stephen Warren18d75642012-06-08 12:34:21 -06001042 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001043 }
1044}
1045
Stephen Warren62ae68f2012-06-08 12:34:23 -06001046static void soc_remove_link_components(struct snd_soc_card *card, int num,
1047 int order)
1048{
1049 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1050 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1051 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1052 struct snd_soc_platform *platform = rtd->platform;
1053 struct snd_soc_codec *codec;
1054
1055 /* remove the platform */
1056 if (platform && platform->probed &&
1057 platform->driver->remove_order == order) {
1058 soc_remove_platform(platform);
1059 }
1060
1061 /* remove the CODEC-side CODEC */
1062 if (codec_dai) {
1063 codec = codec_dai->codec;
1064 if (codec && codec->probed &&
1065 codec->driver->remove_order == order)
1066 soc_remove_codec(codec);
1067 }
1068
1069 /* remove any CPU-side CODEC */
1070 if (cpu_dai) {
1071 codec = cpu_dai->codec;
1072 if (codec && codec->probed &&
1073 codec->driver->remove_order == order)
1074 soc_remove_codec(codec);
1075 }
1076}
1077
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001078static void soc_remove_dai_links(struct snd_soc_card *card)
1079{
Liam Girdwood0168bf02011-06-07 16:08:05 +01001080 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001081
Liam Girdwood0168bf02011-06-07 16:08:05 +01001082 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1083 order++) {
1084 for (dai = 0; dai < card->num_rtd; dai++)
Stephen Warren62ae68f2012-06-08 12:34:23 -06001085 soc_remove_link_dais(card, dai, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001086 }
Stephen Warren62ae68f2012-06-08 12:34:23 -06001087
1088 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1089 order++) {
1090 for (dai = 0; dai < card->num_rtd; dai++)
1091 soc_remove_link_components(card, dai, order);
1092 }
1093
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001094 card->num_rtd = 0;
1095}
1096
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001097static void soc_set_name_prefix(struct snd_soc_card *card,
1098 struct snd_soc_codec *codec)
1099{
1100 int i;
1101
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001102 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001103 return;
1104
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001105 for (i = 0; i < card->num_configs; i++) {
1106 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001107 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1108 codec->name_prefix = map->name_prefix;
1109 break;
1110 }
1111 }
1112}
1113
Jarkko Nikula589c3562010-12-06 16:27:07 +02001114static int soc_probe_codec(struct snd_soc_card *card,
1115 struct snd_soc_codec *codec)
1116{
1117 int ret = 0;
Mark Brown89b95ac02011-03-07 16:38:44 +00001118 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001119 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001120
1121 codec->card = card;
1122 codec->dapm.card = card;
1123 soc_set_name_prefix(card, codec);
1124
Jarkko Nikula70d293312011-01-27 16:24:22 +02001125 if (!try_module_get(codec->dev->driver->owner))
1126 return -ENODEV;
1127
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001128 soc_init_codec_debugfs(codec);
1129
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001130 if (driver->dapm_widgets)
1131 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1132 driver->num_dapm_widgets);
1133
Mark Brown888df392012-02-16 19:37:51 -08001134 /* Create DAPM widgets for each DAI stream */
Nariman Poushin261edc72014-03-31 15:47:12 +01001135 list_for_each_entry(dai, &codec->component.dai_list, list) {
1136 ret = snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1137
1138 if (ret != 0) {
1139 dev_err(codec->dev,
1140 "Failed to create DAI widgets %d\n", ret);
1141 goto err_probe;
1142 }
1143 }
Mark Brown888df392012-02-16 19:37:51 -08001144
Mark Brown33c5f962011-08-22 18:40:30 +01001145 codec->dapm.idle_bias_off = driver->idle_bias_off;
1146
Xiubo Lia32c17b2014-03-11 12:43:22 +08001147 if (!codec->write && dev_get_regmap(codec->dev, NULL)) {
1148 /* Set the default I/O up try regmap */
1149 ret = snd_soc_codec_set_cache_io(codec, NULL);
1150 if (ret < 0) {
1151 dev_err(codec->dev,
1152 "Failed to set cache I/O: %d\n", ret);
1153 goto err_probe;
1154 }
1155 }
Xiubo Life2265e2014-02-27 17:49:52 +08001156
Mark Brown89b95ac02011-03-07 16:38:44 +00001157 if (driver->probe) {
1158 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001159 if (ret < 0) {
1160 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001161 "ASoC: failed to probe CODEC %d\n", ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001162 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001163 }
Chuansheng Liuff541f42012-12-21 18:17:12 +08001164 WARN(codec->dapm.idle_bias_off &&
1165 codec->dapm.bias_level != SND_SOC_BIAS_OFF,
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001166 "codec %s can not start from non-off bias with idle_bias_off==1\n",
1167 codec->name);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001168 }
1169
Mark Brownb7af1da2011-04-07 19:18:44 +09001170 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001171 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001172 driver->num_controls);
Mark Brown89b95ac02011-03-07 16:38:44 +00001173 if (driver->dapm_routes)
1174 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1175 driver->num_dapm_routes);
1176
Jarkko Nikula589c3562010-12-06 16:27:07 +02001177 /* mark codec as probed and add to card codec list */
1178 codec->probed = 1;
1179 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001180 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001181
Jarkko Nikula70d293312011-01-27 16:24:22 +02001182 return 0;
1183
1184err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001185 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001186 module_put(codec->dev->driver->owner);
1187
Jarkko Nikula589c3562010-12-06 16:27:07 +02001188 return ret;
1189}
1190
Liam Girdwood956245e2011-07-01 16:54:08 +01001191static int soc_probe_platform(struct snd_soc_card *card,
1192 struct snd_soc_platform *platform)
1193{
1194 int ret = 0;
1195 const struct snd_soc_platform_driver *driver = platform->driver;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001196 struct snd_soc_component *component;
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001197 struct snd_soc_dai *dai;
Liam Girdwood956245e2011-07-01 16:54:08 +01001198
1199 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001200 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001201
1202 if (!try_module_get(platform->dev->driver->owner))
1203 return -ENODEV;
1204
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001205 soc_init_platform_debugfs(platform);
1206
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001207 if (driver->dapm_widgets)
1208 snd_soc_dapm_new_controls(&platform->dapm,
1209 driver->dapm_widgets, driver->num_dapm_widgets);
1210
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001211 /* Create DAPM widgets for each DAI stream */
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001212 list_for_each_entry(component, &component_list, list) {
1213 if (component->dev != platform->dev)
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001214 continue;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01001215 list_for_each_entry(dai, &component->dai_list, list)
1216 snd_soc_dapm_new_dai_widgets(&platform->dapm, dai);
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001217 }
1218
Stephen Warren3fec6b62012-04-05 12:28:01 -06001219 platform->dapm.idle_bias_off = 1;
1220
Liam Girdwood956245e2011-07-01 16:54:08 +01001221 if (driver->probe) {
1222 ret = driver->probe(platform);
1223 if (ret < 0) {
1224 dev_err(platform->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001225 "ASoC: failed to probe platform %d\n", ret);
Liam Girdwood956245e2011-07-01 16:54:08 +01001226 goto err_probe;
1227 }
1228 }
1229
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001230 if (driver->controls)
1231 snd_soc_add_platform_controls(platform, driver->controls,
1232 driver->num_controls);
1233 if (driver->dapm_routes)
1234 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1235 driver->num_dapm_routes);
1236
Liam Girdwood956245e2011-07-01 16:54:08 +01001237 /* mark platform as probed and add to card platform list */
1238 platform->probed = 1;
1239 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001240 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001241
1242 return 0;
1243
1244err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001245 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001246 module_put(platform->dev->driver->owner);
1247
1248 return ret;
1249}
1250
Mark Brown36ae1a92012-01-06 17:12:45 -08001251static void rtd_release(struct device *dev)
1252{
1253 kfree(dev);
1254}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001255
Jarkko Nikula589c3562010-12-06 16:27:07 +02001256static int soc_post_component_init(struct snd_soc_card *card,
1257 struct snd_soc_codec *codec,
1258 int num, int dailess)
1259{
1260 struct snd_soc_dai_link *dai_link = NULL;
1261 struct snd_soc_aux_dev *aux_dev = NULL;
1262 struct snd_soc_pcm_runtime *rtd;
Lars-Peter Clausen6479f15ad2014-03-12 15:27:40 +01001263 const char *name;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001264 int ret = 0;
1265
1266 if (!dailess) {
1267 dai_link = &card->dai_link[num];
1268 rtd = &card->rtd[num];
1269 name = dai_link->name;
1270 } else {
1271 aux_dev = &card->aux_dev[num];
1272 rtd = &card->rtd_aux[num];
1273 name = aux_dev->name;
1274 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001275 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001276
Jarkko Nikula589c3562010-12-06 16:27:07 +02001277 /* do machine specific initialization */
1278 if (!dailess && dai_link->init)
1279 ret = dai_link->init(rtd);
1280 else if (dailess && aux_dev->init)
1281 ret = aux_dev->init(&codec->dapm);
1282 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001283 dev_err(card->dev, "ASoC: failed to init %s: %d\n", name, ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001284 return ret;
1285 }
Jarkko Nikula589c3562010-12-06 16:27:07 +02001286
Jarkko Nikula589c3562010-12-06 16:27:07 +02001287 /* register the rtd device */
1288 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001289
1290 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1291 if (!rtd->dev)
1292 return -ENOMEM;
1293 device_initialize(rtd->dev);
1294 rtd->dev->parent = card->dev;
1295 rtd->dev->release = rtd_release;
1296 rtd->dev->init_name = name;
1297 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001298 mutex_init(&rtd->pcm_mutex);
Liam Girdwood01d75842012-04-25 12:12:49 +01001299 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients);
1300 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients);
1301 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients);
1302 INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients);
Mark Brown36ae1a92012-01-06 17:12:45 -08001303 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001304 if (ret < 0) {
Chuansheng Liu865df9c2012-12-26 00:56:05 +08001305 /* calling put_device() here to free the rtd->dev */
1306 put_device(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001307 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001308 "ASoC: failed to register runtime device: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001309 return ret;
1310 }
1311 rtd->dev_registered = 1;
1312
1313 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001314 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001315 if (ret < 0)
1316 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001317 "ASoC: failed to add codec dapm sysfs entries: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001318
1319 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001320 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001321 if (ret < 0)
1322 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001323 "ASoC: failed to add codec sysfs files: %d\n", ret);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001324
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001325#ifdef CONFIG_DEBUG_FS
1326 /* add DPCM sysfs entries */
Liam Girdwoodcd0f8912012-04-30 11:05:30 +01001327 if (!dailess && !dai_link->dynamic)
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001328 goto out;
1329
1330 ret = soc_dpcm_debugfs_add(rtd);
1331 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001332 dev_err(rtd->dev, "ASoC: failed to add dpcm sysfs entries: %d\n", ret);
Liam Girdwoodf86dcef2012-04-25 12:12:50 +01001333
1334out:
1335#endif
Jarkko Nikula589c3562010-12-06 16:27:07 +02001336 return 0;
1337}
1338
Stephen Warren62ae68f2012-06-08 12:34:23 -06001339static int soc_probe_link_components(struct snd_soc_card *card, int num,
1340 int order)
1341{
1342 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1343 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1344 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1345 struct snd_soc_platform *platform = rtd->platform;
1346 int ret;
1347
1348 /* probe the CPU-side component, if it is a CODEC */
1349 if (cpu_dai->codec &&
1350 !cpu_dai->codec->probed &&
1351 cpu_dai->codec->driver->probe_order == order) {
1352 ret = soc_probe_codec(card, cpu_dai->codec);
1353 if (ret < 0)
1354 return ret;
1355 }
1356
1357 /* probe the CODEC-side component */
1358 if (!codec_dai->codec->probed &&
1359 codec_dai->codec->driver->probe_order == order) {
1360 ret = soc_probe_codec(card, codec_dai->codec);
1361 if (ret < 0)
1362 return ret;
1363 }
1364
1365 /* probe the platform */
1366 if (!platform->probed &&
1367 platform->driver->probe_order == order) {
1368 ret = soc_probe_platform(card, platform);
1369 if (ret < 0)
1370 return ret;
1371 }
1372
1373 return 0;
1374}
1375
1376static int soc_probe_link_dais(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001377{
1378 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1379 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1380 struct snd_soc_codec *codec = rtd->codec;
1381 struct snd_soc_platform *platform = rtd->platform;
Mark Brownc74184e2012-04-04 22:12:09 +01001382 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1383 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1384 struct snd_soc_dapm_widget *play_w, *capture_w;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001385 int ret;
1386
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001387 dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n",
Liam Girdwood0168bf02011-06-07 16:08:05 +01001388 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001389
1390 /* config components */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001391 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001392 codec_dai->card = card;
1393 cpu_dai->card = card;
1394
1395 /* set default power off timeout */
1396 rtd->pmdown_time = pmdown_time;
1397
1398 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001399 if (!cpu_dai->probed &&
1400 cpu_dai->driver->probe_order == order) {
Stephen Warrena9db7db2012-06-08 12:34:20 -06001401 if (!cpu_dai->codec) {
1402 cpu_dai->dapm.card = card;
1403 if (!try_module_get(cpu_dai->dev->driver->owner))
1404 return -ENODEV;
Liam Girdwood61b61e32011-05-24 13:57:43 +01001405
Stephen Warren18d75642012-06-08 12:34:21 -06001406 list_add(&cpu_dai->dapm.list, &card->dapm_list);
Stephen Warrena9db7db2012-06-08 12:34:20 -06001407 }
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00001408
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001409 if (cpu_dai->driver->probe) {
1410 ret = cpu_dai->driver->probe(cpu_dai);
1411 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001412 dev_err(cpu_dai->dev,
1413 "ASoC: failed to probe CPU DAI %s: %d\n",
1414 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001415 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001416 return ret;
1417 }
1418 }
1419 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001420 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001421 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1422 }
1423
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001424 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001425 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001426 if (codec_dai->driver->probe) {
1427 ret = codec_dai->driver->probe(codec_dai);
1428 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001429 dev_err(codec_dai->dev,
1430 "ASoC: failed to probe CODEC DAI %s: %d\n",
1431 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001432 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001433 }
1434 }
1435
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001436 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001437 codec_dai->probed = 1;
1438 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001439 }
1440
Liam Girdwood0168bf02011-06-07 16:08:05 +01001441 /* complete DAI probe during last probe */
1442 if (order != SND_SOC_COMP_ORDER_LAST)
1443 return 0;
1444
Jarkko Nikula589c3562010-12-06 16:27:07 +02001445 ret = soc_post_component_init(card, codec, num, 0);
1446 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001447 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001448
Mark Brown36ae1a92012-01-06 17:12:45 -08001449 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001450 if (ret < 0)
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001451 dev_warn(rtd->dev, "ASoC: failed to add pmdown_time sysfs: %d\n",
1452 ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001453
Namarta Kohli1245b702012-08-16 17:10:41 +05301454 if (cpu_dai->driver->compress_dai) {
1455 /*create compress_device"*/
1456 ret = soc_new_compress(rtd, num);
Mark Brownc74184e2012-04-04 22:12:09 +01001457 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001458 dev_err(card->dev, "ASoC: can't create compress %s\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301459 dai_link->stream_name);
Mark Brownc74184e2012-04-04 22:12:09 +01001460 return ret;
1461 }
1462 } else {
Namarta Kohli1245b702012-08-16 17:10:41 +05301463
1464 if (!dai_link->params) {
1465 /* create the pcm */
1466 ret = soc_new_pcm(rtd, num);
1467 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001468 dev_err(card->dev, "ASoC: can't create pcm %s :%d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301469 dai_link->stream_name, ret);
Mark Brownc74184e2012-04-04 22:12:09 +01001470 return ret;
1471 }
Namarta Kohli1245b702012-08-16 17:10:41 +05301472 } else {
Richard Fitzgerald9d58a072013-08-05 13:17:28 +01001473 INIT_DELAYED_WORK(&rtd->delayed_work,
1474 codec2codec_close_delayed_work);
1475
Namarta Kohli1245b702012-08-16 17:10:41 +05301476 /* link the DAI widgets */
1477 play_w = codec_dai->playback_widget;
1478 capture_w = cpu_dai->capture_widget;
1479 if (play_w && capture_w) {
1480 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
Mark Brownc74184e2012-04-04 22:12:09 +01001481 capture_w, play_w);
Namarta Kohli1245b702012-08-16 17:10:41 +05301482 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001483 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301484 play_w->name, capture_w->name, ret);
1485 return ret;
1486 }
1487 }
1488
1489 play_w = cpu_dai->playback_widget;
1490 capture_w = codec_dai->capture_widget;
1491 if (play_w && capture_w) {
1492 ret = snd_soc_dapm_new_pcm(card, dai_link->params,
1493 capture_w, play_w);
1494 if (ret != 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001495 dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n",
Namarta Kohli1245b702012-08-16 17:10:41 +05301496 play_w->name, capture_w->name, ret);
1497 return ret;
1498 }
Mark Brownc74184e2012-04-04 22:12:09 +01001499 }
1500 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001501 }
1502
1503 /* add platform data for AC97 devices */
1504 if (rtd->codec_dai->driver->ac97_control)
1505 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1506
1507 return 0;
1508}
1509
1510#ifdef CONFIG_SND_SOC_AC97_BUS
1511static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1512{
1513 int ret;
1514
1515 /* Only instantiate AC97 if not already done by the adaptor
1516 * for the generic AC97 subsystem.
1517 */
1518 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001519 /*
1520 * It is possible that the AC97 device is already registered to
1521 * the device subsystem. This happens when the device is created
1522 * via snd_ac97_mixer(). Currently only SoC codec that does so
1523 * is the generic AC97 glue but others migh emerge.
1524 *
1525 * In those cases we don't try to register the device again.
1526 */
1527 if (!rtd->codec->ac97_created)
1528 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001529
1530 ret = soc_ac97_dev_register(rtd->codec);
1531 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001532 dev_err(rtd->codec->dev,
1533 "ASoC: AC97 device register failed: %d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001534 return ret;
1535 }
1536
1537 rtd->codec->ac97_registered = 1;
1538 }
1539 return 0;
1540}
1541
1542static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1543{
1544 if (codec->ac97_registered) {
1545 soc_ac97_dev_unregister(codec);
1546 codec->ac97_registered = 0;
1547 }
1548}
1549#endif
1550
Mark Brownb19e6e72012-03-14 21:18:39 +00001551static int soc_check_aux_dev(struct snd_soc_card *card, int num)
1552{
1553 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
1554 struct snd_soc_codec *codec;
1555
1556 /* find CODEC from registered CODECs*/
1557 list_for_each_entry(codec, &codec_list, list) {
1558 if (!strcmp(codec->name, aux_dev->codec_name))
1559 return 0;
1560 }
1561
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001562 dev_err(card->dev, "ASoC: %s not registered\n", aux_dev->codec_name);
Mark Brownfb099cb2012-08-09 18:44:37 +01001563
Mark Brownb19e6e72012-03-14 21:18:39 +00001564 return -EPROBE_DEFER;
1565}
1566
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001567static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1568{
1569 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001570 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001571 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001572
1573 /* find CODEC from registered CODECs*/
1574 list_for_each_entry(codec, &codec_list, list) {
1575 if (!strcmp(codec->name, aux_dev->codec_name)) {
1576 if (codec->probed) {
1577 dev_err(codec->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001578 "ASoC: codec already probed");
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001579 ret = -EBUSY;
1580 goto out;
1581 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001582 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001583 }
1584 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001585 /* codec not found */
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001586 dev_err(card->dev, "ASoC: codec %s not found", aux_dev->codec_name);
Mark Brownb19e6e72012-03-14 21:18:39 +00001587 return -EPROBE_DEFER;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001588
Jarkko Nikula676ad982010-12-03 09:18:22 +02001589found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001590 ret = soc_probe_codec(card, codec);
1591 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001592 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001593
Jarkko Nikula589c3562010-12-06 16:27:07 +02001594 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001595
1596out:
1597 return ret;
1598}
1599
1600static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1601{
1602 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1603 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001604
1605 /* unregister the rtd device */
1606 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001607 device_remove_file(rtd->dev, &dev_attr_codec_reg);
Chuansheng Liud3bf1562012-12-26 00:57:32 +08001608 device_unregister(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001609 rtd->dev_registered = 0;
1610 }
1611
Jarkko Nikula589c3562010-12-06 16:27:07 +02001612 if (codec && codec->probed)
1613 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001614}
1615
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001616static int snd_soc_init_codec_cache(struct snd_soc_codec *codec)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001617{
1618 int ret;
1619
1620 if (codec->cache_init)
1621 return 0;
1622
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001623 ret = snd_soc_cache_init(codec);
1624 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001625 dev_err(codec->dev,
1626 "ASoC: Failed to set cache compression type: %d\n",
1627 ret);
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001628 return ret;
1629 }
1630 codec->cache_init = 1;
1631 return 0;
1632}
1633
Mark Brownb19e6e72012-03-14 21:18:39 +00001634static int snd_soc_instantiate_card(struct snd_soc_card *card)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001635{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001636 struct snd_soc_codec *codec;
Mark Brown75d9ac42011-09-27 16:41:01 +01001637 struct snd_soc_dai_link *dai_link;
Mark Brownf04209a2012-04-09 16:29:21 +01001638 int ret, i, order, dai_fmt;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001639
Liam Girdwood01b9d992012-03-07 10:38:25 +00001640 mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001641
Mark Brownb19e6e72012-03-14 21:18:39 +00001642 /* bind DAIs */
1643 for (i = 0; i < card->num_links; i++) {
1644 ret = soc_bind_dai_link(card, i);
1645 if (ret != 0)
1646 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001647 }
1648
Mark Brownb19e6e72012-03-14 21:18:39 +00001649 /* check aux_devs too */
1650 for (i = 0; i < card->num_aux_devs; i++) {
1651 ret = soc_check_aux_dev(card, i);
1652 if (ret != 0)
1653 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001654 }
1655
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001656 /* initialize the register cache for each available codec */
1657 list_for_each_entry(codec, &codec_list, list) {
1658 if (codec->cache_init)
1659 continue;
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +02001660 ret = snd_soc_init_codec_cache(codec);
Mark Brownb19e6e72012-03-14 21:18:39 +00001661 if (ret < 0)
1662 goto base_error;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001663 }
1664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001665 /* card bind complete so register a sound card */
Takashi Iwai102b5a82014-01-29 14:42:55 +01001666 ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001667 card->owner, 0, &card->snd_card);
1668 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001669 dev_err(card->dev,
1670 "ASoC: can't create sound card for card %s: %d\n",
1671 card->name, ret);
Mark Brownb19e6e72012-03-14 21:18:39 +00001672 goto base_error;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001673 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001674
Mark Browne37a4972011-03-02 18:21:57 +00001675 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1676 card->dapm.dev = card->dev;
1677 card->dapm.card = card;
1678 list_add(&card->dapm.list, &card->dapm_list);
1679
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001680#ifdef CONFIG_DEBUG_FS
1681 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1682#endif
1683
Mark Brown88ee1c62011-02-02 10:43:26 +00001684#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001685 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001686 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001687#endif
Andy Green6ed25972008-06-13 16:24:05 +01001688
Mark Brown9a841eb2011-04-12 17:51:37 -07001689 if (card->dapm_widgets)
1690 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1691 card->num_dapm_widgets);
1692
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001693 /* initialise the sound card only once */
1694 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001695 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001696 if (ret < 0)
1697 goto card_probe_error;
1698 }
1699
Stephen Warren62ae68f2012-06-08 12:34:23 -06001700 /* probe all components used by DAI links on this card */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001701 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1702 order++) {
1703 for (i = 0; i < card->num_links; i++) {
Stephen Warren62ae68f2012-06-08 12:34:23 -06001704 ret = soc_probe_link_components(card, i, order);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001705 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001706 dev_err(card->dev,
1707 "ASoC: failed to instantiate card %d\n",
1708 ret);
Stephen Warren62ae68f2012-06-08 12:34:23 -06001709 goto probe_dai_err;
1710 }
1711 }
1712 }
1713
1714 /* probe all DAI links on this card */
1715 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1716 order++) {
1717 for (i = 0; i < card->num_links; i++) {
1718 ret = soc_probe_link_dais(card, i, order);
1719 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001720 dev_err(card->dev,
1721 "ASoC: failed to instantiate card %d\n",
1722 ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001723 goto probe_dai_err;
1724 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001725 }
1726 }
1727
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001728 for (i = 0; i < card->num_aux_devs; i++) {
1729 ret = soc_probe_aux_dev(card, i);
1730 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001731 dev_err(card->dev,
1732 "ASoC: failed to add auxiliary devices %d\n",
1733 ret);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001734 goto probe_aux_dev_err;
1735 }
1736 }
1737
Mark Brown888df392012-02-16 19:37:51 -08001738 snd_soc_dapm_link_dai_widgets(card);
Liam Girdwoodb893ea52014-01-08 10:40:19 +00001739 snd_soc_dapm_connect_dai_link_widgets(card);
Mark Brown888df392012-02-16 19:37:51 -08001740
Mark Brownb7af1da2011-04-07 19:18:44 +09001741 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001742 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001743
Mark Brownb8ad29d2011-03-02 18:35:51 +00001744 if (card->dapm_routes)
1745 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1746 card->num_dapm_routes);
1747
Mark Brown75d9ac42011-09-27 16:41:01 +01001748 for (i = 0; i < card->num_links; i++) {
1749 dai_link = &card->dai_link[i];
Mark Brownf04209a2012-04-09 16:29:21 +01001750 dai_fmt = dai_link->dai_fmt;
Mark Brown75d9ac42011-09-27 16:41:01 +01001751
Mark Brownf04209a2012-04-09 16:29:21 +01001752 if (dai_fmt) {
Mark Brown75d9ac42011-09-27 16:41:01 +01001753 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001754 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001755 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001756 dev_warn(card->rtd[i].codec_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001757 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001758 ret);
Mark Brownf04209a2012-04-09 16:29:21 +01001759 }
1760
1761 /* If this is a regular CPU link there will be a platform */
Stephen Warrenfe33d4c2012-05-14 14:47:22 -06001762 if (dai_fmt &&
1763 (dai_link->platform_name || dai_link->platform_of_node)) {
Mark Brownf04209a2012-04-09 16:29:21 +01001764 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1765 dai_fmt);
1766 if (ret != 0 && ret != -ENOTSUPP)
1767 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001768 "ASoC: Failed to set DAI format: %d\n",
Mark Brownf04209a2012-04-09 16:29:21 +01001769 ret);
1770 } else if (dai_fmt) {
1771 /* Flip the polarity for the "CPU" end */
1772 dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK;
1773 switch (dai_link->dai_fmt &
1774 SND_SOC_DAIFMT_MASTER_MASK) {
1775 case SND_SOC_DAIFMT_CBM_CFM:
1776 dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
1777 break;
1778 case SND_SOC_DAIFMT_CBM_CFS:
1779 dai_fmt |= SND_SOC_DAIFMT_CBS_CFM;
1780 break;
1781 case SND_SOC_DAIFMT_CBS_CFM:
1782 dai_fmt |= SND_SOC_DAIFMT_CBM_CFS;
1783 break;
1784 case SND_SOC_DAIFMT_CBS_CFS:
1785 dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
1786 break;
1787 }
Mark Brown75d9ac42011-09-27 16:41:01 +01001788
1789 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
Mark Brownf04209a2012-04-09 16:29:21 +01001790 dai_fmt);
Shawn Guo5e4ba562012-03-09 00:59:40 +08001791 if (ret != 0 && ret != -ENOTSUPP)
Mark Brown75d9ac42011-09-27 16:41:01 +01001792 dev_warn(card->rtd[i].cpu_dai->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001793 "ASoC: Failed to set DAI format: %d\n",
Mark Brown75d9ac42011-09-27 16:41:01 +01001794 ret);
1795 }
1796 }
1797
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001798 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001799 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001800 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1801 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001802 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1803 "%s", card->driver_name ? card->driver_name : card->name);
1804 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1805 switch (card->snd_card->driver[i]) {
1806 case '_':
1807 case '-':
1808 case '\0':
1809 break;
1810 default:
1811 if (!isalnum(card->snd_card->driver[i]))
1812 card->snd_card->driver[i] = '_';
1813 break;
1814 }
1815 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001816
Mark Brown28e9ad92011-03-02 18:36:34 +00001817 if (card->late_probe) {
1818 ret = card->late_probe(card);
1819 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001820 dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n",
Mark Brown28e9ad92011-03-02 18:36:34 +00001821 card->name, ret);
1822 goto probe_aux_dev_err;
1823 }
1824 }
1825
Stephen Warren16332812011-11-23 12:42:04 -07001826 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001827 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001828 snd_soc_dapm_auto_nc_codec_pins(codec);
1829
Lars-Peter Clausen824ef822013-08-27 15:51:01 +02001830 snd_soc_dapm_new_widgets(card);
Lars-Peter Clausen8c193b82013-08-27 15:51:00 +02001831
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001832 ret = snd_card_register(card->snd_card);
1833 if (ret < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001834 dev_err(card->dev, "ASoC: failed to register soundcard %d\n",
1835 ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001836 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001837 }
1838
1839#ifdef CONFIG_SND_SOC_AC97_BUS
1840 /* register any AC97 codecs */
1841 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001842 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1843 if (ret < 0) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02001844 dev_err(card->dev,
1845 "ASoC: failed to register AC97: %d\n", ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001846 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001847 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001848 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001849 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001850 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001851#endif
1852
Mark Brown435c5e22008-12-04 15:32:53 +00001853 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001854 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001855 mutex_unlock(&card->mutex);
Mark Brownb19e6e72012-03-14 21:18:39 +00001856
1857 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001858
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001859probe_aux_dev_err:
1860 for (i = 0; i < card->num_aux_devs; i++)
1861 soc_remove_aux_dev(card, i);
1862
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001863probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001864 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001865
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001866card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001867 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001868 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001869
1870 snd_card_free(card->snd_card);
1871
Mark Brownb19e6e72012-03-14 21:18:39 +00001872base_error:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001873 mutex_unlock(&card->mutex);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001874
Mark Brownb19e6e72012-03-14 21:18:39 +00001875 return ret;
Mark Brown435c5e22008-12-04 15:32:53 +00001876}
1877
1878/* probes a new socdev */
1879static int soc_probe(struct platform_device *pdev)
1880{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001881 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001882
Vinod Koul70a7ca32011-01-14 19:22:48 +05301883 /*
1884 * no card, so machine driver should be registering card
1885 * we should not be here in that case so ret error
1886 */
1887 if (!card)
1888 return -EINVAL;
1889
Mark Brownfe4085e2012-03-02 13:07:41 +00001890 dev_warn(&pdev->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00001891 "ASoC: machine %s should use snd_soc_register_card()\n",
Mark Brownfe4085e2012-03-02 13:07:41 +00001892 card->name);
1893
Mark Brown435c5e22008-12-04 15:32:53 +00001894 /* Bodge while we unpick instantiation */
1895 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001896
Mark Brown28d528c2012-08-09 18:45:23 +01001897 return snd_soc_register_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001898}
1899
Vinod Koulb0e26482011-01-13 22:48:02 +05301900static int soc_cleanup_card_resources(struct snd_soc_card *card)
1901{
Vinod Koulb0e26482011-01-13 22:48:02 +05301902 int i;
1903
1904 /* make sure any delayed work runs */
1905 for (i = 0; i < card->num_rtd; i++) {
1906 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001907 flush_delayed_work(&rtd->delayed_work);
Vinod Koulb0e26482011-01-13 22:48:02 +05301908 }
1909
1910 /* remove auxiliary devices */
1911 for (i = 0; i < card->num_aux_devs; i++)
1912 soc_remove_aux_dev(card, i);
1913
1914 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001915 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301916
1917 soc_cleanup_card_debugfs(card);
1918
1919 /* remove the card */
1920 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001921 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301922
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001923 snd_soc_dapm_free(&card->dapm);
1924
Vinod Koulb0e26482011-01-13 22:48:02 +05301925 snd_card_free(card->snd_card);
1926 return 0;
1927
1928}
1929
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930/* removes a socdev */
1931static int soc_remove(struct platform_device *pdev)
1932{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001933 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934
Mark Brownc5af3a22008-11-28 13:29:45 +00001935 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001936 return 0;
1937}
1938
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001939int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001940{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001941 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001942 int i;
Mark Brown51737472009-06-22 13:16:51 +01001943
1944 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001945 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001946
1947 /* Flush out pmdown_time work - we actually do want to run it
1948 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001949 for (i = 0; i < card->num_rtd; i++) {
1950 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo43829732012-08-20 14:51:24 -07001951 flush_delayed_work(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001952 }
Mark Brown51737472009-06-22 13:16:51 +01001953
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001954 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001955
Nicolin Chen988e8cc2013-11-04 14:57:31 +08001956 /* deactivate pins to sleep state */
1957 for (i = 0; i < card->num_rtd; i++) {
1958 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1959 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
1960 pinctrl_pm_select_sleep_state(codec_dai->dev);
1961 pinctrl_pm_select_sleep_state(cpu_dai->dev);
1962 }
1963
Mark Brown416356f2009-06-30 19:05:15 +01001964 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001965}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001966EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001967
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001968const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301969 .suspend = snd_soc_suspend,
1970 .resume = snd_soc_resume,
1971 .freeze = snd_soc_suspend,
1972 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001973 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301974 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001975};
Stephen Warrendeb26072011-04-05 19:35:30 -06001976EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001977
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001978/* ASoC platform driver */
1979static struct platform_driver soc_driver = {
1980 .driver = {
1981 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001982 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001983 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001984 },
1985 .probe = soc_probe,
1986 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001987};
1988
Mark Brown096e49d2009-07-05 15:12:22 +01001989/**
1990 * snd_soc_codec_volatile_register: Report if a register is volatile.
1991 *
1992 * @codec: CODEC to query.
1993 * @reg: Register to query.
1994 *
1995 * Boolean function indiciating if a CODEC register is volatile.
1996 */
Mark Brown181e0552011-01-24 14:05:25 +00001997int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1998 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001999{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00002000 if (codec->volatile_register)
2001 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01002002 else
2003 return 0;
2004}
2005EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
2006
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002007/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002008 * snd_soc_codec_readable_register: Report if a register is readable.
2009 *
2010 * @codec: CODEC to query.
2011 * @reg: Register to query.
2012 *
2013 * Boolean function indicating if a CODEC register is readable.
2014 */
2015int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
2016 unsigned int reg)
2017{
2018 if (codec->readable_register)
2019 return codec->readable_register(codec, reg);
2020 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002021 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002022}
2023EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
2024
2025/**
2026 * snd_soc_codec_writable_register: Report if a register is writable.
2027 *
2028 * @codec: CODEC to query.
2029 * @reg: Register to query.
2030 *
2031 * Boolean function indicating if a CODEC register is writable.
2032 */
2033int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
2034 unsigned int reg)
2035{
2036 if (codec->writable_register)
2037 return codec->writable_register(codec, reg);
2038 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02002039 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002040}
2041EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
2042
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002043int snd_soc_platform_read(struct snd_soc_platform *platform,
2044 unsigned int reg)
2045{
2046 unsigned int ret;
2047
2048 if (!platform->driver->read) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002049 dev_err(platform->dev, "ASoC: platform has no read back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002050 return -1;
2051 }
2052
2053 ret = platform->driver->read(platform, reg);
2054 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002055 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002056
2057 return ret;
2058}
2059EXPORT_SYMBOL_GPL(snd_soc_platform_read);
2060
2061int snd_soc_platform_write(struct snd_soc_platform *platform,
2062 unsigned int reg, unsigned int val)
2063{
2064 if (!platform->driver->write) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002065 dev_err(platform->dev, "ASoC: platform has no write back\n");
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002066 return -1;
2067 }
2068
2069 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01002070 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01002071 return platform->driver->write(platform, reg, val);
2072}
2073EXPORT_SYMBOL_GPL(snd_soc_platform_write);
2074
Dimitris Papastamos239c9702011-03-24 13:45:18 +00002075/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002076 * snd_soc_new_ac97_codec - initailise AC97 device
2077 * @codec: audio codec
2078 * @ops: AC97 bus operations
2079 * @num: AC97 codec number
2080 *
2081 * Initialises AC97 codec resources for use by ad-hoc devices only.
2082 */
2083int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
2084 struct snd_ac97_bus_ops *ops, int num)
2085{
2086 mutex_lock(&codec->mutex);
2087
2088 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
2089 if (codec->ac97 == NULL) {
2090 mutex_unlock(&codec->mutex);
2091 return -ENOMEM;
2092 }
2093
2094 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
2095 if (codec->ac97->bus == NULL) {
2096 kfree(codec->ac97);
2097 codec->ac97 = NULL;
2098 mutex_unlock(&codec->mutex);
2099 return -ENOMEM;
2100 }
2101
2102 codec->ac97->bus->ops = ops;
2103 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03002104
2105 /*
2106 * Mark the AC97 device to be created by us. This way we ensure that the
2107 * device will be registered with the device subsystem later on.
2108 */
2109 codec->ac97_created = 1;
2110
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002111 mutex_unlock(&codec->mutex);
2112 return 0;
2113}
2114EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
2115
Markus Pargmann741a5092013-08-19 17:05:55 +02002116static struct snd_ac97_reset_cfg snd_ac97_rst_cfg;
2117
2118static void snd_soc_ac97_warm_reset(struct snd_ac97 *ac97)
2119{
2120 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2121
2122 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_warm_reset);
2123
2124 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 1);
2125
2126 udelay(10);
2127
2128 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2129
2130 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2131 msleep(2);
2132}
2133
2134static void snd_soc_ac97_reset(struct snd_ac97 *ac97)
2135{
2136 struct pinctrl *pctl = snd_ac97_rst_cfg.pctl;
2137
2138 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_reset);
2139
2140 gpio_direction_output(snd_ac97_rst_cfg.gpio_sync, 0);
2141 gpio_direction_output(snd_ac97_rst_cfg.gpio_sdata, 0);
2142 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 0);
2143
2144 udelay(10);
2145
2146 gpio_direction_output(snd_ac97_rst_cfg.gpio_reset, 1);
2147
2148 pinctrl_select_state(pctl, snd_ac97_rst_cfg.pstate_run);
2149 msleep(2);
2150}
2151
2152static int snd_soc_ac97_parse_pinctl(struct device *dev,
2153 struct snd_ac97_reset_cfg *cfg)
2154{
2155 struct pinctrl *p;
2156 struct pinctrl_state *state;
2157 int gpio;
2158 int ret;
2159
2160 p = devm_pinctrl_get(dev);
2161 if (IS_ERR(p)) {
2162 dev_err(dev, "Failed to get pinctrl\n");
2163 return PTR_RET(p);
2164 }
2165 cfg->pctl = p;
2166
2167 state = pinctrl_lookup_state(p, "ac97-reset");
2168 if (IS_ERR(state)) {
2169 dev_err(dev, "Can't find pinctrl state ac97-reset\n");
2170 return PTR_RET(state);
2171 }
2172 cfg->pstate_reset = state;
2173
2174 state = pinctrl_lookup_state(p, "ac97-warm-reset");
2175 if (IS_ERR(state)) {
2176 dev_err(dev, "Can't find pinctrl state ac97-warm-reset\n");
2177 return PTR_RET(state);
2178 }
2179 cfg->pstate_warm_reset = state;
2180
2181 state = pinctrl_lookup_state(p, "ac97-running");
2182 if (IS_ERR(state)) {
2183 dev_err(dev, "Can't find pinctrl state ac97-running\n");
2184 return PTR_RET(state);
2185 }
2186 cfg->pstate_run = state;
2187
2188 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 0);
2189 if (gpio < 0) {
2190 dev_err(dev, "Can't find ac97-sync gpio\n");
2191 return gpio;
2192 }
2193 ret = devm_gpio_request(dev, gpio, "AC97 link sync");
2194 if (ret) {
2195 dev_err(dev, "Failed requesting ac97-sync gpio\n");
2196 return ret;
2197 }
2198 cfg->gpio_sync = gpio;
2199
2200 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 1);
2201 if (gpio < 0) {
2202 dev_err(dev, "Can't find ac97-sdata gpio %d\n", gpio);
2203 return gpio;
2204 }
2205 ret = devm_gpio_request(dev, gpio, "AC97 link sdata");
2206 if (ret) {
2207 dev_err(dev, "Failed requesting ac97-sdata gpio\n");
2208 return ret;
2209 }
2210 cfg->gpio_sdata = gpio;
2211
2212 gpio = of_get_named_gpio(dev->of_node, "ac97-gpios", 2);
2213 if (gpio < 0) {
2214 dev_err(dev, "Can't find ac97-reset gpio\n");
2215 return gpio;
2216 }
2217 ret = devm_gpio_request(dev, gpio, "AC97 link reset");
2218 if (ret) {
2219 dev_err(dev, "Failed requesting ac97-reset gpio\n");
2220 return ret;
2221 }
2222 cfg->gpio_reset = gpio;
2223
2224 return 0;
2225}
2226
Mark Brownb047e1c2013-06-26 12:45:59 +01002227struct snd_ac97_bus_ops *soc_ac97_ops;
Kevin Hilmanf74b5e22013-06-28 11:17:49 -07002228EXPORT_SYMBOL_GPL(soc_ac97_ops);
Mark Brownb047e1c2013-06-26 12:45:59 +01002229
2230int snd_soc_set_ac97_ops(struct snd_ac97_bus_ops *ops)
2231{
2232 if (ops == soc_ac97_ops)
2233 return 0;
2234
2235 if (soc_ac97_ops && ops)
2236 return -EBUSY;
2237
2238 soc_ac97_ops = ops;
2239
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops);
2243
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002244/**
Markus Pargmann741a5092013-08-19 17:05:55 +02002245 * snd_soc_set_ac97_ops_of_reset - Set ac97 ops with generic ac97 reset functions
2246 *
2247 * This function sets the reset and warm_reset properties of ops and parses
2248 * the device node of pdev to get pinctrl states and gpio numbers to use.
2249 */
2250int snd_soc_set_ac97_ops_of_reset(struct snd_ac97_bus_ops *ops,
2251 struct platform_device *pdev)
2252{
2253 struct device *dev = &pdev->dev;
2254 struct snd_ac97_reset_cfg cfg;
2255 int ret;
2256
2257 ret = snd_soc_ac97_parse_pinctl(dev, &cfg);
2258 if (ret)
2259 return ret;
2260
2261 ret = snd_soc_set_ac97_ops(ops);
2262 if (ret)
2263 return ret;
2264
2265 ops->warm_reset = snd_soc_ac97_warm_reset;
2266 ops->reset = snd_soc_ac97_reset;
2267
2268 snd_ac97_rst_cfg = cfg;
2269 return 0;
2270}
2271EXPORT_SYMBOL_GPL(snd_soc_set_ac97_ops_of_reset);
2272
2273/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002274 * snd_soc_free_ac97_codec - free AC97 codec device
2275 * @codec: audio codec
2276 *
2277 * Frees AC97 codec device resources.
2278 */
2279void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
2280{
2281 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002282#ifdef CONFIG_SND_SOC_AC97_BUS
2283 soc_unregister_ac97_dai_link(codec);
2284#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002285 kfree(codec->ac97->bus);
2286 kfree(codec->ac97);
2287 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03002288 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002289 mutex_unlock(&codec->mutex);
2290}
2291EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
2292
Mark Brownc3753702010-11-01 15:41:57 -04002293unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
2294{
2295 unsigned int ret;
2296
Mark Brownc3acec22010-12-02 16:15:29 +00002297 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04002298 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04002299 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04002300
2301 return ret;
2302}
2303EXPORT_SYMBOL_GPL(snd_soc_read);
2304
2305unsigned int snd_soc_write(struct snd_soc_codec *codec,
2306 unsigned int reg, unsigned int val)
2307{
2308 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04002309 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00002310 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04002311}
2312EXPORT_SYMBOL_GPL(snd_soc_write);
2313
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002314/**
2315 * snd_soc_update_bits - update codec register bits
2316 * @codec: audio codec
2317 * @reg: codec register
2318 * @mask: register mask
2319 * @value: new value
2320 *
2321 * Writes new register value.
2322 *
Timur Tabi180c3292011-01-10 15:58:13 -06002323 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002324 */
2325int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002326 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002327{
Mark Brown8a713da2011-12-03 12:33:55 +00002328 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002329 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06002330 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002331
Mark Brown8a713da2011-12-03 12:33:55 +00002332 if (codec->using_regmap) {
2333 ret = regmap_update_bits_check(codec->control_data, reg,
2334 mask, value, &change);
2335 } else {
2336 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06002337 if (ret < 0)
2338 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00002339
2340 old = ret;
2341 new = (old & ~mask) | (value & mask);
2342 change = old != new;
2343 if (change)
2344 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06002345 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002346
Mark Brown8a713da2011-12-03 12:33:55 +00002347 if (ret < 0)
2348 return ret;
2349
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002350 return change;
2351}
2352EXPORT_SYMBOL_GPL(snd_soc_update_bits);
2353
2354/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002355 * snd_soc_update_bits_locked - update codec register bits
2356 * @codec: audio codec
2357 * @reg: codec register
2358 * @mask: register mask
2359 * @value: new value
2360 *
2361 * Writes new register value, and takes the codec mutex.
2362 *
2363 * Returns 1 for change else 0.
2364 */
Mark Browndd1b3d52009-12-04 14:22:03 +00002365int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
2366 unsigned short reg, unsigned int mask,
2367 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002368{
2369 int change;
2370
2371 mutex_lock(&codec->mutex);
2372 change = snd_soc_update_bits(codec, reg, mask, value);
2373 mutex_unlock(&codec->mutex);
2374
2375 return change;
2376}
Mark Browndd1b3d52009-12-04 14:22:03 +00002377EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002378
2379/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002380 * snd_soc_test_bits - test register for change
2381 * @codec: audio codec
2382 * @reg: codec register
2383 * @mask: register mask
2384 * @value: new value
2385 *
2386 * Tests a register with a new value and checks if the new value is
2387 * different from the old value.
2388 *
2389 * Returns 1 for change else 0.
2390 */
2391int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002392 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002393{
2394 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002395 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002397 old = snd_soc_read(codec, reg);
2398 new = (old & ~mask) | value;
2399 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002400
2401 return change;
2402}
2403EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2404
2405/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002406 * snd_soc_cnew - create new control
2407 * @_template: control template
2408 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002409 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002410 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411 *
2412 * Create a new mixer control from a template control.
2413 *
2414 * Returns 0 for success, else error.
2415 */
2416struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002417 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002418 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002419{
2420 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002421 struct snd_kcontrol *kcontrol;
2422 char *name = NULL;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002423
2424 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002425 template.index = 0;
2426
Mark Brownefb7ac32011-03-08 17:23:24 +00002427 if (!long_name)
2428 long_name = template.name;
2429
2430 if (prefix) {
Lars-Peter Clausen2b581072013-05-14 11:05:32 +02002431 name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name);
Mark Brownefb7ac32011-03-08 17:23:24 +00002432 if (!name)
2433 return NULL;
2434
Mark Brownefb7ac32011-03-08 17:23:24 +00002435 template.name = name;
2436 } else {
2437 template.name = long_name;
2438 }
2439
2440 kcontrol = snd_ctl_new1(&template, data);
2441
2442 kfree(name);
2443
2444 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002445}
2446EXPORT_SYMBOL_GPL(snd_soc_cnew);
2447
Liam Girdwood022658b2012-02-03 17:43:09 +00002448static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2449 const struct snd_kcontrol_new *controls, int num_controls,
2450 const char *prefix, void *data)
2451{
2452 int err, i;
2453
2454 for (i = 0; i < num_controls; i++) {
2455 const struct snd_kcontrol_new *control = &controls[i];
2456 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2457 control->name, prefix));
2458 if (err < 0) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00002459 dev_err(dev, "ASoC: Failed to add %s: %d\n",
2460 control->name, err);
Liam Girdwood022658b2012-02-03 17:43:09 +00002461 return err;
2462 }
2463 }
2464
2465 return 0;
2466}
2467
Dimitris Papastamos4fefd692013-07-29 13:51:58 +01002468struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card,
2469 const char *name)
2470{
2471 struct snd_card *card = soc_card->snd_card;
2472 struct snd_kcontrol *kctl;
2473
2474 if (unlikely(!name))
2475 return NULL;
2476
2477 list_for_each_entry(kctl, &card->controls, list)
2478 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name)))
2479 return kctl;
2480 return NULL;
2481}
2482EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol);
2483
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002484/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002485 * snd_soc_add_codec_controls - add an array of controls to a codec.
2486 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002487 * duplicating this code.
2488 *
2489 * @codec: codec to add controls to
2490 * @controls: array of controls to add
2491 * @num_controls: number of elements in the array
2492 *
2493 * Return 0 for success, else error.
2494 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002495int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002496 const struct snd_kcontrol_new *controls, int num_controls)
2497{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002498 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002499
Liam Girdwood022658b2012-02-03 17:43:09 +00002500 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2501 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002502}
Liam Girdwood022658b2012-02-03 17:43:09 +00002503EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002504
2505/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002506 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002507 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002508 *
2509 * @platform: platform to add controls to
2510 * @controls: array of controls to add
2511 * @num_controls: number of elements in the array
2512 *
2513 * Return 0 for success, else error.
2514 */
2515int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2516 const struct snd_kcontrol_new *controls, int num_controls)
2517{
2518 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002519
Liam Girdwood022658b2012-02-03 17:43:09 +00002520 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2521 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002522}
2523EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2524
2525/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002526 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2527 * Convenience function to add a list of controls.
2528 *
2529 * @soc_card: SoC card to add controls to
2530 * @controls: array of controls to add
2531 * @num_controls: number of elements in the array
2532 *
2533 * Return 0 for success, else error.
2534 */
2535int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2536 const struct snd_kcontrol_new *controls, int num_controls)
2537{
2538 struct snd_card *card = soc_card->snd_card;
2539
2540 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2541 NULL, soc_card);
2542}
2543EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2544
2545/**
2546 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2547 * Convienience function to add a list of controls.
2548 *
2549 * @dai: DAI to add controls to
2550 * @controls: array of controls to add
2551 * @num_controls: number of elements in the array
2552 *
2553 * Return 0 for success, else error.
2554 */
2555int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2556 const struct snd_kcontrol_new *controls, int num_controls)
2557{
2558 struct snd_card *card = dai->card->snd_card;
2559
2560 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2561 NULL, dai);
2562}
2563EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2564
2565/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002566 * snd_soc_info_enum_double - enumerated double mixer info callback
2567 * @kcontrol: mixer control
2568 * @uinfo: control element information
2569 *
2570 * Callback to provide information about a double enumerated
2571 * mixer control.
2572 *
2573 * Returns 0 for success.
2574 */
2575int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2576 struct snd_ctl_elem_info *uinfo)
2577{
2578 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2579
2580 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2581 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002582 uinfo->value.enumerated.items = e->items;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002583
Takashi Iwai9a8d38d2014-02-18 08:11:42 +01002584 if (uinfo->value.enumerated.item >= e->items)
2585 uinfo->value.enumerated.item = e->items - 1;
Takashi Iwaia19685c2013-10-28 14:21:46 +01002586 strlcpy(uinfo->value.enumerated.name,
2587 e->texts[uinfo->value.enumerated.item],
2588 sizeof(uinfo->value.enumerated.name));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002589 return 0;
2590}
2591EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2592
2593/**
2594 * snd_soc_get_enum_double - enumerated double mixer get callback
2595 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002596 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002597 *
2598 * Callback to get the value of a double enumerated mixer.
2599 *
2600 * Returns 0 for success.
2601 */
2602int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2603 struct snd_ctl_elem_value *ucontrol)
2604{
2605 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2606 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002607 unsigned int val, item;
2608 unsigned int reg_val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002609
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002610 reg_val = snd_soc_read(codec, e->reg);
2611 val = (reg_val >> e->shift_l) & e->mask;
2612 item = snd_soc_enum_val_to_item(e, val);
2613 ucontrol->value.enumerated.item[0] = item;
2614 if (e->shift_l != e->shift_r) {
2615 val = (reg_val >> e->shift_l) & e->mask;
2616 item = snd_soc_enum_val_to_item(e, val);
2617 ucontrol->value.enumerated.item[1] = item;
2618 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002619
2620 return 0;
2621}
2622EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2623
2624/**
2625 * snd_soc_put_enum_double - enumerated double mixer put callback
2626 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002627 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002628 *
2629 * Callback to set the value of a double enumerated mixer.
2630 *
2631 * Returns 0 for success.
2632 */
2633int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2634 struct snd_ctl_elem_value *ucontrol)
2635{
2636 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2637 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002638 unsigned int *item = ucontrol->value.enumerated.item;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002639 unsigned int val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002640 unsigned int mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002641
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002642 if (item[0] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002643 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002644 val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002645 mask = e->mask << e->shift_l;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002646 if (e->shift_l != e->shift_r) {
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002647 if (item[1] >= e->items)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002648 return -EINVAL;
Lars-Peter Clausen29ae2fa2014-02-28 08:31:03 +01002649 val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02002650 mask |= e->mask << e->shift_r;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002651 }
2652
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002653 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002654}
2655EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2656
2657/**
Markus Pargmannf227b882014-01-16 16:02:10 +01002658 * snd_soc_read_signed - Read a codec register and interprete as signed value
2659 * @codec: codec
2660 * @reg: Register to read
2661 * @mask: Mask to use after shifting the register value
2662 * @shift: Right shift of register value
2663 * @sign_bit: Bit that describes if a number is negative or not.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002664 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002665 * This functions reads a codec register. The register value is shifted right
2666 * by 'shift' bits and masked with the given 'mask'. Afterwards it translates
2667 * the given registervalue into a signed integer if sign_bit is non-zero.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002668 *
Markus Pargmannf227b882014-01-16 16:02:10 +01002669 * Returns the register value as signed int.
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002670 */
Markus Pargmannf227b882014-01-16 16:02:10 +01002671static int snd_soc_read_signed(struct snd_soc_codec *codec, unsigned int reg,
2672 unsigned int mask, unsigned int shift, unsigned int sign_bit)
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002673{
Markus Pargmannf227b882014-01-16 16:02:10 +01002674 int ret;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002675 unsigned int val;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002676
Markus Pargmannf227b882014-01-16 16:02:10 +01002677 val = (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002678
Markus Pargmannf227b882014-01-16 16:02:10 +01002679 if (!sign_bit)
2680 return val;
2681
2682 /* non-negative number */
2683 if (!(val & BIT(sign_bit)))
2684 return val;
2685
2686 ret = val;
2687
2688 /*
2689 * The register most probably does not contain a full-sized int.
2690 * Instead we have an arbitrary number of bits in a signed
2691 * representation which has to be translated into a full-sized int.
2692 * This is done by filling up all bits above the sign-bit.
2693 */
2694 ret |= ~((int)(BIT(sign_bit) - 1));
2695
2696 return ret;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002697}
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002698
2699/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002700 * snd_soc_info_volsw - single mixer info callback
2701 * @kcontrol: mixer control
2702 * @uinfo: control element information
2703 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002704 * Callback to provide information about a single mixer control, or a double
2705 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002706 *
2707 * Returns 0 for success.
2708 */
2709int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2710 struct snd_ctl_elem_info *uinfo)
2711{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002712 struct soc_mixer_control *mc =
2713 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002714 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002715
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002716 if (!mc->platform_max)
2717 mc->platform_max = mc->max;
2718 platform_max = mc->platform_max;
2719
2720 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002721 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2722 else
2723 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2724
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002725 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002726 uinfo->value.integer.min = 0;
Markus Pargmannf227b882014-01-16 16:02:10 +01002727 uinfo->value.integer.max = platform_max - mc->min;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002728 return 0;
2729}
2730EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2731
2732/**
2733 * snd_soc_get_volsw - single mixer get callback
2734 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002735 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002736 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002737 * Callback to get the value of a single mixer control, or a double mixer
2738 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002739 *
2740 * Returns 0 for success.
2741 */
2742int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2743 struct snd_ctl_elem_value *ucontrol)
2744{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002745 struct soc_mixer_control *mc =
2746 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002747 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002748 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002749 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002750 unsigned int shift = mc->shift;
2751 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002752 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002753 int min = mc->min;
2754 int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002755 unsigned int mask = (1 << fls(max)) - 1;
2756 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002757
Markus Pargmannf227b882014-01-16 16:02:10 +01002758 if (sign_bit)
2759 mask = BIT(sign_bit + 1) - 1;
2760
2761 ucontrol->value.integer.value[0] = snd_soc_read_signed(codec, reg, mask,
2762 shift, sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002763 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002764 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002765 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002766
2767 if (snd_soc_volsw_is_stereo(mc)) {
2768 if (reg == reg2)
2769 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002770 snd_soc_read_signed(codec, reg, mask, rshift,
2771 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002772 else
2773 ucontrol->value.integer.value[1] =
Markus Pargmannf227b882014-01-16 16:02:10 +01002774 snd_soc_read_signed(codec, reg2, mask, shift,
2775 sign_bit) - min;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002776 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002777 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002778 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002779 }
2780
2781 return 0;
2782}
2783EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2784
2785/**
2786 * snd_soc_put_volsw - single mixer put callback
2787 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002788 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002789 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002790 * Callback to set the value of a single mixer control, or a double mixer
2791 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002792 *
2793 * Returns 0 for success.
2794 */
2795int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2796 struct snd_ctl_elem_value *ucontrol)
2797{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002798 struct soc_mixer_control *mc =
2799 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002800 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002801 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002802 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002803 unsigned int shift = mc->shift;
2804 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002805 int max = mc->max;
Markus Pargmannf227b882014-01-16 16:02:10 +01002806 int min = mc->min;
2807 unsigned int sign_bit = mc->sign_bit;
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002808 unsigned int mask = (1 << fls(max)) - 1;
2809 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002810 int err;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002811 bool type_2r = false;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002812 unsigned int val2 = 0;
2813 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002814
Markus Pargmannf227b882014-01-16 16:02:10 +01002815 if (sign_bit)
2816 mask = BIT(sign_bit + 1) - 1;
2817
2818 val = ((ucontrol->value.integer.value[0] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002819 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002820 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002821 val_mask = mask << shift;
2822 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002823 if (snd_soc_volsw_is_stereo(mc)) {
Markus Pargmannf227b882014-01-16 16:02:10 +01002824 val2 = ((ucontrol->value.integer.value[1] + min) & mask);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002825 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002826 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002827 if (reg == reg2) {
2828 val_mask |= mask << rshift;
2829 val |= val2 << rshift;
2830 } else {
2831 val2 = val2 << shift;
Nenghua Cao939d9f12014-03-03 19:08:23 +08002832 type_2r = true;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002833 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002834 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002835 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002836 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002837 return err;
2838
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002839 if (type_2r)
2840 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2841
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002842 return err;
2843}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002844EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002845
Mark Browne13ac2e2008-05-28 17:58:05 +01002846/**
Brian Austin1d99f242012-03-30 10:43:55 -05002847 * snd_soc_get_volsw_sx - single mixer get callback
2848 * @kcontrol: mixer control
2849 * @ucontrol: control element information
2850 *
2851 * Callback to get the value of a single mixer control, or a double mixer
2852 * control that spans 2 registers.
2853 *
2854 * Returns 0 for success.
2855 */
2856int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
2857 struct snd_ctl_elem_value *ucontrol)
2858{
2859 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2860 struct soc_mixer_control *mc =
2861 (struct soc_mixer_control *)kcontrol->private_value;
2862
2863 unsigned int reg = mc->reg;
2864 unsigned int reg2 = mc->rreg;
2865 unsigned int shift = mc->shift;
2866 unsigned int rshift = mc->rshift;
2867 int max = mc->max;
2868 int min = mc->min;
2869 int mask = (1 << (fls(min + max) - 1)) - 1;
2870
2871 ucontrol->value.integer.value[0] =
2872 ((snd_soc_read(codec, reg) >> shift) - min) & mask;
2873
2874 if (snd_soc_volsw_is_stereo(mc))
2875 ucontrol->value.integer.value[1] =
2876 ((snd_soc_read(codec, reg2) >> rshift) - min) & mask;
2877
2878 return 0;
2879}
2880EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
2881
2882/**
2883 * snd_soc_put_volsw_sx - double mixer set callback
2884 * @kcontrol: mixer control
2885 * @uinfo: control element information
2886 *
2887 * Callback to set the value of a double mixer control that spans 2 registers.
2888 *
2889 * Returns 0 for success.
2890 */
2891int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol,
2892 struct snd_ctl_elem_value *ucontrol)
2893{
2894 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2895 struct soc_mixer_control *mc =
2896 (struct soc_mixer_control *)kcontrol->private_value;
2897
2898 unsigned int reg = mc->reg;
2899 unsigned int reg2 = mc->rreg;
2900 unsigned int shift = mc->shift;
2901 unsigned int rshift = mc->rshift;
2902 int max = mc->max;
2903 int min = mc->min;
2904 int mask = (1 << (fls(min + max) - 1)) - 1;
Brian Austin27f1d752012-04-03 11:33:50 -05002905 int err = 0;
Brian Austin1d99f242012-03-30 10:43:55 -05002906 unsigned short val, val_mask, val2 = 0;
2907
2908 val_mask = mask << shift;
2909 val = (ucontrol->value.integer.value[0] + min) & mask;
2910 val = val << shift;
2911
Mukund Navadad0558522012-11-09 11:53:40 +05302912 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2913 if (err < 0)
2914 return err;
Brian Austin1d99f242012-03-30 10:43:55 -05002915
2916 if (snd_soc_volsw_is_stereo(mc)) {
2917 val_mask = mask << rshift;
2918 val2 = (ucontrol->value.integer.value[1] + min) & mask;
2919 val2 = val2 << rshift;
2920
2921 if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2))
2922 return err;
2923 }
2924 return 0;
2925}
2926EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx);
2927
2928/**
Mark Browne13ac2e2008-05-28 17:58:05 +01002929 * snd_soc_info_volsw_s8 - signed mixer info callback
2930 * @kcontrol: mixer control
2931 * @uinfo: control element information
2932 *
2933 * Callback to provide information about a signed mixer control.
2934 *
2935 * Returns 0 for success.
2936 */
2937int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2938 struct snd_ctl_elem_info *uinfo)
2939{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002940 struct soc_mixer_control *mc =
2941 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002942 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002943 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002944
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002945 if (!mc->platform_max)
2946 mc->platform_max = mc->max;
2947 platform_max = mc->platform_max;
2948
Mark Browne13ac2e2008-05-28 17:58:05 +01002949 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2950 uinfo->count = 2;
2951 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002952 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002953 return 0;
2954}
2955EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2956
2957/**
2958 * snd_soc_get_volsw_s8 - signed mixer get callback
2959 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002960 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002961 *
2962 * Callback to get the value of a signed mixer control.
2963 *
2964 * Returns 0 for success.
2965 */
2966int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2967 struct snd_ctl_elem_value *ucontrol)
2968{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002969 struct soc_mixer_control *mc =
2970 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002971 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002972 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002973 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002974 int val = snd_soc_read(codec, reg);
2975
2976 ucontrol->value.integer.value[0] =
2977 ((signed char)(val & 0xff))-min;
2978 ucontrol->value.integer.value[1] =
2979 ((signed char)((val >> 8) & 0xff))-min;
2980 return 0;
2981}
2982EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2983
2984/**
2985 * snd_soc_put_volsw_sgn - signed mixer put callback
2986 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002987 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002988 *
2989 * Callback to set the value of a signed mixer control.
2990 *
2991 * Returns 0 for success.
2992 */
2993int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2994 struct snd_ctl_elem_value *ucontrol)
2995{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002996 struct soc_mixer_control *mc =
2997 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002998 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf8d2008-07-29 10:22:24 -04002999 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01003000 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03003001 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01003002
3003 val = (ucontrol->value.integer.value[0]+min) & 0xff;
3004 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
3005
Eero Nurkkala6c508c62009-10-30 13:34:03 +02003006 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01003007}
3008EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
3009
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003010/**
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003011 * snd_soc_info_volsw_range - single mixer info callback with range.
3012 * @kcontrol: mixer control
3013 * @uinfo: control element information
3014 *
3015 * Callback to provide information, within a range, about a single
3016 * mixer control.
3017 *
3018 * returns 0 for success.
3019 */
3020int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol,
3021 struct snd_ctl_elem_info *uinfo)
3022{
3023 struct soc_mixer_control *mc =
3024 (struct soc_mixer_control *)kcontrol->private_value;
3025 int platform_max;
3026 int min = mc->min;
3027
3028 if (!mc->platform_max)
3029 mc->platform_max = mc->max;
3030 platform_max = mc->platform_max;
3031
3032 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Mark Brown9bde4f02012-12-19 16:05:00 +00003033 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003034 uinfo->value.integer.min = 0;
3035 uinfo->value.integer.max = platform_max - min;
3036
3037 return 0;
3038}
3039EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range);
3040
3041/**
3042 * snd_soc_put_volsw_range - single mixer put value callback with range.
3043 * @kcontrol: mixer control
3044 * @ucontrol: control element information
3045 *
3046 * Callback to set the value, within a range, for a single mixer control.
3047 *
3048 * Returns 0 for success.
3049 */
3050int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol,
3051 struct snd_ctl_elem_value *ucontrol)
3052{
3053 struct soc_mixer_control *mc =
3054 (struct soc_mixer_control *)kcontrol->private_value;
3055 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3056 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003057 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003058 unsigned int shift = mc->shift;
3059 int min = mc->min;
3060 int max = mc->max;
3061 unsigned int mask = (1 << fls(max)) - 1;
3062 unsigned int invert = mc->invert;
3063 unsigned int val, val_mask;
Mark Brown9bde4f02012-12-19 16:05:00 +00003064 int ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003065
3066 val = ((ucontrol->value.integer.value[0] + min) & mask);
3067 if (invert)
3068 val = max - val;
3069 val_mask = mask << shift;
3070 val = val << shift;
3071
Mark Brown9bde4f02012-12-19 16:05:00 +00003072 ret = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Joonyoung Shim0eaa6cc2013-03-26 14:41:05 +09003073 if (ret < 0)
Mark Brown9bde4f02012-12-19 16:05:00 +00003074 return ret;
3075
3076 if (snd_soc_volsw_is_stereo(mc)) {
3077 val = ((ucontrol->value.integer.value[1] + min) & mask);
3078 if (invert)
3079 val = max - val;
3080 val_mask = mask << shift;
3081 val = val << shift;
3082
3083 ret = snd_soc_update_bits_locked(codec, rreg, val_mask, val);
3084 }
3085
3086 return ret;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003087}
3088EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range);
3089
3090/**
3091 * snd_soc_get_volsw_range - single mixer get callback with range
3092 * @kcontrol: mixer control
3093 * @ucontrol: control element information
3094 *
3095 * Callback to get the value, within a range, of a single mixer control.
3096 *
3097 * Returns 0 for success.
3098 */
3099int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
3100 struct snd_ctl_elem_value *ucontrol)
3101{
3102 struct soc_mixer_control *mc =
3103 (struct soc_mixer_control *)kcontrol->private_value;
3104 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3105 unsigned int reg = mc->reg;
Mark Brown9bde4f02012-12-19 16:05:00 +00003106 unsigned int rreg = mc->rreg;
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003107 unsigned int shift = mc->shift;
3108 int min = mc->min;
3109 int max = mc->max;
3110 unsigned int mask = (1 << fls(max)) - 1;
3111 unsigned int invert = mc->invert;
3112
3113 ucontrol->value.integer.value[0] =
3114 (snd_soc_read(codec, reg) >> shift) & mask;
3115 if (invert)
3116 ucontrol->value.integer.value[0] =
3117 max - ucontrol->value.integer.value[0];
3118 ucontrol->value.integer.value[0] =
3119 ucontrol->value.integer.value[0] - min;
3120
Mark Brown9bde4f02012-12-19 16:05:00 +00003121 if (snd_soc_volsw_is_stereo(mc)) {
3122 ucontrol->value.integer.value[1] =
3123 (snd_soc_read(codec, rreg) >> shift) & mask;
3124 if (invert)
3125 ucontrol->value.integer.value[1] =
3126 max - ucontrol->value.integer.value[1];
3127 ucontrol->value.integer.value[1] =
3128 ucontrol->value.integer.value[1] - min;
3129 }
3130
Adam Thomson6c9d8cf2012-05-31 15:18:01 +01003131 return 0;
3132}
3133EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range);
3134
3135/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003136 * snd_soc_limit_volume - Set new limit to an existing volume control.
3137 *
3138 * @codec: where to look for the control
3139 * @name: Name of the control
3140 * @max: new maximum limit
3141 *
3142 * Return 0 for success, else error.
3143 */
3144int snd_soc_limit_volume(struct snd_soc_codec *codec,
3145 const char *name, int max)
3146{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003147 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003148 struct snd_kcontrol *kctl;
3149 struct soc_mixer_control *mc;
3150 int found = 0;
3151 int ret = -EINVAL;
3152
3153 /* Sanity check for name and max */
3154 if (unlikely(!name || max <= 0))
3155 return -EINVAL;
3156
3157 list_for_each_entry(kctl, &card->controls, list) {
3158 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
3159 found = 1;
3160 break;
3161 }
3162 }
3163 if (found) {
3164 mc = (struct soc_mixer_control *)kctl->private_value;
3165 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03003166 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03003167 ret = 0;
3168 }
3169 }
3170 return ret;
3171}
3172EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
3173
Mark Brown71d08512011-10-10 18:31:26 +01003174int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
3175 struct snd_ctl_elem_info *uinfo)
3176{
3177 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3178 struct soc_bytes *params = (void *)kcontrol->private_value;
3179
3180 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
3181 uinfo->count = params->num_regs * codec->val_bytes;
3182
3183 return 0;
3184}
3185EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
3186
3187int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
3188 struct snd_ctl_elem_value *ucontrol)
3189{
3190 struct soc_bytes *params = (void *)kcontrol->private_value;
3191 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3192 int ret;
3193
3194 if (codec->using_regmap)
3195 ret = regmap_raw_read(codec->control_data, params->base,
3196 ucontrol->value.bytes.data,
3197 params->num_regs * codec->val_bytes);
3198 else
3199 ret = -EINVAL;
3200
Mark Brownf831b052012-02-17 16:20:33 -08003201 /* Hide any masked bytes to ensure consistent data reporting */
3202 if (ret == 0 && params->mask) {
3203 switch (codec->val_bytes) {
3204 case 1:
3205 ucontrol->value.bytes.data[0] &= ~params->mask;
3206 break;
3207 case 2:
3208 ((u16 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003209 &= cpu_to_be16(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003210 break;
3211 case 4:
3212 ((u32 *)(&ucontrol->value.bytes.data))[0]
Charles Keepax8f1ec932013-11-29 13:33:11 +00003213 &= cpu_to_be32(~params->mask);
Mark Brownf831b052012-02-17 16:20:33 -08003214 break;
3215 default:
3216 return -EINVAL;
3217 }
3218 }
3219
Mark Brown71d08512011-10-10 18:31:26 +01003220 return ret;
3221}
3222EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
3223
3224int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
3225 struct snd_ctl_elem_value *ucontrol)
3226{
3227 struct soc_bytes *params = (void *)kcontrol->private_value;
3228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08003229 int ret, len;
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003230 unsigned int val, mask;
Mark Brownf831b052012-02-17 16:20:33 -08003231 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01003232
Mark Brownf831b052012-02-17 16:20:33 -08003233 if (!codec->using_regmap)
3234 return -EINVAL;
3235
Mark Brownf831b052012-02-17 16:20:33 -08003236 len = params->num_regs * codec->val_bytes;
3237
Mark Brownb5a8fe42013-01-20 21:42:22 +09003238 data = kmemdup(ucontrol->value.bytes.data, len, GFP_KERNEL | GFP_DMA);
3239 if (!data)
3240 return -ENOMEM;
3241
Mark Brownf831b052012-02-17 16:20:33 -08003242 /*
3243 * If we've got a mask then we need to preserve the register
3244 * bits. We shouldn't modify the incoming data so take a
3245 * copy.
3246 */
3247 if (params->mask) {
3248 ret = regmap_read(codec->control_data, params->base, &val);
3249 if (ret != 0)
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003250 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003251
3252 val &= params->mask;
3253
Mark Brownf831b052012-02-17 16:20:33 -08003254 switch (codec->val_bytes) {
3255 case 1:
3256 ((u8 *)data)[0] &= ~params->mask;
3257 ((u8 *)data)[0] |= val;
3258 break;
3259 case 2:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003260 mask = ~params->mask;
3261 ret = regmap_parse_val(codec->control_data,
3262 &mask, &mask);
3263 if (ret != 0)
3264 goto out;
3265
3266 ((u16 *)data)[0] &= mask;
3267
3268 ret = regmap_parse_val(codec->control_data,
3269 &val, &val);
3270 if (ret != 0)
3271 goto out;
3272
3273 ((u16 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003274 break;
3275 case 4:
Nenghua Caoa1a564e2014-02-19 18:44:58 +08003276 mask = ~params->mask;
3277 ret = regmap_parse_val(codec->control_data,
3278 &mask, &mask);
3279 if (ret != 0)
3280 goto out;
3281
3282 ((u32 *)data)[0] &= mask;
3283
3284 ret = regmap_parse_val(codec->control_data,
3285 &val, &val);
3286 if (ret != 0)
3287 goto out;
3288
3289 ((u32 *)data)[0] |= val;
Mark Brownf831b052012-02-17 16:20:33 -08003290 break;
3291 default:
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003292 ret = -EINVAL;
3293 goto out;
Mark Brownf831b052012-02-17 16:20:33 -08003294 }
3295 }
3296
3297 ret = regmap_raw_write(codec->control_data, params->base,
3298 data, len);
3299
Wei Yongjune8b18ad2013-03-12 00:35:14 +08003300out:
Mark Brownb5a8fe42013-01-20 21:42:22 +09003301 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01003302
3303 return ret;
3304}
3305EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
3306
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02003307/**
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003308 * snd_soc_info_xr_sx - signed multi register info callback
3309 * @kcontrol: mreg control
3310 * @uinfo: control element information
3311 *
3312 * Callback to provide information of a control that can
3313 * span multiple codec registers which together
3314 * forms a single signed value in a MSB/LSB manner.
3315 *
3316 * Returns 0 for success.
3317 */
3318int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol,
3319 struct snd_ctl_elem_info *uinfo)
3320{
3321 struct soc_mreg_control *mc =
3322 (struct soc_mreg_control *)kcontrol->private_value;
3323 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
3324 uinfo->count = 1;
3325 uinfo->value.integer.min = mc->min;
3326 uinfo->value.integer.max = mc->max;
3327
3328 return 0;
3329}
3330EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx);
3331
3332/**
3333 * snd_soc_get_xr_sx - signed multi register get callback
3334 * @kcontrol: mreg control
3335 * @ucontrol: control element information
3336 *
3337 * Callback to get the value of a control that can span
3338 * multiple codec registers which together forms a single
3339 * signed value in a MSB/LSB manner. The control supports
3340 * specifying total no of bits used to allow for bitfields
3341 * across the multiple codec registers.
3342 *
3343 * Returns 0 for success.
3344 */
3345int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol,
3346 struct snd_ctl_elem_value *ucontrol)
3347{
3348 struct soc_mreg_control *mc =
3349 (struct soc_mreg_control *)kcontrol->private_value;
3350 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3351 unsigned int regbase = mc->regbase;
3352 unsigned int regcount = mc->regcount;
3353 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3354 unsigned int regwmask = (1<<regwshift)-1;
3355 unsigned int invert = mc->invert;
3356 unsigned long mask = (1UL<<mc->nbits)-1;
3357 long min = mc->min;
3358 long max = mc->max;
3359 long val = 0;
3360 unsigned long regval;
3361 unsigned int i;
3362
3363 for (i = 0; i < regcount; i++) {
3364 regval = snd_soc_read(codec, regbase+i) & regwmask;
3365 val |= regval << (regwshift*(regcount-i-1));
3366 }
3367 val &= mask;
3368 if (min < 0 && val > max)
3369 val |= ~mask;
3370 if (invert)
3371 val = max - val;
3372 ucontrol->value.integer.value[0] = val;
3373
3374 return 0;
3375}
3376EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx);
3377
3378/**
3379 * snd_soc_put_xr_sx - signed multi register get callback
3380 * @kcontrol: mreg control
3381 * @ucontrol: control element information
3382 *
3383 * Callback to set the value of a control that can span
3384 * multiple codec registers which together forms a single
3385 * signed value in a MSB/LSB manner. The control supports
3386 * specifying total no of bits used to allow for bitfields
3387 * across the multiple codec registers.
3388 *
3389 * Returns 0 for success.
3390 */
3391int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol,
3392 struct snd_ctl_elem_value *ucontrol)
3393{
3394 struct soc_mreg_control *mc =
3395 (struct soc_mreg_control *)kcontrol->private_value;
3396 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3397 unsigned int regbase = mc->regbase;
3398 unsigned int regcount = mc->regcount;
3399 unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE;
3400 unsigned int regwmask = (1<<regwshift)-1;
3401 unsigned int invert = mc->invert;
3402 unsigned long mask = (1UL<<mc->nbits)-1;
Kristoffer KARLSSON4183eed22012-04-20 11:32:13 +02003403 long max = mc->max;
3404 long val = ucontrol->value.integer.value[0];
3405 unsigned int i, regval, regmask;
3406 int err;
3407
3408 if (invert)
3409 val = max - val;
3410 val &= mask;
3411 for (i = 0; i < regcount; i++) {
3412 regval = (val >> (regwshift*(regcount-i-1))) & regwmask;
3413 regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask;
3414 err = snd_soc_update_bits_locked(codec, regbase+i,
3415 regmask, regval);
3416 if (err < 0)
3417 return err;
3418 }
3419
3420 return 0;
3421}
3422EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx);
3423
3424/**
Kristoffer KARLSSONdd7b10b2012-04-20 11:32:44 +02003425 * snd_soc_get_strobe - strobe get callback
3426 * @kcontrol: mixer control
3427 * @ucontrol: control element information
3428 *
3429 * Callback get the value of a strobe mixer control.
3430 *
3431 * Returns 0 for success.
3432 */
3433int snd_soc_get_strobe(struct snd_kcontrol *kcontrol,
3434 struct snd_ctl_elem_value *ucontrol)
3435{
3436 struct soc_mixer_control *mc =
3437 (struct soc_mixer_control *)kcontrol->private_value;
3438 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3439 unsigned int reg = mc->reg;
3440 unsigned int shift = mc->shift;
3441 unsigned int mask = 1 << shift;
3442 unsigned int invert = mc->invert != 0;
3443 unsigned int val = snd_soc_read(codec, reg) & mask;
3444
3445 if (shift != 0 && val != 0)
3446 val = val >> shift;
3447 ucontrol->value.enumerated.item[0] = val ^ invert;
3448
3449 return 0;
3450}
3451EXPORT_SYMBOL_GPL(snd_soc_get_strobe);
3452
3453/**
3454 * snd_soc_put_strobe - strobe put callback
3455 * @kcontrol: mixer control
3456 * @ucontrol: control element information
3457 *
3458 * Callback strobe a register bit to high then low (or the inverse)
3459 * in one pass of a single mixer enum control.
3460 *
3461 * Returns 1 for success.
3462 */
3463int snd_soc_put_strobe(struct snd_kcontrol *kcontrol,
3464 struct snd_ctl_elem_value *ucontrol)
3465{
3466 struct soc_mixer_control *mc =
3467 (struct soc_mixer_control *)kcontrol->private_value;
3468 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
3469 unsigned int reg = mc->reg;
3470 unsigned int shift = mc->shift;
3471 unsigned int mask = 1 << shift;
3472 unsigned int invert = mc->invert != 0;
3473 unsigned int strobe = ucontrol->value.enumerated.item[0] != 0;
3474 unsigned int val1 = (strobe ^ invert) ? mask : 0;
3475 unsigned int val2 = (strobe ^ invert) ? 0 : mask;
3476 int err;
3477
3478 err = snd_soc_update_bits_locked(codec, reg, mask, val1);
3479 if (err < 0)
3480 return err;
3481
3482 err = snd_soc_update_bits_locked(codec, reg, mask, val2);
3483 return err;
3484}
3485EXPORT_SYMBOL_GPL(snd_soc_put_strobe);
3486
3487/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003488 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
3489 * @dai: DAI
3490 * @clk_id: DAI specific clock ID
3491 * @freq: new clock frequency in Hz
3492 * @dir: new clock direction - input/output.
3493 *
3494 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
3495 */
3496int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
3497 unsigned int freq, int dir)
3498{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003499 if (dai->driver && dai->driver->ops->set_sysclk)
3500 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003501 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003502 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00003503 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003504 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003505 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003506}
3507EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
3508
3509/**
Mark Brownec4ee522011-03-07 20:58:11 +00003510 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
3511 * @codec: CODEC
3512 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01003513 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00003514 * @freq: new clock frequency in Hz
3515 * @dir: new clock direction - input/output.
3516 *
3517 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
3518 */
3519int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01003520 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00003521{
3522 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01003523 return codec->driver->set_sysclk(codec, clk_id, source,
3524 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00003525 else
Mark Brown1104a9c2014-01-15 19:04:19 +00003526 return -ENOTSUPP;
Mark Brownec4ee522011-03-07 20:58:11 +00003527}
3528EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
3529
3530/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003531 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
3532 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00003533 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003534 * @div: new clock divisor.
3535 *
3536 * Configures the clock dividers. This is used to derive the best DAI bit and
3537 * frame clocks from the system or master clock. It's best to set the DAI bit
3538 * and frame clocks as low as possible to save system power.
3539 */
3540int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
3541 int div_id, int div)
3542{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003543 if (dai->driver && dai->driver->ops->set_clkdiv)
3544 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003545 else
3546 return -EINVAL;
3547}
3548EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
3549
3550/**
3551 * snd_soc_dai_set_pll - configure DAI PLL.
3552 * @dai: DAI
3553 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01003554 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003555 * @freq_in: PLL input clock frequency in Hz
3556 * @freq_out: requested PLL output clock frequency in Hz
3557 *
3558 * Configures and enables PLL to generate output clock based on input clock.
3559 */
Mark Brown85488032009-09-05 18:52:16 +01003560int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
3561 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003562{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003563 if (dai->driver && dai->driver->ops->set_pll)
3564 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01003565 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00003566 else if (dai->codec && dai->codec->driver->set_pll)
3567 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
3568 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003569 else
3570 return -EINVAL;
3571}
3572EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
3573
Mark Brownec4ee522011-03-07 20:58:11 +00003574/*
3575 * snd_soc_codec_set_pll - configure codec PLL.
3576 * @codec: CODEC
3577 * @pll_id: DAI specific PLL ID
3578 * @source: DAI specific source for the PLL
3579 * @freq_in: PLL input clock frequency in Hz
3580 * @freq_out: requested PLL output clock frequency in Hz
3581 *
3582 * Configures and enables PLL to generate output clock based on input clock.
3583 */
3584int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
3585 unsigned int freq_in, unsigned int freq_out)
3586{
3587 if (codec->driver->set_pll)
3588 return codec->driver->set_pll(codec, pll_id, source,
3589 freq_in, freq_out);
3590 else
3591 return -EINVAL;
3592}
3593EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
3594
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003595/**
Liam Girdwoode54cf762013-09-16 13:01:46 +01003596 * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio.
3597 * @dai: DAI
3598 * @ratio Ratio of BCLK to Sample rate.
3599 *
3600 * Configures the DAI for a preset BCLK to sample rate ratio.
3601 */
3602int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio)
3603{
3604 if (dai->driver && dai->driver->ops->set_bclk_ratio)
3605 return dai->driver->ops->set_bclk_ratio(dai, ratio);
3606 else
3607 return -EINVAL;
3608}
3609EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio);
3610
3611/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003612 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
3613 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003614 * @fmt: SND_SOC_DAIFMT_ format value.
3615 *
3616 * Configures the DAI hardware format and clocking.
3617 */
3618int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
3619{
Shawn Guo5e4ba562012-03-09 00:59:40 +08003620 if (dai->driver == NULL)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003621 return -EINVAL;
Shawn Guo5e4ba562012-03-09 00:59:40 +08003622 if (dai->driver->ops->set_fmt == NULL)
3623 return -ENOTSUPP;
3624 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003625}
3626EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
3627
3628/**
Xiubo Lie5c21512014-03-21 14:17:12 +08003629 * snd_soc_xlate_tdm_slot - generate tx/rx slot mask.
Xiubo Li89c67852014-02-14 09:34:35 +08003630 * @slots: Number of slots in use.
3631 * @tx_mask: bitmask representing active TX slots.
3632 * @rx_mask: bitmask representing active RX slots.
3633 *
3634 * Generates the TDM tx and rx slot default masks for DAI.
3635 */
Xiubo Lie5c21512014-03-21 14:17:12 +08003636static int snd_soc_xlate_tdm_slot_mask(unsigned int slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003637 unsigned int *tx_mask,
3638 unsigned int *rx_mask)
3639{
3640 if (*tx_mask || *rx_mask)
3641 return 0;
3642
3643 if (!slots)
3644 return -EINVAL;
3645
3646 *tx_mask = (1 << slots) - 1;
3647 *rx_mask = (1 << slots) - 1;
3648
3649 return 0;
3650}
3651
3652/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003653 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
3654 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003655 * @tx_mask: bitmask representing active TX slots.
3656 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003657 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003658 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003659 *
3660 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
3661 * specific.
3662 */
3663int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003664 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003665{
Xiubo Lie5c21512014-03-21 14:17:12 +08003666 if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask)
3667 dai->driver->ops->xlate_tdm_slot_mask(slots,
Xiubo Li89c67852014-02-14 09:34:35 +08003668 &tx_mask, &rx_mask);
3669 else
Xiubo Lie5c21512014-03-21 14:17:12 +08003670 snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask);
Xiubo Li89c67852014-02-14 09:34:35 +08003671
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003672 if (dai->driver && dai->driver->ops->set_tdm_slot)
3673 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03003674 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003675 else
Xiubo Lib2cbb6e2014-01-23 13:02:47 +08003676 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003677}
3678EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
3679
3680/**
Barry Song472df3c2009-09-12 01:16:29 +08003681 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3682 * @dai: DAI
3683 * @tx_num: how many TX channels
3684 * @tx_slot: pointer to an array which imply the TX slot number channel
3685 * 0~num-1 uses
3686 * @rx_num: how many RX channels
3687 * @rx_slot: pointer to an array which imply the RX slot number channel
3688 * 0~num-1 uses
3689 *
3690 * configure the relationship between channel number and TDM slot number.
3691 */
3692int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3693 unsigned int tx_num, unsigned int *tx_slot,
3694 unsigned int rx_num, unsigned int *rx_slot)
3695{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003696 if (dai->driver && dai->driver->ops->set_channel_map)
3697 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003698 rx_num, rx_slot);
3699 else
3700 return -EINVAL;
3701}
3702EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3703
3704/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003705 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3706 * @dai: DAI
3707 * @tristate: tristate enable
3708 *
3709 * Tristates the DAI so that others can use it.
3710 */
3711int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3712{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003713 if (dai->driver && dai->driver->ops->set_tristate)
3714 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003715 else
3716 return -EINVAL;
3717}
3718EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3719
3720/**
3721 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3722 * @dai: DAI
3723 * @mute: mute enable
Mark Brownda183962013-02-06 15:44:07 +00003724 * @direction: stream to mute
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003725 *
3726 * Mutes the DAI DAC.
3727 */
Mark Brownda183962013-02-06 15:44:07 +00003728int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute,
3729 int direction)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003730{
Mark Brownda183962013-02-06 15:44:07 +00003731 if (!dai->driver)
3732 return -ENOTSUPP;
3733
3734 if (dai->driver->ops->mute_stream)
3735 return dai->driver->ops->mute_stream(dai, mute, direction);
3736 else if (direction == SNDRV_PCM_STREAM_PLAYBACK &&
3737 dai->driver->ops->digital_mute)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003738 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003739 else
Mark Brown04570c62012-04-13 19:16:03 +01003740 return -ENOTSUPP;
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003741}
3742EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3743
Mark Brownc5af3a22008-11-28 13:29:45 +00003744/**
3745 * snd_soc_register_card - Register a card with the ASoC core
3746 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003747 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003748 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003749 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303750int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003751{
Mark Brownb19e6e72012-03-14 21:18:39 +00003752 int i, ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003753
Mark Brownc5af3a22008-11-28 13:29:45 +00003754 if (!card->name || !card->dev)
3755 return -EINVAL;
3756
Stephen Warren5a504962011-12-21 10:40:59 -07003757 for (i = 0; i < card->num_links; i++) {
3758 struct snd_soc_dai_link *link = &card->dai_link[i];
3759
3760 /*
3761 * Codec must be specified by 1 of name or OF node,
3762 * not both or neither.
3763 */
3764 if (!!link->codec_name == !!link->codec_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003765 dev_err(card->dev,
3766 "ASoC: Neither/both codec name/of_node are set for %s\n",
3767 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003768 return -EINVAL;
3769 }
Stephen Warrenbc926572012-05-25 18:22:11 -06003770 /* Codec DAI name must be specified */
3771 if (!link->codec_dai_name) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003772 dev_err(card->dev,
3773 "ASoC: codec_dai_name not set for %s\n",
3774 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003775 return -EINVAL;
3776 }
Stephen Warren5a504962011-12-21 10:40:59 -07003777
3778 /*
3779 * Platform may be specified by either name or OF node, but
3780 * can be left unspecified, and a dummy platform will be used.
3781 */
3782 if (link->platform_name && link->platform_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003783 dev_err(card->dev,
3784 "ASoC: Both platform name/of_node are set for %s\n",
3785 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003786 return -EINVAL;
3787 }
3788
3789 /*
Stephen Warrenbc926572012-05-25 18:22:11 -06003790 * CPU device may be specified by either name or OF node, but
3791 * can be left unspecified, and will be matched based on DAI
3792 * name alone..
Stephen Warren5a504962011-12-21 10:40:59 -07003793 */
Stephen Warrenbc926572012-05-25 18:22:11 -06003794 if (link->cpu_name && link->cpu_of_node) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003795 dev_err(card->dev,
3796 "ASoC: Neither/both cpu name/of_node are set for %s\n",
3797 link->name);
Stephen Warrenbc926572012-05-25 18:22:11 -06003798 return -EINVAL;
3799 }
3800 /*
3801 * At least one of CPU DAI name or CPU device name/node must be
3802 * specified
3803 */
3804 if (!link->cpu_dai_name &&
3805 !(link->cpu_name || link->cpu_of_node)) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003806 dev_err(card->dev,
3807 "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n",
3808 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003809 return -EINVAL;
3810 }
3811 }
3812
Mark Browned77cc12011-05-03 18:25:34 +01003813 dev_set_drvdata(card->dev, card);
3814
Stephen Warren111c6412011-01-28 14:26:35 -07003815 snd_soc_initialize_card_lists(card);
3816
Vinod Koul150dd2f2011-01-13 22:48:32 +05303817 soc_init_card_debugfs(card);
3818
Mark Brown181a6892012-03-14 20:18:49 +00003819 card->rtd = devm_kzalloc(card->dev,
3820 sizeof(struct snd_soc_pcm_runtime) *
3821 (card->num_links + card->num_aux_devs),
3822 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003823 if (card->rtd == NULL)
3824 return -ENOMEM;
Liam Girdwooda7dbb602012-04-17 18:00:11 +01003825 card->num_rtd = 0;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003826 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003827
3828 for (i = 0; i < card->num_links; i++)
3829 card->rtd[i].dai_link = &card->dai_link[i];
3830
Mark Brownc5af3a22008-11-28 13:29:45 +00003831 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003832 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003833 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003834 mutex_init(&card->mutex);
Liam Girdwooda73fb2d2012-03-07 10:38:26 +00003835 mutex_init(&card->dapm_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003836
Mark Brownb19e6e72012-03-14 21:18:39 +00003837 ret = snd_soc_instantiate_card(card);
3838 if (ret != 0)
3839 soc_cleanup_card_debugfs(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003840
Nicolin Chen988e8cc2013-11-04 14:57:31 +08003841 /* deactivate pins to sleep state */
3842 for (i = 0; i < card->num_rtd; i++) {
3843 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
3844 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
3845 if (!codec_dai->active)
3846 pinctrl_pm_select_sleep_state(codec_dai->dev);
3847 if (!cpu_dai->active)
3848 pinctrl_pm_select_sleep_state(cpu_dai->dev);
3849 }
3850
Mark Brownb19e6e72012-03-14 21:18:39 +00003851 return ret;
Mark Brownc5af3a22008-11-28 13:29:45 +00003852}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303853EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003854
3855/**
3856 * snd_soc_unregister_card - Unregister a card with the ASoC core
3857 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003858 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003859 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003860 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303861int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003862{
Vinod Koulb0e26482011-01-13 22:48:02 +05303863 if (card->instantiated)
3864 soc_cleanup_card_resources(card);
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003865 dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name);
Mark Brownc5af3a22008-11-28 13:29:45 +00003866
3867 return 0;
3868}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303869EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003870
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003871/*
3872 * Simplify DAI link configuration by removing ".-1" from device names
3873 * and sanitizing names.
3874 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003875static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003876{
3877 char *found, name[NAME_SIZE];
3878 int id1, id2;
3879
3880 if (dev_name(dev) == NULL)
3881 return NULL;
3882
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003883 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003884
3885 /* are we a "%s.%d" name (platform and SPI components) */
3886 found = strstr(name, dev->driver->name);
3887 if (found) {
3888 /* get ID */
3889 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3890
3891 /* discard ID from name if ID == -1 */
3892 if (*id == -1)
3893 found[strlen(dev->driver->name)] = '\0';
3894 }
3895
3896 } else {
3897 /* I2C component devices are named "bus-addr" */
3898 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3899 char tmp[NAME_SIZE];
3900
3901 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003902 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003903
3904 /* sanitize component name for DAI link creation */
3905 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003906 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003907 } else
3908 *id = 0;
3909 }
3910
3911 return kstrdup(name, GFP_KERNEL);
3912}
3913
3914/*
3915 * Simplify DAI link naming for single devices with multiple DAIs by removing
3916 * any ".-1" and using the DAI name (instead of device name).
3917 */
3918static inline char *fmt_multiple_name(struct device *dev,
3919 struct snd_soc_dai_driver *dai_drv)
3920{
3921 if (dai_drv->name == NULL) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02003922 dev_err(dev,
3923 "ASoC: error - multiple DAI %s registered with no name\n",
3924 dev_name(dev));
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003925 return NULL;
3926 }
3927
3928 return kstrdup(dai_drv->name, GFP_KERNEL);
3929}
3930
Mark Brown91151712008-11-30 23:31:24 +00003931/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003932 * snd_soc_unregister_dai - Unregister DAIs from the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003933 *
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003934 * @component: The component for which the DAIs should be unregistered
Mark Brown91151712008-11-30 23:31:24 +00003935 */
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003936static void snd_soc_unregister_dais(struct snd_soc_component *component)
Mark Brown91151712008-11-30 23:31:24 +00003937{
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003938 struct snd_soc_dai *dai, *_dai;
Mark Brown91151712008-11-30 23:31:24 +00003939
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003940 list_for_each_entry_safe(dai, _dai, &component->dai_list, list) {
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003941 dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n",
3942 dai->name);
Lars-Peter Clausen5c1d5f02014-03-12 08:34:39 +01003943 list_del(&dai->list);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003944 kfree(dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003945 kfree(dai);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003946 }
Mark Brown91151712008-11-30 23:31:24 +00003947}
Mark Brown91151712008-11-30 23:31:24 +00003948
3949/**
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003950 * snd_soc_register_dais - Register a DAI with the ASoC core
Mark Brown91151712008-11-30 23:31:24 +00003951 *
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003952 * @component: The component the DAIs are registered for
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003953 * @codec: The CODEC that the DAIs are registered for, NULL if the component is
3954 * not a CODEC.
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003955 * @dai_drv: DAI driver to use for the DAIs
Mark Brownac11a2b2009-01-01 12:18:17 +00003956 * @count: Number of DAIs
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003957 * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the
3958 * parent's name.
Mark Brown91151712008-11-30 23:31:24 +00003959 */
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003960static int snd_soc_register_dais(struct snd_soc_component *component,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01003961 struct snd_soc_codec *codec, struct snd_soc_dai_driver *dai_drv,
3962 size_t count, bool legacy_dai_naming)
Mark Brown91151712008-11-30 23:31:24 +00003963{
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01003964 struct device *dev = component->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003965 struct snd_soc_dai *dai;
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003966 unsigned int i;
3967 int ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003968
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00003969 dev_dbg(dev, "ASoC: dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003970
3971 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003972
3973 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003974 if (dai == NULL) {
3975 ret = -ENOMEM;
3976 goto err;
3977 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003978
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003979 /*
3980 * Back in the old days when we still had component-less DAIs,
3981 * instead of having a static name, component-less DAIs would
3982 * inherit the name of the parent device so it is possible to
3983 * register multiple instances of the DAI. We still need to keep
3984 * the same naming style even though those DAIs are not
3985 * component-less anymore.
3986 */
3987 if (count == 1 && legacy_dai_naming) {
3988 dai->name = fmt_single_name(dev, &dai->id);
3989 } else {
3990 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3991 if (dai_drv[i].id)
3992 dai->id = dai_drv[i].id;
3993 else
3994 dai->id = i;
3995 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003996 if (dai->name == NULL) {
3997 kfree(dai);
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01003998 ret = -ENOMEM;
Mark Brown91151712008-11-30 23:31:24 +00003999 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004000 }
4001
Lars-Peter Clausen6106d122014-03-05 13:17:46 +01004002 dai->component = component;
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004003 dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004004 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004005 dai->driver = &dai_drv[i];
Liam Girdwoodbe09ad92012-03-07 11:47:41 +00004006 dai->dapm.dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004007 if (!dai->driver->ops)
4008 dai->driver->ops = &null_dai_ops;
4009
Peter Ujfalusi5f800082012-08-07 10:24:13 +03004010 if (!dai->codec)
4011 dai->dapm.idle_bias_off = 1;
4012
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004013 list_add(&dai->list, &component->dai_list);
Mark Brown054880f2012-04-09 17:29:19 +01004014
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004015 dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00004016 }
4017
4018 return 0;
4019
4020err:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004021 snd_soc_unregister_dais(component);
Mark Brown91151712008-11-30 23:31:24 +00004022
4023 return ret;
4024}
Mark Brown91151712008-11-30 23:31:24 +00004025
4026/**
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004027 * snd_soc_register_component - Register a component with the ASoC core
4028 *
4029 */
4030static int
4031__snd_soc_register_component(struct device *dev,
4032 struct snd_soc_component *cmpnt,
4033 const struct snd_soc_component_driver *cmpnt_drv,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004034 struct snd_soc_codec *codec,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004035 struct snd_soc_dai_driver *dai_drv,
4036 int num_dai, bool allow_single_dai)
4037{
4038 int ret;
4039
4040 dev_dbg(dev, "component register %s\n", dev_name(dev));
4041
4042 if (!cmpnt) {
4043 dev_err(dev, "ASoC: Failed to connecting component\n");
4044 return -ENOMEM;
4045 }
4046
4047 cmpnt->name = fmt_single_name(dev, &cmpnt->id);
4048 if (!cmpnt->name) {
4049 dev_err(dev, "ASoC: Failed to simplifying name\n");
4050 return -ENOMEM;
4051 }
4052
4053 cmpnt->dev = dev;
4054 cmpnt->driver = cmpnt_drv;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004055 cmpnt->dai_drv = dai_drv;
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004056 cmpnt->num_dai = num_dai;
Lars-Peter Clausen1438c2f2014-03-09 17:41:47 +01004057 INIT_LIST_HEAD(&cmpnt->dai_list);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004058
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004059 ret = snd_soc_register_dais(cmpnt, codec, dai_drv, num_dai,
4060 allow_single_dai);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004061 if (ret < 0) {
4062 dev_err(dev, "ASoC: Failed to regster DAIs: %d\n", ret);
4063 goto error_component_name;
4064 }
4065
4066 mutex_lock(&client_mutex);
4067 list_add(&cmpnt->list, &component_list);
4068 mutex_unlock(&client_mutex);
4069
4070 dev_dbg(cmpnt->dev, "ASoC: Registered component '%s'\n", cmpnt->name);
4071
4072 return ret;
4073
4074error_component_name:
4075 kfree(cmpnt->name);
4076
4077 return ret;
4078}
4079
4080int snd_soc_register_component(struct device *dev,
4081 const struct snd_soc_component_driver *cmpnt_drv,
4082 struct snd_soc_dai_driver *dai_drv,
4083 int num_dai)
4084{
4085 struct snd_soc_component *cmpnt;
4086
4087 cmpnt = devm_kzalloc(dev, sizeof(*cmpnt), GFP_KERNEL);
4088 if (!cmpnt) {
4089 dev_err(dev, "ASoC: Failed to allocate memory\n");
4090 return -ENOMEM;
4091 }
4092
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004093 cmpnt->ignore_pmdown_time = true;
4094
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004095 return __snd_soc_register_component(dev, cmpnt, cmpnt_drv, NULL,
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004096 dai_drv, num_dai, true);
4097}
4098EXPORT_SYMBOL_GPL(snd_soc_register_component);
4099
4100/**
4101 * snd_soc_unregister_component - Unregister a component from the ASoC core
4102 *
4103 */
4104void snd_soc_unregister_component(struct device *dev)
4105{
4106 struct snd_soc_component *cmpnt;
4107
4108 list_for_each_entry(cmpnt, &component_list, list) {
4109 if (dev == cmpnt->dev)
4110 goto found;
4111 }
4112 return;
4113
4114found:
Lars-Peter Clausen32c9ba52014-03-09 17:41:45 +01004115 snd_soc_unregister_dais(cmpnt);
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004116
4117 mutex_lock(&client_mutex);
4118 list_del(&cmpnt->list);
4119 mutex_unlock(&client_mutex);
4120
4121 dev_dbg(dev, "ASoC: Unregistered component '%s'\n", cmpnt->name);
4122 kfree(cmpnt->name);
4123}
4124EXPORT_SYMBOL_GPL(snd_soc_unregister_component);
4125
4126/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004127 * snd_soc_add_platform - Add a platform to the ASoC core
4128 * @dev: The parent device for the platform
4129 * @platform: The platform to add
4130 * @platform_driver: The driver for the platform
Mark Brown12a48a8c2008-12-03 19:40:30 +00004131 */
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004132int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform,
Lars-Peter Clausend79e57d2013-03-27 12:02:22 +01004133 const struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004134{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004135 /* create platform component name */
4136 platform->name = fmt_single_name(dev, &platform->id);
Dan Carpenterb5c745f2013-07-22 09:56:54 +03004137 if (platform->name == NULL)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004138 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004139
4140 platform->dev = dev;
4141 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01004142 platform->dapm.dev = dev;
4143 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004144 platform->dapm.stream_event = platform_drv->stream_event;
Liam Girdwoodcc22d372012-03-06 18:16:18 +00004145 mutex_init(&platform->mutex);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004146
4147 mutex_lock(&client_mutex);
4148 list_add(&platform->list, &platform_list);
4149 mutex_unlock(&client_mutex);
4150
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004151 dev_dbg(dev, "ASoC: Registered platform '%s'\n", platform->name);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004152
4153 return 0;
4154}
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004155EXPORT_SYMBOL_GPL(snd_soc_add_platform);
4156
4157/**
4158 * snd_soc_register_platform - Register a platform with the ASoC core
4159 *
4160 * @platform: platform to register
4161 */
4162int snd_soc_register_platform(struct device *dev,
4163 const struct snd_soc_platform_driver *platform_drv)
4164{
4165 struct snd_soc_platform *platform;
4166 int ret;
4167
4168 dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev));
4169
4170 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
4171 if (platform == NULL)
4172 return -ENOMEM;
4173
4174 ret = snd_soc_add_platform(dev, platform, platform_drv);
4175 if (ret)
4176 kfree(platform);
4177
4178 return ret;
4179}
Mark Brown12a48a8c2008-12-03 19:40:30 +00004180EXPORT_SYMBOL_GPL(snd_soc_register_platform);
4181
4182/**
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004183 * snd_soc_remove_platform - Remove a platform from the ASoC core
4184 * @platform: the platform to remove
4185 */
4186void snd_soc_remove_platform(struct snd_soc_platform *platform)
4187{
4188 mutex_lock(&client_mutex);
4189 list_del(&platform->list);
4190 mutex_unlock(&client_mutex);
4191
4192 dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n",
4193 platform->name);
4194 kfree(platform->name);
4195}
4196EXPORT_SYMBOL_GPL(snd_soc_remove_platform);
4197
4198struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev)
4199{
4200 struct snd_soc_platform *platform;
4201
4202 list_for_each_entry(platform, &platform_list, list) {
4203 if (dev == platform->dev)
4204 return platform;
4205 }
4206
4207 return NULL;
4208}
4209EXPORT_SYMBOL_GPL(snd_soc_lookup_platform);
4210
4211/**
Mark Brown12a48a8c2008-12-03 19:40:30 +00004212 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
4213 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004214 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00004215 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004216void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00004217{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004218 struct snd_soc_platform *platform;
4219
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004220 platform = snd_soc_lookup_platform(dev);
4221 if (!platform)
4222 return;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004223
Lars-Peter Clausen71a45cd2013-04-15 19:19:49 +02004224 snd_soc_remove_platform(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004225 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00004226}
4227EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
4228
Mark Brown151ab222009-05-09 16:22:58 +01004229static u64 codec_format_map[] = {
4230 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
4231 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
4232 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
4233 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
4234 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
4235 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
4236 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4237 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
4238 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
4239 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
4240 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
4241 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
4242 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
4243 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
4244 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
4245 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
4246};
4247
4248/* Fix up the DAI formats for endianness: codecs don't actually see
4249 * the endianness of the data but we're using the CPU format
4250 * definitions which do need to include endianness so we ensure that
4251 * codec DAIs always have both big and little endian variants set.
4252 */
4253static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
4254{
4255 int i;
4256
4257 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
4258 if (stream->formats & codec_format_map[i])
4259 stream->formats |= codec_format_map[i];
4260}
4261
Mark Brown0d0cf002008-12-10 14:32:45 +00004262/**
4263 * snd_soc_register_codec - Register a codec with the ASoC core
4264 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004265 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00004266 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004267int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00004268 const struct snd_soc_codec_driver *codec_drv,
4269 struct snd_soc_dai_driver *dai_drv,
4270 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00004271{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004272 struct snd_soc_codec *codec;
4273 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01004274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004275 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00004276
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004277 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
4278 if (codec == NULL)
4279 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00004280
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004281 /* create CODEC component name */
4282 codec->name = fmt_single_name(dev, &codec->id);
4283 if (codec->name == NULL) {
Kuninori Morimotof790b942013-02-25 00:40:09 -08004284 ret = -ENOMEM;
4285 goto fail_codec;
Mark Brown151ab222009-05-09 16:22:58 +01004286 }
4287
Mark Brownc3acec22010-12-02 16:15:29 +00004288 codec->write = codec_drv->write;
4289 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00004290 codec->volatile_register = codec_drv->volatile_register;
4291 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00004292 codec->writable_register = codec_drv->writable_register;
Lars-Peter Clausen3d594002014-03-05 13:17:48 +01004293 codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02004294 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
4295 codec->dapm.dev = dev;
4296 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00004297 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01004298 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004299 codec->dev = dev;
4300 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004301 codec->num_dai = num_dai;
4302 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004303
4304 for (i = 0; i < num_dai; i++) {
4305 fixup_codec_formats(&dai_drv[i].playback);
4306 fixup_codec_formats(&dai_drv[i].capture);
4307 }
4308
Mark Brown054880f2012-04-09 17:29:19 +01004309 mutex_lock(&client_mutex);
4310 list_add(&codec->list, &codec_list);
4311 mutex_unlock(&client_mutex);
4312
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004313 /* register component */
4314 ret = __snd_soc_register_component(dev, &codec->component,
4315 &codec_drv->component_driver,
Lars-Peter Clausen6cc240f2014-03-09 17:41:46 +01004316 codec, dai_drv, num_dai, false);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004317 if (ret < 0) {
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004318 dev_err(codec->dev, "ASoC: Failed to regster component: %d\n", ret);
Kuninori Morimoto5acd7df2013-02-25 00:40:40 -08004319 goto fail_codec_name;
Mark Brown26b01cc2010-08-18 20:20:55 +01004320 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004321
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004322 dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00004323 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004324
Kuninori Morimotof790b942013-02-25 00:40:09 -08004325fail_codec_name:
Kuninori Morimotoe1328a82013-03-07 17:42:33 -08004326 mutex_lock(&client_mutex);
4327 list_del(&codec->list);
4328 mutex_unlock(&client_mutex);
4329
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004330 kfree(codec->name);
Kuninori Morimotof790b942013-02-25 00:40:09 -08004331fail_codec:
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004332 kfree(codec);
4333 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00004334}
4335EXPORT_SYMBOL_GPL(snd_soc_register_codec);
4336
4337/**
4338 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
4339 *
Mark Brownac11a2b2009-01-01 12:18:17 +00004340 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00004341 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004342void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00004343{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004344 struct snd_soc_codec *codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004345
4346 list_for_each_entry(codec, &codec_list, list) {
4347 if (dev == codec->dev)
4348 goto found;
4349 }
4350 return;
4351
4352found:
Kuninori Morimotod191bd82013-09-04 19:39:03 -07004353 snd_soc_unregister_component(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004354
Mark Brown0d0cf002008-12-10 14:32:45 +00004355 mutex_lock(&client_mutex);
4356 list_del(&codec->list);
4357 mutex_unlock(&client_mutex);
4358
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004359 dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004360
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00004361 snd_soc_cache_exit(codec);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01004362 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00004363 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00004364}
4365EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
4366
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004367/* Retrieve a card's name from device tree */
4368int snd_soc_of_parse_card_name(struct snd_soc_card *card,
4369 const char *propname)
4370{
4371 struct device_node *np = card->dev->of_node;
4372 int ret;
4373
4374 ret = of_property_read_string_index(np, propname, 0, &card->name);
4375 /*
4376 * EINVAL means the property does not exist. This is fine providing
4377 * card->name was previously set, which is checked later in
4378 * snd_soc_register_card.
4379 */
4380 if (ret < 0 && ret != -EINVAL) {
4381 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004382 "ASoC: Property '%s' could not be read: %d\n",
Stephen Warrenbec4fa02011-12-12 15:55:34 -07004383 propname, ret);
4384 return ret;
4385 }
4386
4387 return 0;
4388}
4389EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
4390
Xiubo Li9a6d4862014-02-08 15:59:52 +08004391static const struct snd_soc_dapm_widget simple_widgets[] = {
4392 SND_SOC_DAPM_MIC("Microphone", NULL),
4393 SND_SOC_DAPM_LINE("Line", NULL),
4394 SND_SOC_DAPM_HP("Headphone", NULL),
4395 SND_SOC_DAPM_SPK("Speaker", NULL),
4396};
4397
4398int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card,
4399 const char *propname)
4400{
4401 struct device_node *np = card->dev->of_node;
4402 struct snd_soc_dapm_widget *widgets;
4403 const char *template, *wname;
4404 int i, j, num_widgets, ret;
4405
4406 num_widgets = of_property_count_strings(np, propname);
4407 if (num_widgets < 0) {
4408 dev_err(card->dev,
4409 "ASoC: Property '%s' does not exist\n", propname);
4410 return -EINVAL;
4411 }
4412 if (num_widgets & 1) {
4413 dev_err(card->dev,
4414 "ASoC: Property '%s' length is not even\n", propname);
4415 return -EINVAL;
4416 }
4417
4418 num_widgets /= 2;
4419 if (!num_widgets) {
4420 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
4421 propname);
4422 return -EINVAL;
4423 }
4424
4425 widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets),
4426 GFP_KERNEL);
4427 if (!widgets) {
4428 dev_err(card->dev,
4429 "ASoC: Could not allocate memory for widgets\n");
4430 return -ENOMEM;
4431 }
4432
4433 for (i = 0; i < num_widgets; i++) {
4434 ret = of_property_read_string_index(np, propname,
4435 2 * i, &template);
4436 if (ret) {
4437 dev_err(card->dev,
4438 "ASoC: Property '%s' index %d read error:%d\n",
4439 propname, 2 * i, ret);
4440 return -EINVAL;
4441 }
4442
4443 for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) {
4444 if (!strncmp(template, simple_widgets[j].name,
4445 strlen(simple_widgets[j].name))) {
4446 widgets[i] = simple_widgets[j];
4447 break;
4448 }
4449 }
4450
4451 if (j >= ARRAY_SIZE(simple_widgets)) {
4452 dev_err(card->dev,
4453 "ASoC: DAPM widget '%s' is not supported\n",
4454 template);
4455 return -EINVAL;
4456 }
4457
4458 ret = of_property_read_string_index(np, propname,
4459 (2 * i) + 1,
4460 &wname);
4461 if (ret) {
4462 dev_err(card->dev,
4463 "ASoC: Property '%s' index %d read error:%d\n",
4464 propname, (2 * i) + 1, ret);
4465 return -EINVAL;
4466 }
4467
4468 widgets[i].name = wname;
4469 }
4470
4471 card->dapm_widgets = widgets;
4472 card->num_dapm_widgets = num_widgets;
4473
4474 return 0;
4475}
4476EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets);
4477
Xiubo Li89c67852014-02-14 09:34:35 +08004478int snd_soc_of_parse_tdm_slot(struct device_node *np,
4479 unsigned int *slots,
4480 unsigned int *slot_width)
4481{
4482 u32 val;
4483 int ret;
4484
4485 if (of_property_read_bool(np, "dai-tdm-slot-num")) {
4486 ret = of_property_read_u32(np, "dai-tdm-slot-num", &val);
4487 if (ret)
4488 return ret;
4489
4490 if (slots)
4491 *slots = val;
4492 }
4493
4494 if (of_property_read_bool(np, "dai-tdm-slot-width")) {
4495 ret = of_property_read_u32(np, "dai-tdm-slot-width", &val);
4496 if (ret)
4497 return ret;
4498
4499 if (slot_width)
4500 *slot_width = val;
4501 }
4502
4503 return 0;
4504}
4505EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot);
4506
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004507int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
4508 const char *propname)
4509{
4510 struct device_node *np = card->dev->of_node;
4511 int num_routes;
4512 struct snd_soc_dapm_route *routes;
4513 int i, ret;
4514
4515 num_routes = of_property_count_strings(np, propname);
Richard Zhaoc34ce322012-04-24 15:24:43 +08004516 if (num_routes < 0 || num_routes & 1) {
Michał Mirosław10e8aa92013-05-04 22:21:38 +02004517 dev_err(card->dev,
4518 "ASoC: Property '%s' does not exist or its length is not even\n",
4519 propname);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004520 return -EINVAL;
4521 }
4522 num_routes /= 2;
4523 if (!num_routes) {
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004524 dev_err(card->dev, "ASoC: Property '%s's length is zero\n",
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004525 propname);
4526 return -EINVAL;
4527 }
4528
4529 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
4530 GFP_KERNEL);
4531 if (!routes) {
4532 dev_err(card->dev,
Liam Girdwoodf110bfc2012-11-19 14:47:09 +00004533 "ASoC: Could not allocate DAPM route table\n");
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004534 return -EINVAL;
4535 }
4536
4537 for (i = 0; i < num_routes; i++) {
4538 ret = of_property_read_string_index(np, propname,
4539 2 * i, &routes[i].sink);
4540 if (ret) {
Mark Brownc871bd02012-12-10 16:19:52 +09004541 dev_err(card->dev,
4542 "ASoC: Property '%s' index %d could not be read: %d\n",
4543 propname, 2 * i, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004544 return -EINVAL;
4545 }
4546 ret = of_property_read_string_index(np, propname,
4547 (2 * i) + 1, &routes[i].source);
4548 if (ret) {
4549 dev_err(card->dev,
Mark Brownc871bd02012-12-10 16:19:52 +09004550 "ASoC: Property '%s' index %d could not be read: %d\n",
4551 propname, (2 * i) + 1, ret);
Stephen Warrena4a54dd2011-12-12 15:55:35 -07004552 return -EINVAL;
4553 }
4554 }
4555
4556 card->num_dapm_routes = num_routes;
4557 card->dapm_routes = routes;
4558
4559 return 0;
4560}
4561EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
4562
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004563unsigned int snd_soc_of_parse_daifmt(struct device_node *np,
4564 const char *prefix)
4565{
4566 int ret, i;
4567 char prop[128];
4568 unsigned int format = 0;
4569 int bit, frame;
4570 const char *str;
4571 struct {
4572 char *name;
4573 unsigned int val;
4574 } of_fmt_table[] = {
4575 { "i2s", SND_SOC_DAIFMT_I2S },
4576 { "right_j", SND_SOC_DAIFMT_RIGHT_J },
4577 { "left_j", SND_SOC_DAIFMT_LEFT_J },
4578 { "dsp_a", SND_SOC_DAIFMT_DSP_A },
4579 { "dsp_b", SND_SOC_DAIFMT_DSP_B },
4580 { "ac97", SND_SOC_DAIFMT_AC97 },
4581 { "pdm", SND_SOC_DAIFMT_PDM},
4582 { "msb", SND_SOC_DAIFMT_MSB },
4583 { "lsb", SND_SOC_DAIFMT_LSB },
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004584 };
4585
4586 if (!prefix)
4587 prefix = "";
4588
4589 /*
4590 * check "[prefix]format = xxx"
4591 * SND_SOC_DAIFMT_FORMAT_MASK area
4592 */
4593 snprintf(prop, sizeof(prop), "%sformat", prefix);
4594 ret = of_property_read_string(np, prop, &str);
4595 if (ret == 0) {
4596 for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) {
4597 if (strcmp(str, of_fmt_table[i].name) == 0) {
4598 format |= of_fmt_table[i].val;
4599 break;
4600 }
4601 }
4602 }
4603
4604 /*
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004605 * check "[prefix]continuous-clock"
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004606 * SND_SOC_DAIFMT_CLOCK_MASK area
4607 */
Kuninori Morimoto8c2d6a92013-01-29 21:03:36 -08004608 snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix);
4609 if (of_get_property(np, prop, NULL))
4610 format |= SND_SOC_DAIFMT_CONT;
4611 else
4612 format |= SND_SOC_DAIFMT_GATED;
Kuninori Morimotoa7930ed2013-01-14 18:36:04 -08004613
4614 /*
4615 * check "[prefix]bitclock-inversion"
4616 * check "[prefix]frame-inversion"
4617 * SND_SOC_DAIFMT_INV_MASK area
4618 */
4619 snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix);
4620 bit = !!of_get_property(np, prop, NULL);
4621
4622 snprintf(prop, sizeof(prop), "%sframe-inversion", prefix);
4623 frame = !!of_get_property(np, prop, NULL);
4624
4625 switch ((bit << 4) + frame) {
4626 case 0x11:
4627 format |= SND_SOC_DAIFMT_IB_IF;
4628 break;
4629 case 0x10:
4630 format |= SND_SOC_DAIFMT_IB_NF;
4631 break;
4632 case 0x01:
4633 format |= SND_SOC_DAIFMT_NB_IF;
4634 break;
4635 default:
4636 /* SND_SOC_DAIFMT_NB_NF is default */
4637 break;
4638 }
4639
4640 /*
4641 * check "[prefix]bitclock-master"
4642 * check "[prefix]frame-master"
4643 * SND_SOC_DAIFMT_MASTER_MASK area
4644 */
4645 snprintf(prop, sizeof(prop), "%sbitclock-master", prefix);
4646 bit = !!of_get_property(np, prop, NULL);
4647
4648 snprintf(prop, sizeof(prop), "%sframe-master", prefix);
4649 frame = !!of_get_property(np, prop, NULL);
4650
4651 switch ((bit << 4) + frame) {
4652 case 0x11:
4653 format |= SND_SOC_DAIFMT_CBM_CFM;
4654 break;
4655 case 0x10:
4656 format |= SND_SOC_DAIFMT_CBM_CFS;
4657 break;
4658 case 0x01:
4659 format |= SND_SOC_DAIFMT_CBS_CFM;
4660 break;
4661 default:
4662 format |= SND_SOC_DAIFMT_CBS_CFS;
4663 break;
4664 }
4665
4666 return format;
4667}
4668EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt);
4669
Kuninori Morimotocb470082013-09-10 17:39:56 -07004670int snd_soc_of_get_dai_name(struct device_node *of_node,
4671 const char **dai_name)
4672{
4673 struct snd_soc_component *pos;
4674 struct of_phandle_args args;
4675 int ret;
4676
4677 ret = of_parse_phandle_with_args(of_node, "sound-dai",
4678 "#sound-dai-cells", 0, &args);
4679 if (ret)
4680 return ret;
4681
4682 ret = -EPROBE_DEFER;
4683
4684 mutex_lock(&client_mutex);
4685 list_for_each_entry(pos, &component_list, list) {
4686 if (pos->dev->of_node != args.np)
4687 continue;
4688
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004689 if (pos->driver->of_xlate_dai_name) {
4690 ret = pos->driver->of_xlate_dai_name(pos, &args, dai_name);
4691 } else {
4692 int id = -1;
4693
4694 switch (args.args_count) {
4695 case 0:
4696 id = 0; /* same as dai_drv[0] */
4697 break;
4698 case 1:
4699 id = args.args[0];
4700 break;
4701 default:
4702 /* not supported */
4703 break;
4704 }
4705
4706 if (id < 0 || id >= pos->num_dai) {
4707 ret = -EINVAL;
Xiubo Lie41975e2013-12-20 14:39:51 +08004708 break;
Kuninori Morimoto6833c452013-10-16 22:05:26 -07004709 }
Xiubo Lie41975e2013-12-20 14:39:51 +08004710
4711 ret = 0;
4712
4713 *dai_name = pos->dai_drv[id].name;
4714 if (!*dai_name)
4715 *dai_name = pos->name;
Kuninori Morimotocb470082013-09-10 17:39:56 -07004716 }
4717
Kuninori Morimotocb470082013-09-10 17:39:56 -07004718 break;
4719 }
4720 mutex_unlock(&client_mutex);
4721
4722 of_node_put(args.np);
4723
4724 return ret;
4725}
4726EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name);
4727
Takashi Iwaic9b3a402008-12-10 07:47:22 +01004728static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004729{
Mark Brown384c89e2008-12-03 17:34:03 +00004730#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004731 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
4732 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02004733 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00004734 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00004735 }
Mark Brownc3c5a192010-09-15 18:15:14 +01004736
Mark Brown8a9dab12011-01-10 22:25:21 +00004737 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01004738 &codec_list_fops))
4739 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
4740
Mark Brown8a9dab12011-01-10 22:25:21 +00004741 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01004742 &dai_list_fops))
4743 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01004744
Mark Brown8a9dab12011-01-10 22:25:21 +00004745 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01004746 &platform_list_fops))
4747 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00004748#endif
4749
Mark Brownfb257892011-04-28 10:57:54 +01004750 snd_soc_util_init();
4751
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004752 return platform_driver_register(&soc_driver);
4753}
Mark Brown4abe8e12010-10-12 17:41:03 +01004754module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004755
Mark Brown7d8c16a2008-11-30 22:11:24 +00004756static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004757{
Mark Brownfb257892011-04-28 10:57:54 +01004758 snd_soc_util_exit();
4759
Mark Brown384c89e2008-12-03 17:34:03 +00004760#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00004761 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00004762#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02004763 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004764}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004765module_exit(snd_soc_exit);
4766
4767/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01004768MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02004769MODULE_DESCRIPTION("ALSA SoC Core");
4770MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02004771MODULE_ALIAS("platform:soc-audio");