blob: 29dbbd793fc6267796e1f3a68109fa5295bb661d [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>
Mark Brownf0e8ed82011-09-20 11:41:54 +010033#include <linux/ctype.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Stephen Warrenbec4fa02011-12-12 15:55:34 -070035#include <linux/of.h>
Marek Vasut474828a2009-07-22 13:01:03 +020036#include <sound/ac97_codec.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020037#include <sound/core.h>
Mark Brown3028eb82010-12-05 12:22:46 +000038#include <sound/jack.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020039#include <sound/pcm.h>
40#include <sound/pcm_params.h>
41#include <sound/soc.h>
Frank Mandarinodb2a4162006-10-06 18:31:09 +020042#include <sound/initval.h>
43
Mark Browna8b1d342010-11-03 18:05:58 -040044#define CREATE_TRACE_POINTS
45#include <trace/events/asoc.h>
46
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000047#define NAME_SIZE 32
48
Frank Mandarinodb2a4162006-10-06 18:31:09 +020049static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
50
Mark Brown384c89e2008-12-03 17:34:03 +000051#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +000052struct dentry *snd_soc_debugfs_root;
53EXPORT_SYMBOL_GPL(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +000054#endif
55
Mark Brownc5af3a22008-11-28 13:29:45 +000056static DEFINE_MUTEX(client_mutex);
57static LIST_HEAD(card_list);
Mark Brown91151712008-11-30 23:31:24 +000058static LIST_HEAD(dai_list);
Mark Brown12a48a8c2008-12-03 19:40:30 +000059static LIST_HEAD(platform_list);
Mark Brown0d0cf002008-12-10 14:32:45 +000060static LIST_HEAD(codec_list);
Mark Brownc5af3a22008-11-28 13:29:45 +000061
Frank Mandarinodb2a4162006-10-06 18:31:09 +020062/*
63 * This is a timeout to do a DAPM powerdown after a stream is closed().
64 * It can be used to eliminate pops between different playback streams, e.g.
65 * between two audio tracks.
66 */
67static int pmdown_time = 5000;
68module_param(pmdown_time, int, 0);
69MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
70
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +000071/* returns the minimum number of bytes needed to represent
72 * a particular given value */
73static int min_bytes_needed(unsigned long val)
74{
75 int c = 0;
76 int i;
77
78 for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c)
79 if (val & (1UL << i))
80 break;
81 c = (sizeof val * 8) - c;
82 if (!c || (c % 8))
83 c = (c + 8) / 8;
84 else
85 c /= 8;
86 return c;
87}
88
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000089/* fill buf which is 'len' bytes with a formatted
90 * string of the form 'reg: value\n' */
91static int format_register_str(struct snd_soc_codec *codec,
92 unsigned int reg, char *buf, size_t len)
Mark Brown2624d5f2009-11-03 21:56:13 +000093{
Stephen Warren00b317a2011-04-01 14:50:44 -060094 int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
95 int regsize = codec->driver->reg_word_size * 2;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +000096 int ret;
97 char tmpbuf[len + 1];
98 char regbuf[regsize + 1];
99
100 /* since tmpbuf is allocated on the stack, warn the callers if they
101 * try to abuse this function */
102 WARN_ON(len > 63);
103
104 /* +2 for ': ' and + 1 for '\n' */
105 if (wordsize + regsize + 2 + 1 != len)
106 return -EINVAL;
107
Mark Brown25032c12011-08-01 13:52:48 +0900108 ret = snd_soc_read(codec, reg);
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000109 if (ret < 0) {
110 memset(regbuf, 'X', regsize);
111 regbuf[regsize] = '\0';
112 } else {
113 snprintf(regbuf, regsize + 1, "%.*x", regsize, ret);
114 }
115
116 /* prepare the buffer */
117 snprintf(tmpbuf, len + 1, "%.*x: %s\n", wordsize, reg, regbuf);
118 /* copy it back to the caller without the '\0' */
119 memcpy(buf, tmpbuf, len);
120
121 return 0;
122}
123
124/* codec register dump */
125static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf,
126 size_t count, loff_t pos)
127{
128 int i, step = 1;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000129 int wordsize, regsize;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000130 int len;
131 size_t total = 0;
132 loff_t p = 0;
Dimitris Papastamos2bc9a812011-02-01 12:24:08 +0000133
Stephen Warren00b317a2011-04-01 14:50:44 -0600134 wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2;
135 regsize = codec->driver->reg_word_size * 2;
Mark Brown2624d5f2009-11-03 21:56:13 +0000136
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000137 len = wordsize + regsize + 2 + 1;
138
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000139 if (!codec->driver->reg_cache_size)
Mark Brown2624d5f2009-11-03 21:56:13 +0000140 return 0;
141
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000142 if (codec->driver->reg_cache_step)
143 step = codec->driver->reg_cache_step;
Mark Brown2624d5f2009-11-03 21:56:13 +0000144
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000145 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
Lars-Peter Clausenb92d1502011-08-27 18:24:14 +0200146 if (!snd_soc_codec_readable_register(codec, i))
Mark Brown2624d5f2009-11-03 21:56:13 +0000147 continue;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000148 if (codec->driver->display_register) {
149 count += codec->driver->display_register(codec, buf + count,
Mark Brown2624d5f2009-11-03 21:56:13 +0000150 PAGE_SIZE - count, i);
Mark Brown5164d742010-07-14 16:14:33 +0100151 } else {
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000152 /* only support larger than PAGE_SIZE bytes debugfs
153 * entries for the default case */
154 if (p >= pos) {
155 if (total + len >= count - 1)
156 break;
157 format_register_str(codec, i, buf + total, len);
158 total += len;
159 }
160 p += len;
Mark Brown5164d742010-07-14 16:14:33 +0100161 }
Mark Brown2624d5f2009-11-03 21:56:13 +0000162 }
163
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000164 total = min(total, count - 1);
Mark Brown2624d5f2009-11-03 21:56:13 +0000165
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000166 return total;
Mark Brown2624d5f2009-11-03 21:56:13 +0000167}
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000168
Mark Brown2624d5f2009-11-03 21:56:13 +0000169static ssize_t codec_reg_show(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
Mark Brown36ae1a92012-01-06 17:12:45 -0800172 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000173
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000174 return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0);
Mark Brown2624d5f2009-11-03 21:56:13 +0000175}
176
177static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
178
Mark Browndbe21402010-02-12 11:37:24 +0000179static ssize_t pmdown_time_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
Mark Brown36ae1a92012-01-06 17:12:45 -0800182 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Browndbe21402010-02-12 11:37:24 +0000183
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000184 return sprintf(buf, "%ld\n", rtd->pmdown_time);
Mark Browndbe21402010-02-12 11:37:24 +0000185}
186
187static ssize_t pmdown_time_set(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
Mark Brown36ae1a92012-01-06 17:12:45 -0800191 struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
Mark Brownc593b522010-10-27 20:11:17 -0700192 int ret;
Mark Browndbe21402010-02-12 11:37:24 +0000193
Mark Brownc593b522010-10-27 20:11:17 -0700194 ret = strict_strtol(buf, 10, &rtd->pmdown_time);
195 if (ret)
196 return ret;
Mark Browndbe21402010-02-12 11:37:24 +0000197
198 return count;
199}
200
201static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
202
Mark Brown2624d5f2009-11-03 21:56:13 +0000203#ifdef CONFIG_DEBUG_FS
204static int codec_reg_open_file(struct inode *inode, struct file *file)
205{
206 file->private_data = inode->i_private;
207 return 0;
208}
209
210static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000211 size_t count, loff_t *ppos)
Mark Brown2624d5f2009-11-03 21:56:13 +0000212{
213 ssize_t ret;
214 struct snd_soc_codec *codec = file->private_data;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000215 char *buf;
216
217 if (*ppos < 0 || !count)
218 return -EINVAL;
219
220 buf = kmalloc(count, GFP_KERNEL);
Mark Brown2624d5f2009-11-03 21:56:13 +0000221 if (!buf)
222 return -ENOMEM;
Dimitris Papastamos13fd1792011-02-02 13:58:58 +0000223
224 ret = soc_codec_reg_show(codec, buf, count, *ppos);
225 if (ret >= 0) {
226 if (copy_to_user(user_buf, buf, ret)) {
227 kfree(buf);
228 return -EFAULT;
229 }
230 *ppos += ret;
231 }
232
Mark Brown2624d5f2009-11-03 21:56:13 +0000233 kfree(buf);
234 return ret;
235}
236
237static ssize_t codec_reg_write_file(struct file *file,
238 const char __user *user_buf, size_t count, loff_t *ppos)
239{
240 char buf[32];
Stephen Boyd34e268d2011-05-12 16:50:10 -0700241 size_t buf_size;
Mark Brown2624d5f2009-11-03 21:56:13 +0000242 char *start = buf;
243 unsigned long reg, value;
Mark Brown2624d5f2009-11-03 21:56:13 +0000244 struct snd_soc_codec *codec = file->private_data;
245
246 buf_size = min(count, (sizeof(buf)-1));
247 if (copy_from_user(buf, user_buf, buf_size))
248 return -EFAULT;
249 buf[buf_size] = 0;
250
Mark Brown2624d5f2009-11-03 21:56:13 +0000251 while (*start == ' ')
252 start++;
253 reg = simple_strtoul(start, &start, 16);
Mark Brown2624d5f2009-11-03 21:56:13 +0000254 while (*start == ' ')
255 start++;
256 if (strict_strtoul(start, 16, &value))
257 return -EINVAL;
Mark Brown0d51a9c2011-01-06 16:04:57 +0000258
259 /* Userspace has been fiddling around behind the kernel's back */
260 add_taint(TAINT_USER);
261
Dimitris Papastamose4f078d2010-12-07 16:30:38 +0000262 snd_soc_write(codec, reg, value);
Mark Brown2624d5f2009-11-03 21:56:13 +0000263 return buf_size;
264}
265
266static const struct file_operations codec_reg_fops = {
267 .open = codec_reg_open_file,
268 .read = codec_reg_read_file,
269 .write = codec_reg_write_file,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200270 .llseek = default_llseek,
Mark Brown2624d5f2009-11-03 21:56:13 +0000271};
272
273static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
274{
Jarkko Nikulad6ce4cf2010-11-05 20:35:20 +0200275 struct dentry *debugfs_card_root = codec->card->debugfs_card_root;
276
277 codec->debugfs_codec_root = debugfs_create_dir(codec->name,
278 debugfs_card_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000279 if (!codec->debugfs_codec_root) {
Liam Girdwood64e60f92012-02-15 15:15:37 +0000280 dev_warn(codec->dev, "Failed to create codec debugfs directory\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000281 return;
282 }
283
Mark Brownaaee8ef2011-01-26 20:53:50 +0000284 debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root,
285 &codec->cache_sync);
286 debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root,
287 &codec->cache_only);
288
Mark Brown2624d5f2009-11-03 21:56:13 +0000289 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
290 codec->debugfs_codec_root,
291 codec, &codec_reg_fops);
292 if (!codec->debugfs_reg)
Liam Girdwood64e60f92012-02-15 15:15:37 +0000293 dev_warn(codec->dev, "Failed to create codec register debugfs file\n");
Mark Brown2624d5f2009-11-03 21:56:13 +0000294
Lars-Peter Clausen8eecaf62011-04-30 19:45:48 +0200295 snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root);
Mark Brown2624d5f2009-11-03 21:56:13 +0000296}
297
298static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
299{
300 debugfs_remove_recursive(codec->debugfs_codec_root);
301}
302
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000303static void soc_init_platform_debugfs(struct snd_soc_platform *platform)
304{
305 struct dentry *debugfs_card_root = platform->card->debugfs_card_root;
306
307 platform->debugfs_platform_root = debugfs_create_dir(platform->name,
308 debugfs_card_root);
309 if (!platform->debugfs_platform_root) {
310 dev_warn(platform->dev,
311 "Failed to create platform debugfs directory\n");
312 return;
313 }
314
315 snd_soc_dapm_debugfs_init(&platform->dapm,
316 platform->debugfs_platform_root);
317}
318
319static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
320{
321 debugfs_remove_recursive(platform->debugfs_platform_root);
322}
323
Mark Brownc3c5a192010-09-15 18:15:14 +0100324static ssize_t codec_list_read_file(struct file *file, char __user *user_buf,
325 size_t count, loff_t *ppos)
326{
327 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100328 ssize_t len, ret = 0;
Mark Brownc3c5a192010-09-15 18:15:14 +0100329 struct snd_soc_codec *codec;
330
331 if (!buf)
332 return -ENOMEM;
333
Mark Brown2b194f9d2010-10-13 10:52:16 +0100334 list_for_each_entry(codec, &codec_list, list) {
335 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
336 codec->name);
337 if (len >= 0)
338 ret += len;
339 if (ret > PAGE_SIZE) {
340 ret = PAGE_SIZE;
341 break;
342 }
343 }
Mark Brownc3c5a192010-09-15 18:15:14 +0100344
345 if (ret >= 0)
346 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
347
348 kfree(buf);
349
350 return ret;
351}
352
353static const struct file_operations codec_list_fops = {
354 .read = codec_list_read_file,
355 .llseek = default_llseek,/* read accesses f_pos */
356};
357
Mark Brownf3208782010-09-15 18:19:07 +0100358static ssize_t dai_list_read_file(struct file *file, char __user *user_buf,
359 size_t count, loff_t *ppos)
360{
361 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100362 ssize_t len, ret = 0;
Mark Brownf3208782010-09-15 18:19:07 +0100363 struct snd_soc_dai *dai;
364
365 if (!buf)
366 return -ENOMEM;
367
Mark Brown2b194f9d2010-10-13 10:52:16 +0100368 list_for_each_entry(dai, &dai_list, list) {
369 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
370 if (len >= 0)
371 ret += len;
372 if (ret > PAGE_SIZE) {
373 ret = PAGE_SIZE;
374 break;
375 }
376 }
Mark Brownf3208782010-09-15 18:19:07 +0100377
Mark Brown2b194f9d2010-10-13 10:52:16 +0100378 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brownf3208782010-09-15 18:19:07 +0100379
380 kfree(buf);
381
382 return ret;
383}
384
385static const struct file_operations dai_list_fops = {
386 .read = dai_list_read_file,
387 .llseek = default_llseek,/* read accesses f_pos */
388};
389
Mark Brown19c7ac22010-09-15 18:22:40 +0100390static ssize_t platform_list_read_file(struct file *file,
391 char __user *user_buf,
392 size_t count, loff_t *ppos)
393{
394 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Mark Brown2b194f9d2010-10-13 10:52:16 +0100395 ssize_t len, ret = 0;
Mark Brown19c7ac22010-09-15 18:22:40 +0100396 struct snd_soc_platform *platform;
397
398 if (!buf)
399 return -ENOMEM;
400
Mark Brown2b194f9d2010-10-13 10:52:16 +0100401 list_for_each_entry(platform, &platform_list, list) {
402 len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n",
403 platform->name);
404 if (len >= 0)
405 ret += len;
406 if (ret > PAGE_SIZE) {
407 ret = PAGE_SIZE;
408 break;
409 }
410 }
Mark Brown19c7ac22010-09-15 18:22:40 +0100411
Mark Brown2b194f9d2010-10-13 10:52:16 +0100412 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
Mark Brown19c7ac22010-09-15 18:22:40 +0100413
414 kfree(buf);
415
416 return ret;
417}
418
419static const struct file_operations platform_list_fops = {
420 .read = platform_list_read_file,
421 .llseek = default_llseek,/* read accesses f_pos */
422};
423
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200424static void soc_init_card_debugfs(struct snd_soc_card *card)
425{
426 card->debugfs_card_root = debugfs_create_dir(card->name,
Mark Brown8a9dab12011-01-10 22:25:21 +0000427 snd_soc_debugfs_root);
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200428 if (!card->debugfs_card_root) {
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200429 dev_warn(card->dev,
Lothar Waßmann7c08be82011-12-09 14:16:29 +0100430 "ASoC: Failed to create card debugfs directory\n");
Jarkko Nikula3a45b862010-11-05 20:35:21 +0200431 return;
432 }
433
434 card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644,
435 card->debugfs_card_root,
436 &card->pop_time);
437 if (!card->debugfs_pop_time)
438 dev_warn(card->dev,
439 "Failed to create pop time debugfs file\n");
Jarkko Nikulaa6052152010-11-05 20:35:19 +0200440}
441
442static void soc_cleanup_card_debugfs(struct snd_soc_card *card)
443{
444 debugfs_remove_recursive(card->debugfs_card_root);
445}
446
Mark Brown2624d5f2009-11-03 21:56:13 +0000447#else
448
449static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
450{
451}
452
453static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
454{
455}
Axel Linb95fccb2010-11-09 17:06:44 +0800456
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000457static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform)
458{
459}
460
461static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform)
462{
463}
464
Axel Linb95fccb2010-11-09 17:06:44 +0800465static inline void soc_init_card_debugfs(struct snd_soc_card *card)
466{
467}
468
469static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card)
470{
471}
Mark Brown2624d5f2009-11-03 21:56:13 +0000472#endif
473
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200474#ifdef CONFIG_SND_SOC_AC97_BUS
475/* unregister ac97 codec */
476static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
477{
478 if (codec->ac97->dev.bus)
479 device_unregister(&codec->ac97->dev);
480 return 0;
481}
482
483/* stop no dev release warning */
484static void soc_ac97_device_release(struct device *dev){}
485
486/* register ac97 codec to bus */
487static int soc_ac97_dev_register(struct snd_soc_codec *codec)
488{
489 int err;
490
491 codec->ac97->dev.bus = &ac97_bus_type;
Mark Brown4ac5c612009-04-01 19:35:01 +0100492 codec->ac97->dev.parent = codec->card->dev;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200493 codec->ac97->dev.release = soc_ac97_device_release;
494
Kay Sieversbb072bf2008-11-02 03:50:35 +0100495 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 codec->card->snd_card->number, 0, codec->name);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200497 err = device_register(&codec->ac97->dev);
498 if (err < 0) {
499 snd_printk(KERN_ERR "Can't register ac97 bus\n");
500 codec->ac97->dev.bus = NULL;
501 return err;
502 }
503 return 0;
504}
505#endif
506
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000507#ifdef CONFIG_PM_SLEEP
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200508/* powers down audio subsystem for suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000509int snd_soc_suspend(struct device *dev)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200510{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000511 struct snd_soc_card *card = dev_get_drvdata(dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200512 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200513 int i;
514
Daniel Macke3509ff2009-06-03 17:44:49 +0200515 /* If the initialization of this soc device failed, there is no codec
516 * associated with it. Just bail out in this case.
517 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000518 if (list_empty(&card->codec_dev_list))
Daniel Macke3509ff2009-06-03 17:44:49 +0200519 return 0;
520
Andy Green6ed25972008-06-13 16:24:05 +0100521 /* Due to the resume being scheduled into a workqueue we could
522 * suspend before that's finished - wait for it to complete.
523 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524 snd_power_lock(card->snd_card);
525 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
526 snd_power_unlock(card->snd_card);
Andy Green6ed25972008-06-13 16:24:05 +0100527
528 /* we're going to block userspace touching us until resume completes */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000529 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
Andy Green6ed25972008-06-13 16:24:05 +0100530
Mark Browna00f90f2010-12-02 16:24:24 +0000531 /* mute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000532 for (i = 0; i < card->num_rtd; i++) {
533 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
534 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100535
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000536 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100537 continue;
538
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000539 if (drv->ops->digital_mute && dai->playback_active)
540 drv->ops->digital_mute(dai, 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200541 }
542
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100543 /* suspend all pcms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000544 for (i = 0; i < card->num_rtd; i++) {
545 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100546 continue;
547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000548 snd_pcm_suspend_all(card->rtd[i].pcm);
Mark Brown3efab7d2010-05-09 13:25:43 +0100549 }
Liam Girdwood4ccab3e2008-01-10 14:39:01 +0100550
Mark Brown87506542008-11-18 20:50:34 +0000551 if (card->suspend_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000552 card->suspend_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200553
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000554 for (i = 0; i < card->num_rtd; i++) {
555 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
556 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100557
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000558 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100559 continue;
560
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000561 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
562 cpu_dai->driver->suspend(cpu_dai);
563 if (platform->driver->suspend && !platform->suspended) {
564 platform->driver->suspend(cpu_dai);
565 platform->suspended = 1;
Mark Brown1547aba2010-05-07 21:11:40 +0100566 }
567 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200568
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000569 /* close any waiting streams and save state */
570 for (i = 0; i < card->num_rtd; i++) {
Tejun Heo5b84ba22010-12-11 17:51:26 +0100571 flush_delayed_work_sync(&card->rtd[i].delayed_work);
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200572 card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000573 }
Mark Brown3efab7d2010-05-09 13:25:43 +0100574
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000575 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800576 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000577
578 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100579 continue;
580
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800581 snd_soc_dapm_stream_event(&card->rtd[i],
582 SNDRV_PCM_STREAM_PLAYBACK,
583 codec_dai,
584 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000585
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800586 snd_soc_dapm_stream_event(&card->rtd[i],
587 SNDRV_PCM_STREAM_CAPTURE,
588 codec_dai,
589 SND_SOC_DAPM_STREAM_SUSPEND);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000590 }
591
592 /* suspend all CODECs */
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200593 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000594 /* If there are paths active then the CODEC will be held with
595 * bias _ON and should not be suspended. */
596 if (!codec->suspended && codec->driver->suspend) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200597 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000598 case SND_SOC_BIAS_STANDBY:
Mark Brown125a25d2012-01-31 15:49:10 +0000599 /*
600 * If the CODEC is capable of idle
601 * bias off then being in STANDBY
602 * means it's doing something,
603 * otherwise fall through.
604 */
605 if (codec->dapm.idle_bias_off) {
606 dev_dbg(codec->dev,
607 "idle_bias_off CODEC on over suspend\n");
608 break;
609 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000610 case SND_SOC_BIAS_OFF:
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +0100611 codec->driver->suspend(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000612 codec->suspended = 1;
Mark Brown7be4ba22011-07-18 13:17:13 +0900613 codec->cache_sync = 1;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000614 break;
615 default:
616 dev_dbg(codec->dev, "CODEC is on over suspend\n");
617 break;
618 }
619 }
620 }
621
622 for (i = 0; i < card->num_rtd; i++) {
623 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
624
625 if (card->rtd[i].dai_link->ignore_suspend)
626 continue;
627
628 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
629 cpu_dai->driver->suspend(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200630 }
631
Mark Brown87506542008-11-18 20:50:34 +0000632 if (card->suspend_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000633 card->suspend_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200634
635 return 0;
636}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000637EXPORT_SYMBOL_GPL(snd_soc_suspend);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200638
Andy Green6ed25972008-06-13 16:24:05 +0100639/* deferred resume work, so resume can complete before we finished
640 * setting our codec back up, which can be very slow on I2C
641 */
642static void soc_resume_deferred(struct work_struct *work)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200643{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000644 struct snd_soc_card *card =
645 container_of(work, struct snd_soc_card, deferred_resume_work);
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200646 struct snd_soc_codec *codec;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200647 int i;
648
Andy Green6ed25972008-06-13 16:24:05 +0100649 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
650 * so userspace apps are blocked from touching us
651 */
652
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000653 dev_dbg(card->dev, "starting resume work\n");
Andy Green6ed25972008-06-13 16:24:05 +0100654
Mark Brown99497882010-05-07 20:24:05 +0100655 /* Bring us up into D2 so that DAPM starts enabling things */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000656 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
Mark Brown99497882010-05-07 20:24:05 +0100657
Mark Brown87506542008-11-18 20:50:34 +0000658 if (card->resume_pre)
Mark Brown70b2ac12011-01-26 14:05:25 +0000659 card->resume_pre(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200660
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000661 /* resume AC97 DAIs */
662 for (i = 0; i < card->num_rtd; i++) {
663 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100664
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000665 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100666 continue;
667
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000668 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
669 cpu_dai->driver->resume(cpu_dai);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200670 }
671
Jarkko Nikula2eea3922010-11-25 17:47:38 +0200672 list_for_each_entry(codec, &card->codec_dev_list, card_list) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000673 /* If the CODEC was idle over suspend then it will have been
674 * left with bias OFF or STANDBY and suspended so we must now
675 * resume. Otherwise the suspend was suppressed.
676 */
677 if (codec->driver->resume && codec->suspended) {
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200678 switch (codec->dapm.bias_level) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000679 case SND_SOC_BIAS_STANDBY:
680 case SND_SOC_BIAS_OFF:
681 codec->driver->resume(codec);
682 codec->suspended = 0;
683 break;
684 default:
685 dev_dbg(codec->dev, "CODEC was on over suspend\n");
686 break;
687 }
Mark Brown1547aba2010-05-07 21:11:40 +0100688 }
689 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200690
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000691 for (i = 0; i < card->num_rtd; i++) {
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800692 struct snd_soc_dai *codec_dai = card->rtd[i].codec_dai;
Mark Brown3efab7d2010-05-09 13:25:43 +0100693
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000694 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100695 continue;
696
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800697 snd_soc_dapm_stream_event(&card->rtd[i],
698 SNDRV_PCM_STREAM_PLAYBACK, codec_dai,
699 SND_SOC_DAPM_STREAM_RESUME);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000700
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800701 snd_soc_dapm_stream_event(&card->rtd[i],
702 SNDRV_PCM_STREAM_CAPTURE, codec_dai,
703 SND_SOC_DAPM_STREAM_RESUME);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200704 }
705
Mark Brown3ff3f642008-05-19 12:32:25 +0200706 /* unmute any active DACs */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000707 for (i = 0; i < card->num_rtd; i++) {
708 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
709 struct snd_soc_dai_driver *drv = dai->driver;
Mark Brown3efab7d2010-05-09 13:25:43 +0100710
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000711 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100712 continue;
713
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000714 if (drv->ops->digital_mute && dai->playback_active)
715 drv->ops->digital_mute(dai, 0);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200716 }
717
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000718 for (i = 0; i < card->num_rtd; i++) {
719 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
720 struct snd_soc_platform *platform = card->rtd[i].platform;
Mark Brown3efab7d2010-05-09 13:25:43 +0100721
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000722 if (card->rtd[i].dai_link->ignore_suspend)
Mark Brown3efab7d2010-05-09 13:25:43 +0100723 continue;
724
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000725 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
726 cpu_dai->driver->resume(cpu_dai);
727 if (platform->driver->resume && platform->suspended) {
728 platform->driver->resume(cpu_dai);
729 platform->suspended = 0;
730 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200731 }
732
Mark Brown87506542008-11-18 20:50:34 +0000733 if (card->resume_post)
Mark Brown70b2ac12011-01-26 14:05:25 +0000734 card->resume_post(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200735
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000736 dev_dbg(card->dev, "resume work completed\n");
Andy Green6ed25972008-06-13 16:24:05 +0100737
738 /* userspace can access us now we are back as we were before */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000739 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
Andy Green6ed25972008-06-13 16:24:05 +0100740}
741
742/* powers up audio subsystem after a suspend */
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000743int snd_soc_resume(struct device *dev)
Andy Green6ed25972008-06-13 16:24:05 +0100744{
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000745 struct snd_soc_card *card = dev_get_drvdata(dev);
Stephen Warren82e14e82011-05-25 14:06:41 -0600746 int i, ac97_control = 0;
Peter Ujfalusib9dd94a2010-02-22 13:27:13 +0200747
Eric Miao5ff1ddf2011-11-23 22:37:00 +0800748 /* If the initialization of this soc device failed, there is no codec
749 * associated with it. Just bail out in this case.
750 */
751 if (list_empty(&card->codec_dev_list))
752 return 0;
753
Mark Brown64ab9ba2009-03-31 11:27:03 +0100754 /* AC97 devices might have other drivers hanging off them so
755 * need to resume immediately. Other drivers don't have that
756 * problem and may take a substantial amount of time to resume
757 * due to I/O costs and anti-pop so handle them out of line.
758 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000759 for (i = 0; i < card->num_rtd; i++) {
760 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
Stephen Warren82e14e82011-05-25 14:06:41 -0600761 ac97_control |= cpu_dai->driver->ac97_control;
762 }
763 if (ac97_control) {
764 dev_dbg(dev, "Resuming AC97 immediately\n");
765 soc_resume_deferred(&card->deferred_resume_work);
766 } else {
767 dev_dbg(dev, "Scheduling resume work\n");
768 if (!schedule_work(&card->deferred_resume_work))
769 dev_err(dev, "resume work item may be lost\n");
Mark Brown64ab9ba2009-03-31 11:27:03 +0100770 }
Andy Green6ed25972008-06-13 16:24:05 +0100771
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200772 return 0;
773}
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000774EXPORT_SYMBOL_GPL(snd_soc_resume);
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200775#else
Mark Brown6f8ab4a2011-01-26 14:59:27 +0000776#define snd_soc_suspend NULL
777#define snd_soc_resume NULL
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200778#endif
779
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100780static const struct snd_soc_dai_ops null_dai_ops = {
Barry Song02a06d32009-10-16 18:13:38 +0800781};
782
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000783static int soc_bind_dai_link(struct snd_soc_card *card, int num)
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200784{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000785 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
786 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
Mark Brownfe3e78e2009-11-03 22:13:13 +0000787 struct snd_soc_codec *codec;
Mark Brown435c5e22008-12-04 15:32:53 +0000788 struct snd_soc_platform *platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000789 struct snd_soc_dai *codec_dai, *cpu_dai;
Mark Brown848dd8b2011-04-27 18:16:32 +0100790 const char *platform_name;
Frank Mandarinodb2a4162006-10-06 18:31:09 +0200791
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000792 if (rtd->complete)
793 return 1;
794 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
Mark Brown63084192008-12-02 15:08:03 +0000795
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000796 /* do we already have the CPU DAI for this link ? */
797 if (rtd->cpu_dai) {
798 goto find_codec;
799 }
800 /* no, then find CPU DAI from registered DAIs*/
801 list_for_each_entry(cpu_dai, &dai_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700802 if (dai_link->cpu_dai_of_node) {
803 if (cpu_dai->dev->of_node != dai_link->cpu_dai_of_node)
804 continue;
805 } else {
806 if (strcmp(cpu_dai->name, dai_link->cpu_dai_name))
807 continue;
808 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700809
810 rtd->cpu_dai = cpu_dai;
811 goto find_codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000812 }
813 dev_dbg(card->dev, "CPU DAI %s not registered\n",
814 dai_link->cpu_dai_name);
815
816find_codec:
817 /* do we already have the CODEC for this link ? */
818 if (rtd->codec) {
819 goto find_platform;
Mark Brownc5af3a22008-11-28 13:29:45 +0000820 }
821
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000822 /* no, then find CODEC from registered CODECs*/
823 list_for_each_entry(codec, &codec_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700824 if (dai_link->codec_of_node) {
825 if (codec->dev->of_node != dai_link->codec_of_node)
826 continue;
827 } else {
828 if (strcmp(codec->name, dai_link->codec_name))
829 continue;
830 }
Mark Brown6b05eda2008-12-08 19:26:48 +0000831
Stephen Warren2610ab72011-12-07 13:58:27 -0700832 rtd->codec = codec;
833
834 /*
835 * CODEC found, so find CODEC DAI from registered DAIs from
836 * this CODEC
837 */
838 list_for_each_entry(codec_dai, &dai_list, list) {
839 if (codec->dev == codec_dai->dev &&
840 !strcmp(codec_dai->name,
841 dai_link->codec_dai_name)) {
842
843 rtd->codec_dai = codec_dai;
844 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000845 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000846 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700847 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
848 dai_link->codec_dai_name);
849
850 goto find_platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000851 }
852 dev_dbg(card->dev, "CODEC %s not registered\n",
853 dai_link->codec_name);
854
855find_platform:
Mark Brown848dd8b2011-04-27 18:16:32 +0100856 /* do we need a platform? */
857 if (rtd->platform)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000858 goto out;
Mark Brown848dd8b2011-04-27 18:16:32 +0100859
860 /* if there's no platform we match on the empty platform */
861 platform_name = dai_link->platform_name;
Stephen Warren5a504962011-12-21 10:40:59 -0700862 if (!platform_name && !dai_link->platform_of_node)
Mark Brown848dd8b2011-04-27 18:16:32 +0100863 platform_name = "snd-soc-dummy";
864
865 /* no, then find one from the set of registered platforms */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000866 list_for_each_entry(platform, &platform_list, list) {
Stephen Warren5a504962011-12-21 10:40:59 -0700867 if (dai_link->platform_of_node) {
868 if (platform->dev->of_node !=
869 dai_link->platform_of_node)
870 continue;
871 } else {
872 if (strcmp(platform->name, platform_name))
873 continue;
874 }
Stephen Warren2610ab72011-12-07 13:58:27 -0700875
876 rtd->platform = platform;
877 goto out;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000878 }
879
880 dev_dbg(card->dev, "platform %s not registered\n",
881 dai_link->platform_name);
882 return 0;
883
884out:
885 /* mark rtd as complete if we found all 4 of our client devices */
886 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
887 rtd->complete = 1;
888 card->num_rtd++;
889 }
890 return 1;
891}
892
Jarkko Nikula589c3562010-12-06 16:27:07 +0200893static void soc_remove_codec(struct snd_soc_codec *codec)
894{
895 int err;
896
897 if (codec->driver->remove) {
898 err = codec->driver->remove(codec);
899 if (err < 0)
900 dev_err(codec->dev,
901 "asoc: failed to remove %s: %d\n",
902 codec->name, err);
903 }
904
905 /* Make sure all DAPM widgets are freed */
906 snd_soc_dapm_free(&codec->dapm);
907
908 soc_cleanup_codec_debugfs(codec);
909 codec->probed = 0;
910 list_del(&codec->card_list);
911 module_put(codec->dev->driver->owner);
912}
913
Liam Girdwood0168bf02011-06-07 16:08:05 +0100914static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000915{
916 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
917 struct snd_soc_codec *codec = rtd->codec;
918 struct snd_soc_platform *platform = rtd->platform;
919 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
920 int err;
921
922 /* unregister the rtd device */
923 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -0800924 device_remove_file(rtd->dev, &dev_attr_pmdown_time);
925 device_remove_file(rtd->dev, &dev_attr_codec_reg);
926 device_unregister(rtd->dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000927 rtd->dev_registered = 0;
928 }
929
930 /* remove the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100931 if (codec_dai && codec_dai->probed &&
932 codec_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000933 if (codec_dai->driver->remove) {
934 err = codec_dai->driver->remove(codec_dai);
935 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200936 pr_err("asoc: failed to remove %s: %d\n",
937 codec_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000938 }
939 codec_dai->probed = 0;
940 list_del(&codec_dai->card_list);
941 }
942
943 /* remove the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100944 if (platform && platform->probed &&
945 platform->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000946 if (platform->driver->remove) {
947 err = platform->driver->remove(platform);
948 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200949 pr_err("asoc: failed to remove %s: %d\n",
950 platform->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000951 }
Liam Girdwood675c4962012-01-16 15:25:37 +0000952
953 /* Make sure all DAPM widgets are freed */
954 snd_soc_dapm_free(&platform->dapm);
955
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +0000956 soc_cleanup_platform_debugfs(platform);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000957 platform->probed = 0;
958 list_del(&platform->card_list);
959 module_put(platform->dev->driver->owner);
960 }
961
962 /* remove the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100963 if (codec && codec->probed &&
964 codec->driver->remove_order == order)
Jarkko Nikula589c3562010-12-06 16:27:07 +0200965 soc_remove_codec(codec);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000966
967 /* remove the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +0100968 if (cpu_dai && cpu_dai->probed &&
969 cpu_dai->driver->remove_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000970 if (cpu_dai->driver->remove) {
971 err = cpu_dai->driver->remove(cpu_dai);
972 if (err < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -0200973 pr_err("asoc: failed to remove %s: %d\n",
974 cpu_dai->name, err);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000975 }
976 cpu_dai->probed = 0;
977 list_del(&cpu_dai->card_list);
978 module_put(cpu_dai->dev->driver->owner);
979 }
980}
981
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900982static void soc_remove_dai_links(struct snd_soc_card *card)
983{
Liam Girdwood0168bf02011-06-07 16:08:05 +0100984 int dai, order;
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900985
Liam Girdwood0168bf02011-06-07 16:08:05 +0100986 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
987 order++) {
988 for (dai = 0; dai < card->num_rtd; dai++)
989 soc_remove_dai_link(card, dai, order);
990 }
Kuninori Morimoto0671fd82011-04-08 14:50:44 +0900991 card->num_rtd = 0;
992}
993
Jarkko Nikulaead9b912010-11-13 20:40:44 +0200994static void soc_set_name_prefix(struct snd_soc_card *card,
995 struct snd_soc_codec *codec)
996{
997 int i;
998
Dimitris Papastamosff819b82010-12-02 14:53:03 +0000999 if (card->codec_conf == NULL)
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001000 return;
1001
Dimitris Papastamosff819b82010-12-02 14:53:03 +00001002 for (i = 0; i < card->num_configs; i++) {
1003 struct snd_soc_codec_conf *map = &card->codec_conf[i];
Jarkko Nikulaead9b912010-11-13 20:40:44 +02001004 if (map->dev_name && !strcmp(codec->name, map->dev_name)) {
1005 codec->name_prefix = map->name_prefix;
1006 break;
1007 }
1008 }
1009}
1010
Jarkko Nikula589c3562010-12-06 16:27:07 +02001011static int soc_probe_codec(struct snd_soc_card *card,
1012 struct snd_soc_codec *codec)
1013{
1014 int ret = 0;
Mark Brown89b95ac2011-03-07 16:38:44 +00001015 const struct snd_soc_codec_driver *driver = codec->driver;
Mark Brown888df392012-02-16 19:37:51 -08001016 struct snd_soc_dai *dai;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001017
1018 codec->card = card;
1019 codec->dapm.card = card;
1020 soc_set_name_prefix(card, codec);
1021
Jarkko Nikula70d293312011-01-27 16:24:22 +02001022 if (!try_module_get(codec->dev->driver->owner))
1023 return -ENODEV;
1024
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001025 soc_init_codec_debugfs(codec);
1026
Lars-Peter Clausen77530152011-05-05 16:59:09 +02001027 if (driver->dapm_widgets)
1028 snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets,
1029 driver->num_dapm_widgets);
1030
Mark Brown888df392012-02-16 19:37:51 -08001031 /* Create DAPM widgets for each DAI stream */
1032 list_for_each_entry(dai, &dai_list, list) {
1033 if (dai->dev != codec->dev)
1034 continue;
1035
1036 snd_soc_dapm_new_dai_widgets(&codec->dapm, dai);
1037 }
1038
Mark Brown33c5f962011-08-22 18:40:30 +01001039 codec->dapm.idle_bias_off = driver->idle_bias_off;
1040
Mark Brown89b95ac2011-03-07 16:38:44 +00001041 if (driver->probe) {
1042 ret = driver->probe(codec);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001043 if (ret < 0) {
1044 dev_err(codec->dev,
1045 "asoc: failed to probe CODEC %s: %d\n",
1046 codec->name, ret);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001047 goto err_probe;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001048 }
1049 }
1050
Mark Brownb7af1da2011-04-07 19:18:44 +09001051 if (driver->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001052 snd_soc_add_codec_controls(codec, driver->controls,
Mark Brownb7af1da2011-04-07 19:18:44 +09001053 driver->num_controls);
Mark Brown89b95ac2011-03-07 16:38:44 +00001054 if (driver->dapm_routes)
1055 snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes,
1056 driver->num_dapm_routes);
1057
Jarkko Nikula589c3562010-12-06 16:27:07 +02001058 /* mark codec as probed and add to card codec list */
1059 codec->probed = 1;
1060 list_add(&codec->card_list, &card->codec_dev_list);
Jarkko Nikula7be31be82010-12-14 12:18:32 +02001061 list_add(&codec->dapm.list, &card->dapm_list);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001062
Jarkko Nikula70d293312011-01-27 16:24:22 +02001063 return 0;
1064
1065err_probe:
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001066 soc_cleanup_codec_debugfs(codec);
Jarkko Nikula70d293312011-01-27 16:24:22 +02001067 module_put(codec->dev->driver->owner);
1068
Jarkko Nikula589c3562010-12-06 16:27:07 +02001069 return ret;
1070}
1071
Liam Girdwood956245e2011-07-01 16:54:08 +01001072static int soc_probe_platform(struct snd_soc_card *card,
1073 struct snd_soc_platform *platform)
1074{
1075 int ret = 0;
1076 const struct snd_soc_platform_driver *driver = platform->driver;
1077
1078 platform->card = card;
Liam Girdwoodb7950642011-07-04 22:10:52 +01001079 platform->dapm.card = card;
Liam Girdwood956245e2011-07-01 16:54:08 +01001080
1081 if (!try_module_get(platform->dev->driver->owner))
1082 return -ENODEV;
1083
Sebastien Guiriec731f1ab2012-02-15 15:25:31 +00001084 soc_init_platform_debugfs(platform);
1085
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001086 if (driver->dapm_widgets)
1087 snd_soc_dapm_new_controls(&platform->dapm,
1088 driver->dapm_widgets, driver->num_dapm_widgets);
1089
Liam Girdwood956245e2011-07-01 16:54:08 +01001090 if (driver->probe) {
1091 ret = driver->probe(platform);
1092 if (ret < 0) {
1093 dev_err(platform->dev,
1094 "asoc: failed to probe platform %s: %d\n",
1095 platform->name, ret);
1096 goto err_probe;
1097 }
1098 }
1099
Liam Girdwoodcb2cf612011-07-04 22:10:53 +01001100 if (driver->controls)
1101 snd_soc_add_platform_controls(platform, driver->controls,
1102 driver->num_controls);
1103 if (driver->dapm_routes)
1104 snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes,
1105 driver->num_dapm_routes);
1106
Liam Girdwood956245e2011-07-01 16:54:08 +01001107 /* mark platform as probed and add to card platform list */
1108 platform->probed = 1;
1109 list_add(&platform->card_list, &card->platform_dev_list);
Liam Girdwoodb7950642011-07-04 22:10:52 +01001110 list_add(&platform->dapm.list, &card->dapm_list);
Liam Girdwood956245e2011-07-01 16:54:08 +01001111
1112 return 0;
1113
1114err_probe:
Liam Girdwood02db1102012-03-02 16:13:44 +00001115 soc_cleanup_platform_debugfs(platform);
Liam Girdwood956245e2011-07-01 16:54:08 +01001116 module_put(platform->dev->driver->owner);
1117
1118 return ret;
1119}
1120
Mark Brown36ae1a92012-01-06 17:12:45 -08001121static void rtd_release(struct device *dev)
1122{
1123 kfree(dev);
1124}
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001125
Jarkko Nikula589c3562010-12-06 16:27:07 +02001126static int soc_post_component_init(struct snd_soc_card *card,
1127 struct snd_soc_codec *codec,
1128 int num, int dailess)
1129{
1130 struct snd_soc_dai_link *dai_link = NULL;
1131 struct snd_soc_aux_dev *aux_dev = NULL;
1132 struct snd_soc_pcm_runtime *rtd;
1133 const char *temp, *name;
1134 int ret = 0;
1135
1136 if (!dailess) {
1137 dai_link = &card->dai_link[num];
1138 rtd = &card->rtd[num];
1139 name = dai_link->name;
1140 } else {
1141 aux_dev = &card->aux_dev[num];
1142 rtd = &card->rtd_aux[num];
1143 name = aux_dev->name;
1144 }
Janusz Krzysztofik0962bb22011-02-02 21:11:41 +01001145 rtd->card = card;
Jarkko Nikula589c3562010-12-06 16:27:07 +02001146
Mark Brown4b1cfcb2011-10-18 00:11:49 +01001147 /* Make sure all DAPM widgets are instantiated */
1148 snd_soc_dapm_new_widgets(&codec->dapm);
1149
Jarkko Nikula589c3562010-12-06 16:27:07 +02001150 /* machine controls, routes and widgets are not prefixed */
1151 temp = codec->name_prefix;
1152 codec->name_prefix = NULL;
1153
1154 /* do machine specific initialization */
1155 if (!dailess && dai_link->init)
1156 ret = dai_link->init(rtd);
1157 else if (dailess && aux_dev->init)
1158 ret = aux_dev->init(&codec->dapm);
1159 if (ret < 0) {
1160 dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret);
1161 return ret;
1162 }
1163 codec->name_prefix = temp;
1164
Jarkko Nikula589c3562010-12-06 16:27:07 +02001165 /* register the rtd device */
1166 rtd->codec = codec;
Mark Brown36ae1a92012-01-06 17:12:45 -08001167
1168 rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1169 if (!rtd->dev)
1170 return -ENOMEM;
1171 device_initialize(rtd->dev);
1172 rtd->dev->parent = card->dev;
1173 rtd->dev->release = rtd_release;
1174 rtd->dev->init_name = name;
1175 dev_set_drvdata(rtd->dev, rtd);
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +01001176 mutex_init(&rtd->pcm_mutex);
Mark Brown36ae1a92012-01-06 17:12:45 -08001177 ret = device_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001178 if (ret < 0) {
1179 dev_err(card->dev,
1180 "asoc: failed to register runtime device: %d\n", ret);
1181 return ret;
1182 }
1183 rtd->dev_registered = 1;
1184
1185 /* add DAPM sysfs entries for this codec */
Mark Brown36ae1a92012-01-06 17:12:45 -08001186 ret = snd_soc_dapm_sys_add(rtd->dev);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001187 if (ret < 0)
1188 dev_err(codec->dev,
1189 "asoc: failed to add codec dapm sysfs entries: %d\n",
1190 ret);
1191
1192 /* add codec sysfs entries */
Mark Brown36ae1a92012-01-06 17:12:45 -08001193 ret = device_create_file(rtd->dev, &dev_attr_codec_reg);
Jarkko Nikula589c3562010-12-06 16:27:07 +02001194 if (ret < 0)
1195 dev_err(codec->dev,
1196 "asoc: failed to add codec sysfs files: %d\n", ret);
1197
1198 return 0;
1199}
1200
Liam Girdwood0168bf02011-06-07 16:08:05 +01001201static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001202{
1203 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1204 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1205 struct snd_soc_codec *codec = rtd->codec;
1206 struct snd_soc_platform *platform = rtd->platform;
1207 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1208 int ret;
1209
Liam Girdwood0168bf02011-06-07 16:08:05 +01001210 dev_dbg(card->dev, "probe %s dai link %d late %d\n",
1211 card->name, num, order);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001212
1213 /* config components */
1214 codec_dai->codec = codec;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001215 cpu_dai->platform = platform;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001216 codec_dai->card = card;
1217 cpu_dai->card = card;
1218
1219 /* set default power off timeout */
1220 rtd->pmdown_time = pmdown_time;
1221
1222 /* probe the cpu_dai */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001223 if (!cpu_dai->probed &&
1224 cpu_dai->driver->probe_order == order) {
Liam Girdwood61b61e32011-05-24 13:57:43 +01001225 if (!try_module_get(cpu_dai->dev->driver->owner))
1226 return -ENODEV;
1227
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001228 if (cpu_dai->driver->probe) {
1229 ret = cpu_dai->driver->probe(cpu_dai);
1230 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001231 pr_err("asoc: failed to probe CPU DAI %s: %d\n",
1232 cpu_dai->name, ret);
Liam Girdwood61b61e32011-05-24 13:57:43 +01001233 module_put(cpu_dai->dev->driver->owner);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001234 return ret;
1235 }
1236 }
1237 cpu_dai->probed = 1;
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001238 /* mark cpu_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001239 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1240 }
1241
1242 /* probe the CODEC */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001243 if (!codec->probed &&
1244 codec->driver->probe_order == order) {
Jarkko Nikula589c3562010-12-06 16:27:07 +02001245 ret = soc_probe_codec(card, codec);
1246 if (ret < 0)
1247 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001248 }
1249
1250 /* probe the platform */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001251 if (!platform->probed &&
1252 platform->driver->probe_order == order) {
Liam Girdwood956245e2011-07-01 16:54:08 +01001253 ret = soc_probe_platform(card, platform);
1254 if (ret < 0)
1255 return ret;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001256 }
1257
1258 /* probe the CODEC DAI */
Liam Girdwood0168bf02011-06-07 16:08:05 +01001259 if (!codec_dai->probed && codec_dai->driver->probe_order == order) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001260 if (codec_dai->driver->probe) {
1261 ret = codec_dai->driver->probe(codec_dai);
1262 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001263 pr_err("asoc: failed to probe CODEC DAI %s: %d\n",
1264 codec_dai->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001265 return ret;
Mark Brown6b05eda2008-12-08 19:26:48 +00001266 }
1267 }
1268
Wolfram Sang1c8371d2011-07-17 18:00:26 +02001269 /* mark codec_dai as probed and add to card dai list */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001270 codec_dai->probed = 1;
1271 list_add(&codec_dai->card_list, &card->dai_dev_list);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001272 }
1273
Liam Girdwood0168bf02011-06-07 16:08:05 +01001274 /* complete DAI probe during last probe */
1275 if (order != SND_SOC_COMP_ORDER_LAST)
1276 return 0;
1277
Jarkko Nikula589c3562010-12-06 16:27:07 +02001278 ret = soc_post_component_init(card, codec, num, 0);
1279 if (ret)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001280 return ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001281
Mark Brown36ae1a92012-01-06 17:12:45 -08001282 ret = device_create_file(rtd->dev, &dev_attr_pmdown_time);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001283 if (ret < 0)
Fabio Estevam0837fc62012-02-17 19:40:38 -02001284 pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001285
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001286 /* create the pcm */
1287 ret = soc_new_pcm(rtd, num);
1288 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001289 pr_err("asoc: can't create pcm %s :%d\n",
1290 dai_link->stream_name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001291 return ret;
1292 }
1293
1294 /* add platform data for AC97 devices */
1295 if (rtd->codec_dai->driver->ac97_control)
1296 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1297
1298 return 0;
1299}
1300
1301#ifdef CONFIG_SND_SOC_AC97_BUS
1302static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1303{
1304 int ret;
1305
1306 /* Only instantiate AC97 if not already done by the adaptor
1307 * for the generic AC97 subsystem.
1308 */
1309 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
Mika Westerberg0562f782010-10-13 11:30:32 +03001310 /*
1311 * It is possible that the AC97 device is already registered to
1312 * the device subsystem. This happens when the device is created
1313 * via snd_ac97_mixer(). Currently only SoC codec that does so
1314 * is the generic AC97 glue but others migh emerge.
1315 *
1316 * In those cases we don't try to register the device again.
1317 */
1318 if (!rtd->codec->ac97_created)
1319 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001320
1321 ret = soc_ac97_dev_register(rtd->codec);
1322 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001323 pr_err("asoc: AC97 device register failed:%d\n", ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001324 return ret;
1325 }
1326
1327 rtd->codec->ac97_registered = 1;
1328 }
1329 return 0;
1330}
1331
1332static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1333{
1334 if (codec->ac97_registered) {
1335 soc_ac97_dev_unregister(codec);
1336 codec->ac97_registered = 0;
1337 }
1338}
1339#endif
1340
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001341static int soc_probe_aux_dev(struct snd_soc_card *card, int num)
1342{
1343 struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num];
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001344 struct snd_soc_codec *codec;
Jarkko Nikula676ad982010-12-03 09:18:22 +02001345 int ret = -ENODEV;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001346
1347 /* find CODEC from registered CODECs*/
1348 list_for_each_entry(codec, &codec_list, list) {
1349 if (!strcmp(codec->name, aux_dev->codec_name)) {
1350 if (codec->probed) {
1351 dev_err(codec->dev,
1352 "asoc: codec already probed");
1353 ret = -EBUSY;
1354 goto out;
1355 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001356 goto found;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001357 }
1358 }
Jarkko Nikula676ad982010-12-03 09:18:22 +02001359 /* codec not found */
1360 dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name);
1361 goto out;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001362
Jarkko Nikula676ad982010-12-03 09:18:22 +02001363found:
Jarkko Nikula589c3562010-12-06 16:27:07 +02001364 ret = soc_probe_codec(card, codec);
1365 if (ret < 0)
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001366 return ret;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001367
Jarkko Nikula589c3562010-12-06 16:27:07 +02001368 ret = soc_post_component_init(card, codec, num, 1);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001369
1370out:
1371 return ret;
1372}
1373
1374static void soc_remove_aux_dev(struct snd_soc_card *card, int num)
1375{
1376 struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num];
1377 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001378
1379 /* unregister the rtd device */
1380 if (rtd->dev_registered) {
Mark Brown36ae1a92012-01-06 17:12:45 -08001381 device_remove_file(rtd->dev, &dev_attr_codec_reg);
1382 device_del(rtd->dev);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001383 rtd->dev_registered = 0;
1384 }
1385
Jarkko Nikula589c3562010-12-06 16:27:07 +02001386 if (codec && codec->probed)
1387 soc_remove_codec(codec);
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001388}
1389
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001390static int snd_soc_init_codec_cache(struct snd_soc_codec *codec,
1391 enum snd_soc_compress_type compress_type)
1392{
1393 int ret;
1394
1395 if (codec->cache_init)
1396 return 0;
1397
1398 /* override the compress_type if necessary */
1399 if (compress_type && codec->compress_type != compress_type)
1400 codec->compress_type = compress_type;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001401 ret = snd_soc_cache_init(codec);
1402 if (ret < 0) {
1403 dev_err(codec->dev, "Failed to set cache compression type: %d\n",
1404 ret);
1405 return ret;
1406 }
1407 codec->cache_init = 1;
1408 return 0;
1409}
1410
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001411static void snd_soc_instantiate_card(struct snd_soc_card *card)
1412{
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001413 struct snd_soc_codec *codec;
1414 struct snd_soc_codec_conf *codec_conf;
1415 enum snd_soc_compress_type compress_type;
Mark Brown75d9ac42011-09-27 16:41:01 +01001416 struct snd_soc_dai_link *dai_link;
Liam Girdwood0168bf02011-06-07 16:08:05 +01001417 int ret, i, order;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001418
1419 mutex_lock(&card->mutex);
1420
1421 if (card->instantiated) {
1422 mutex_unlock(&card->mutex);
1423 return;
1424 }
1425
1426 /* bind DAIs */
1427 for (i = 0; i < card->num_links; i++)
1428 soc_bind_dai_link(card, i);
1429
1430 /* bind completed ? */
1431 if (card->num_rtd != card->num_links) {
1432 mutex_unlock(&card->mutex);
1433 return;
1434 }
1435
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001436 /* initialize the register cache for each available codec */
1437 list_for_each_entry(codec, &codec_list, list) {
1438 if (codec->cache_init)
1439 continue;
Dimitris Papastamos861f2fa2011-01-11 11:43:51 +00001440 /* by default we don't override the compress_type */
1441 compress_type = 0;
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001442 /* check to see if we need to override the compress_type */
1443 for (i = 0; i < card->num_configs; ++i) {
1444 codec_conf = &card->codec_conf[i];
1445 if (!strcmp(codec->name, codec_conf->dev_name)) {
1446 compress_type = codec_conf->compress_type;
1447 if (compress_type && compress_type
1448 != codec->compress_type)
1449 break;
1450 }
1451 }
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00001452 ret = snd_soc_init_codec_cache(codec, compress_type);
1453 if (ret < 0) {
1454 mutex_unlock(&card->mutex);
1455 return;
1456 }
1457 }
1458
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001459 /* card bind complete so register a sound card */
1460 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1461 card->owner, 0, &card->snd_card);
1462 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001463 pr_err("asoc: can't create sound card for card %s: %d\n",
1464 card->name, ret);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001465 mutex_unlock(&card->mutex);
1466 return;
1467 }
1468 card->snd_card->dev = card->dev;
1469
Mark Browne37a4972011-03-02 18:21:57 +00001470 card->dapm.bias_level = SND_SOC_BIAS_OFF;
1471 card->dapm.dev = card->dev;
1472 card->dapm.card = card;
1473 list_add(&card->dapm.list, &card->dapm_list);
1474
Lars-Peter Clausend5d1e0b2011-04-30 19:45:49 +02001475#ifdef CONFIG_DEBUG_FS
1476 snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root);
1477#endif
1478
Mark Brown88ee1c62011-02-02 10:43:26 +00001479#ifdef CONFIG_PM_SLEEP
Andy Green6ed25972008-06-13 16:24:05 +01001480 /* deferred resume work */
Mark Brown63084192008-12-02 15:08:03 +00001481 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
Randy Dunlap1301a962008-06-17 19:19:34 +01001482#endif
Andy Green6ed25972008-06-13 16:24:05 +01001483
Mark Brown9a841eb2011-04-12 17:51:37 -07001484 if (card->dapm_widgets)
1485 snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets,
1486 card->num_dapm_widgets);
1487
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001488 /* initialise the sound card only once */
1489 if (card->probe) {
Mark Browne7361ec2011-01-26 14:17:20 +00001490 ret = card->probe(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001491 if (ret < 0)
1492 goto card_probe_error;
1493 }
1494
Liam Girdwood0168bf02011-06-07 16:08:05 +01001495 /* early DAI link probe */
1496 for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST;
1497 order++) {
1498 for (i = 0; i < card->num_links; i++) {
1499 ret = soc_probe_dai_link(card, i, order);
1500 if (ret < 0) {
1501 pr_err("asoc: failed to instantiate card %s: %d\n",
Mark Brown321de0d2010-09-21 15:09:37 +01001502 card->name, ret);
Liam Girdwood0168bf02011-06-07 16:08:05 +01001503 goto probe_dai_err;
1504 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001505 }
1506 }
1507
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001508 for (i = 0; i < card->num_aux_devs; i++) {
1509 ret = soc_probe_aux_dev(card, i);
1510 if (ret < 0) {
1511 pr_err("asoc: failed to add auxiliary devices %s: %d\n",
1512 card->name, ret);
1513 goto probe_aux_dev_err;
1514 }
1515 }
1516
Mark Brown888df392012-02-16 19:37:51 -08001517 snd_soc_dapm_link_dai_widgets(card);
1518
Mark Brownb7af1da2011-04-07 19:18:44 +09001519 if (card->controls)
Liam Girdwood022658b2012-02-03 17:43:09 +00001520 snd_soc_add_card_controls(card, card->controls, card->num_controls);
Mark Brownb7af1da2011-04-07 19:18:44 +09001521
Mark Brownb8ad29d2011-03-02 18:35:51 +00001522 if (card->dapm_routes)
1523 snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes,
1524 card->num_dapm_routes);
1525
Mark Brownb90d2f92011-10-10 13:38:06 +01001526 snd_soc_dapm_new_widgets(&card->dapm);
1527
Mark Brown75d9ac42011-09-27 16:41:01 +01001528 for (i = 0; i < card->num_links; i++) {
1529 dai_link = &card->dai_link[i];
1530
1531 if (dai_link->dai_fmt) {
1532 ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai,
1533 dai_link->dai_fmt);
1534 if (ret != 0)
1535 dev_warn(card->rtd[i].codec_dai->dev,
1536 "Failed to set DAI format: %d\n",
1537 ret);
1538
1539 ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai,
1540 dai_link->dai_fmt);
1541 if (ret != 0)
1542 dev_warn(card->rtd[i].cpu_dai->dev,
1543 "Failed to set DAI format: %d\n",
1544 ret);
1545 }
1546 }
1547
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001548 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001549 "%s", card->name);
Liam Girdwood22de71b2011-05-12 16:14:04 +01001550 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1551 "%s", card->long_name ? card->long_name : card->name);
Mark Brownf0e8ed82011-09-20 11:41:54 +01001552 snprintf(card->snd_card->driver, sizeof(card->snd_card->driver),
1553 "%s", card->driver_name ? card->driver_name : card->name);
1554 for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) {
1555 switch (card->snd_card->driver[i]) {
1556 case '_':
1557 case '-':
1558 case '\0':
1559 break;
1560 default:
1561 if (!isalnum(card->snd_card->driver[i]))
1562 card->snd_card->driver[i] = '_';
1563 break;
1564 }
1565 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001566
Mark Brown28e9ad92011-03-02 18:36:34 +00001567 if (card->late_probe) {
1568 ret = card->late_probe(card);
1569 if (ret < 0) {
1570 dev_err(card->dev, "%s late_probe() failed: %d\n",
1571 card->name, ret);
1572 goto probe_aux_dev_err;
1573 }
1574 }
1575
Mark Brown2dc00212011-10-08 13:59:44 +01001576 snd_soc_dapm_new_widgets(&card->dapm);
1577
Stephen Warren16332812011-11-23 12:42:04 -07001578 if (card->fully_routed)
Mark Brownb05d8dc2011-11-27 19:38:34 +00001579 list_for_each_entry(codec, &card->codec_dev_list, card_list)
Stephen Warren16332812011-11-23 12:42:04 -07001580 snd_soc_dapm_auto_nc_codec_pins(codec);
1581
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001582 ret = snd_card_register(card->snd_card);
1583 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001584 pr_err("asoc: failed to register soundcard for %s: %d\n",
1585 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001586 goto probe_aux_dev_err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001587 }
1588
1589#ifdef CONFIG_SND_SOC_AC97_BUS
1590 /* register any AC97 codecs */
1591 for (i = 0; i < card->num_rtd; i++) {
Axel Lin6b3ed782010-12-07 16:12:29 +08001592 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1593 if (ret < 0) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02001594 pr_err("asoc: failed to register AC97 %s: %d\n",
1595 card->name, ret);
Axel Lin6b3ed782010-12-07 16:12:29 +08001596 while (--i >= 0)
Mark Brown7d8316d2010-12-13 17:03:27 +00001597 soc_unregister_ac97_dai_link(card->rtd[i].codec);
Axel Lin6b3ed782010-12-07 16:12:29 +08001598 goto probe_aux_dev_err;
Mark Brownfe3e78e2009-11-03 22:13:13 +00001599 }
Axel Lin6b3ed782010-12-07 16:12:29 +08001600 }
Mark Brownfe3e78e2009-11-03 22:13:13 +00001601#endif
1602
Mark Brown435c5e22008-12-04 15:32:53 +00001603 card->instantiated = 1;
Mark Brown4f4c0072011-10-07 14:29:19 +01001604 snd_soc_dapm_sync(&card->dapm);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001605 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001606 return;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001607
Jarkko Nikula2eea3922010-11-25 17:47:38 +02001608probe_aux_dev_err:
1609 for (i = 0; i < card->num_aux_devs; i++)
1610 soc_remove_aux_dev(card, i);
1611
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001612probe_dai_err:
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001613 soc_remove_dai_links(card);
Mark Brownfe3e78e2009-11-03 22:13:13 +00001614
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001615card_probe_error:
Mark Brown87506542008-11-18 20:50:34 +00001616 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001617 card->remove(card);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001618
1619 snd_card_free(card->snd_card);
1620
1621 mutex_unlock(&card->mutex);
Mark Brown435c5e22008-12-04 15:32:53 +00001622}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001623
Mark Brown435c5e22008-12-04 15:32:53 +00001624/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02001625 * Attempt to initialise any uninitialised cards. Must be called with
Mark Brown435c5e22008-12-04 15:32:53 +00001626 * client_mutex.
1627 */
1628static void snd_soc_instantiate_cards(void)
1629{
1630 struct snd_soc_card *card;
1631 list_for_each_entry(card, &card_list, list)
1632 snd_soc_instantiate_card(card);
1633}
1634
1635/* probes a new socdev */
1636static int soc_probe(struct platform_device *pdev)
1637{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001638 struct snd_soc_card *card = platform_get_drvdata(pdev);
Mark Brown435c5e22008-12-04 15:32:53 +00001639 int ret = 0;
Mark Brown435c5e22008-12-04 15:32:53 +00001640
Vinod Koul70a7ca32011-01-14 19:22:48 +05301641 /*
1642 * no card, so machine driver should be registering card
1643 * we should not be here in that case so ret error
1644 */
1645 if (!card)
1646 return -EINVAL;
1647
Mark Brown435c5e22008-12-04 15:32:53 +00001648 /* Bodge while we unpick instantiation */
1649 card->dev = &pdev->dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001650
Mark Brown435c5e22008-12-04 15:32:53 +00001651 ret = snd_soc_register_card(card);
1652 if (ret != 0) {
1653 dev_err(&pdev->dev, "Failed to register card\n");
1654 return ret;
1655 }
1656
1657 return 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001658}
1659
Vinod Koulb0e26482011-01-13 22:48:02 +05301660static int soc_cleanup_card_resources(struct snd_soc_card *card)
1661{
Vinod Koulb0e26482011-01-13 22:48:02 +05301662 int i;
1663
1664 /* make sure any delayed work runs */
1665 for (i = 0; i < card->num_rtd; i++) {
1666 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1667 flush_delayed_work_sync(&rtd->delayed_work);
1668 }
1669
1670 /* remove auxiliary devices */
1671 for (i = 0; i < card->num_aux_devs; i++)
1672 soc_remove_aux_dev(card, i);
1673
1674 /* remove and free each DAI */
Kuninori Morimoto0671fd82011-04-08 14:50:44 +09001675 soc_remove_dai_links(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301676
1677 soc_cleanup_card_debugfs(card);
1678
1679 /* remove the card */
1680 if (card->remove)
Mark Browne7361ec2011-01-26 14:17:20 +00001681 card->remove(card);
Vinod Koulb0e26482011-01-13 22:48:02 +05301682
Lars-Peter Clausen0aaae522011-04-30 19:45:47 +02001683 snd_soc_dapm_free(&card->dapm);
1684
Vinod Koulb0e26482011-01-13 22:48:02 +05301685 kfree(card->rtd);
1686 snd_card_free(card->snd_card);
1687 return 0;
1688
1689}
1690
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001691/* removes a socdev */
1692static int soc_remove(struct platform_device *pdev)
1693{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001694 struct snd_soc_card *card = platform_get_drvdata(pdev);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001695
Mark Brownc5af3a22008-11-28 13:29:45 +00001696 snd_soc_unregister_card(card);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001697 return 0;
1698}
1699
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001700int snd_soc_poweroff(struct device *dev)
Mark Brown51737472009-06-22 13:16:51 +01001701{
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001702 struct snd_soc_card *card = dev_get_drvdata(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001703 int i;
Mark Brown51737472009-06-22 13:16:51 +01001704
1705 if (!card->instantiated)
Mark Brown416356f2009-06-30 19:05:15 +01001706 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001707
1708 /* Flush out pmdown_time work - we actually do want to run it
1709 * now, we're shutting down so no imminent restart. */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001710 for (i = 0; i < card->num_rtd; i++) {
1711 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
Tejun Heo5b84ba22010-12-11 17:51:26 +01001712 flush_delayed_work_sync(&rtd->delayed_work);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001713 }
Mark Brown51737472009-06-22 13:16:51 +01001714
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001715 snd_soc_dapm_shutdown(card);
Mark Brown416356f2009-06-30 19:05:15 +01001716
1717 return 0;
Mark Brown51737472009-06-22 13:16:51 +01001718}
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001719EXPORT_SYMBOL_GPL(snd_soc_poweroff);
Mark Brown51737472009-06-22 13:16:51 +01001720
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001721const struct dev_pm_ops snd_soc_pm_ops = {
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301722 .suspend = snd_soc_suspend,
1723 .resume = snd_soc_resume,
1724 .freeze = snd_soc_suspend,
1725 .thaw = snd_soc_resume,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001726 .poweroff = snd_soc_poweroff,
Viresh Kumarb1dd5892012-02-24 16:25:49 +05301727 .restore = snd_soc_resume,
Mark Brown416356f2009-06-30 19:05:15 +01001728};
Stephen Warrendeb26072011-04-05 19:35:30 -06001729EXPORT_SYMBOL_GPL(snd_soc_pm_ops);
Mark Brown416356f2009-06-30 19:05:15 +01001730
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001731/* ASoC platform driver */
1732static struct platform_driver soc_driver = {
1733 .driver = {
1734 .name = "soc-audio",
Kay Sievers8b45a202008-04-14 13:33:36 +02001735 .owner = THIS_MODULE,
Mark Brown6f8ab4a2011-01-26 14:59:27 +00001736 .pm = &snd_soc_pm_ops,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001737 },
1738 .probe = soc_probe,
1739 .remove = soc_remove,
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001740};
1741
Mark Brown096e49d2009-07-05 15:12:22 +01001742/**
1743 * snd_soc_codec_volatile_register: Report if a register is volatile.
1744 *
1745 * @codec: CODEC to query.
1746 * @reg: Register to query.
1747 *
1748 * Boolean function indiciating if a CODEC register is volatile.
1749 */
Mark Brown181e0552011-01-24 14:05:25 +00001750int snd_soc_codec_volatile_register(struct snd_soc_codec *codec,
1751 unsigned int reg)
Mark Brown096e49d2009-07-05 15:12:22 +01001752{
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00001753 if (codec->volatile_register)
1754 return codec->volatile_register(codec, reg);
Mark Brown096e49d2009-07-05 15:12:22 +01001755 else
1756 return 0;
1757}
1758EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1759
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001760/**
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001761 * snd_soc_codec_readable_register: Report if a register is readable.
1762 *
1763 * @codec: CODEC to query.
1764 * @reg: Register to query.
1765 *
1766 * Boolean function indicating if a CODEC register is readable.
1767 */
1768int snd_soc_codec_readable_register(struct snd_soc_codec *codec,
1769 unsigned int reg)
1770{
1771 if (codec->readable_register)
1772 return codec->readable_register(codec, reg);
1773 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001774 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001775}
1776EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register);
1777
1778/**
1779 * snd_soc_codec_writable_register: Report if a register is writable.
1780 *
1781 * @codec: CODEC to query.
1782 * @reg: Register to query.
1783 *
1784 * Boolean function indicating if a CODEC register is writable.
1785 */
1786int snd_soc_codec_writable_register(struct snd_soc_codec *codec,
1787 unsigned int reg)
1788{
1789 if (codec->writable_register)
1790 return codec->writable_register(codec, reg);
1791 else
Lars-Peter Clausen63fa0a22011-08-27 18:24:12 +02001792 return 1;
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001793}
1794EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register);
1795
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001796int snd_soc_platform_read(struct snd_soc_platform *platform,
1797 unsigned int reg)
1798{
1799 unsigned int ret;
1800
1801 if (!platform->driver->read) {
1802 dev_err(platform->dev, "platform has no read back\n");
1803 return -1;
1804 }
1805
1806 ret = platform->driver->read(platform, reg);
1807 dev_dbg(platform->dev, "read %x => %x\n", reg, ret);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001808 trace_snd_soc_preg_read(platform, reg, ret);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001809
1810 return ret;
1811}
1812EXPORT_SYMBOL_GPL(snd_soc_platform_read);
1813
1814int snd_soc_platform_write(struct snd_soc_platform *platform,
1815 unsigned int reg, unsigned int val)
1816{
1817 if (!platform->driver->write) {
1818 dev_err(platform->dev, "platform has no write back\n");
1819 return -1;
1820 }
1821
1822 dev_dbg(platform->dev, "write %x = %x\n", reg, val);
Liam Girdwooda82ce2a2011-07-04 22:10:50 +01001823 trace_snd_soc_preg_write(platform, reg, val);
Liam Girdwoodf1442bc2011-07-04 11:10:15 +01001824 return platform->driver->write(platform, reg, val);
1825}
1826EXPORT_SYMBOL_GPL(snd_soc_platform_write);
1827
Dimitris Papastamos239c9702011-03-24 13:45:18 +00001828/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001829 * snd_soc_new_ac97_codec - initailise AC97 device
1830 * @codec: audio codec
1831 * @ops: AC97 bus operations
1832 * @num: AC97 codec number
1833 *
1834 * Initialises AC97 codec resources for use by ad-hoc devices only.
1835 */
1836int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1837 struct snd_ac97_bus_ops *ops, int num)
1838{
1839 mutex_lock(&codec->mutex);
1840
1841 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1842 if (codec->ac97 == NULL) {
1843 mutex_unlock(&codec->mutex);
1844 return -ENOMEM;
1845 }
1846
1847 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1848 if (codec->ac97->bus == NULL) {
1849 kfree(codec->ac97);
1850 codec->ac97 = NULL;
1851 mutex_unlock(&codec->mutex);
1852 return -ENOMEM;
1853 }
1854
1855 codec->ac97->bus->ops = ops;
1856 codec->ac97->num = num;
Mika Westerberg0562f782010-10-13 11:30:32 +03001857
1858 /*
1859 * Mark the AC97 device to be created by us. This way we ensure that the
1860 * device will be registered with the device subsystem later on.
1861 */
1862 codec->ac97_created = 1;
1863
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001864 mutex_unlock(&codec->mutex);
1865 return 0;
1866}
1867EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1868
1869/**
1870 * snd_soc_free_ac97_codec - free AC97 codec device
1871 * @codec: audio codec
1872 *
1873 * Frees AC97 codec device resources.
1874 */
1875void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1876{
1877 mutex_lock(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00001878#ifdef CONFIG_SND_SOC_AC97_BUS
1879 soc_unregister_ac97_dai_link(codec);
1880#endif
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001881 kfree(codec->ac97->bus);
1882 kfree(codec->ac97);
1883 codec->ac97 = NULL;
Mika Westerberg0562f782010-10-13 11:30:32 +03001884 codec->ac97_created = 0;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001885 mutex_unlock(&codec->mutex);
1886}
1887EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1888
Mark Brownc3753702010-11-01 15:41:57 -04001889unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg)
1890{
1891 unsigned int ret;
1892
Mark Brownc3acec22010-12-02 16:15:29 +00001893 ret = codec->read(codec, reg);
Mark Brownc3753702010-11-01 15:41:57 -04001894 dev_dbg(codec->dev, "read %x => %x\n", reg, ret);
Mark Browna8b1d342010-11-03 18:05:58 -04001895 trace_snd_soc_reg_read(codec, reg, ret);
Mark Brownc3753702010-11-01 15:41:57 -04001896
1897 return ret;
1898}
1899EXPORT_SYMBOL_GPL(snd_soc_read);
1900
1901unsigned int snd_soc_write(struct snd_soc_codec *codec,
1902 unsigned int reg, unsigned int val)
1903{
1904 dev_dbg(codec->dev, "write %x = %x\n", reg, val);
Mark Browna8b1d342010-11-03 18:05:58 -04001905 trace_snd_soc_reg_write(codec, reg, val);
Mark Brownc3acec22010-12-02 16:15:29 +00001906 return codec->write(codec, reg, val);
Mark Brownc3753702010-11-01 15:41:57 -04001907}
1908EXPORT_SYMBOL_GPL(snd_soc_write);
1909
Dimitris Papastamos5fb609d2011-03-22 10:37:03 +00001910unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec,
1911 unsigned int reg, const void *data, size_t len)
1912{
1913 return codec->bulk_write_raw(codec, reg, data, len);
1914}
1915EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw);
1916
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001917/**
1918 * snd_soc_update_bits - update codec register bits
1919 * @codec: audio codec
1920 * @reg: codec register
1921 * @mask: register mask
1922 * @value: new value
1923 *
1924 * Writes new register value.
1925 *
Timur Tabi180c3292011-01-10 15:58:13 -06001926 * Returns 1 for change, 0 for no change, or negative error code.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001927 */
1928int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001929 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001930{
Mark Brown8a713da2011-12-03 12:33:55 +00001931 bool change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001932 unsigned int old, new;
Timur Tabi180c3292011-01-10 15:58:13 -06001933 int ret;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001934
Mark Brown8a713da2011-12-03 12:33:55 +00001935 if (codec->using_regmap) {
1936 ret = regmap_update_bits_check(codec->control_data, reg,
1937 mask, value, &change);
1938 } else {
1939 ret = snd_soc_read(codec, reg);
Timur Tabi180c3292011-01-10 15:58:13 -06001940 if (ret < 0)
1941 return ret;
Mark Brown8a713da2011-12-03 12:33:55 +00001942
1943 old = ret;
1944 new = (old & ~mask) | (value & mask);
1945 change = old != new;
1946 if (change)
1947 ret = snd_soc_write(codec, reg, new);
Timur Tabi180c3292011-01-10 15:58:13 -06001948 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001949
Mark Brown8a713da2011-12-03 12:33:55 +00001950 if (ret < 0)
1951 return ret;
1952
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001953 return change;
1954}
1955EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1956
1957/**
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001958 * snd_soc_update_bits_locked - update codec register bits
1959 * @codec: audio codec
1960 * @reg: codec register
1961 * @mask: register mask
1962 * @value: new value
1963 *
1964 * Writes new register value, and takes the codec mutex.
1965 *
1966 * Returns 1 for change else 0.
1967 */
Mark Browndd1b3d52009-12-04 14:22:03 +00001968int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1969 unsigned short reg, unsigned int mask,
1970 unsigned int value)
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001971{
1972 int change;
1973
1974 mutex_lock(&codec->mutex);
1975 change = snd_soc_update_bits(codec, reg, mask, value);
1976 mutex_unlock(&codec->mutex);
1977
1978 return change;
1979}
Mark Browndd1b3d52009-12-04 14:22:03 +00001980EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
Eero Nurkkala6c508c62009-10-30 13:34:03 +02001981
1982/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001983 * snd_soc_test_bits - test register for change
1984 * @codec: audio codec
1985 * @reg: codec register
1986 * @mask: register mask
1987 * @value: new value
1988 *
1989 * Tests a register with a new value and checks if the new value is
1990 * different from the old value.
1991 *
1992 * Returns 1 for change else 0.
1993 */
1994int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001995 unsigned int mask, unsigned int value)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001996{
1997 int change;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03001998 unsigned int old, new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02001999
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002000 old = snd_soc_read(codec, reg);
2001 new = (old & ~mask) | value;
2002 change = old != new;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002003
2004 return change;
2005}
2006EXPORT_SYMBOL_GPL(snd_soc_test_bits);
2007
2008/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002009 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
2010 * @substream: the pcm substream
2011 * @hw: the hardware parameters
2012 *
2013 * Sets the substream runtime hardware parameters.
2014 */
2015int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
2016 const struct snd_pcm_hardware *hw)
2017{
2018 struct snd_pcm_runtime *runtime = substream->runtime;
2019 runtime->hw.info = hw->info;
2020 runtime->hw.formats = hw->formats;
2021 runtime->hw.period_bytes_min = hw->period_bytes_min;
2022 runtime->hw.period_bytes_max = hw->period_bytes_max;
2023 runtime->hw.periods_min = hw->periods_min;
2024 runtime->hw.periods_max = hw->periods_max;
2025 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
2026 runtime->hw.fifo_size = hw->fifo_size;
2027 return 0;
2028}
2029EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
2030
2031/**
2032 * snd_soc_cnew - create new control
2033 * @_template: control template
2034 * @data: control private data
Mark Brownac11a2b2009-01-01 12:18:17 +00002035 * @long_name: control long name
Mark Brownefb7ac32011-03-08 17:23:24 +00002036 * @prefix: control name prefix
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002037 *
2038 * Create a new mixer control from a template control.
2039 *
2040 * Returns 0 for success, else error.
2041 */
2042struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
Mark Brown30565572012-02-16 17:07:42 -08002043 void *data, const char *long_name,
Mark Brownefb7ac32011-03-08 17:23:24 +00002044 const char *prefix)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002045{
2046 struct snd_kcontrol_new template;
Mark Brownefb7ac32011-03-08 17:23:24 +00002047 struct snd_kcontrol *kcontrol;
2048 char *name = NULL;
2049 int name_len;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002050
2051 memcpy(&template, _template, sizeof(template));
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002052 template.index = 0;
2053
Mark Brownefb7ac32011-03-08 17:23:24 +00002054 if (!long_name)
2055 long_name = template.name;
2056
2057 if (prefix) {
2058 name_len = strlen(long_name) + strlen(prefix) + 2;
Axel Lin57cf9d42011-08-20 11:03:44 +08002059 name = kmalloc(name_len, GFP_KERNEL);
Mark Brownefb7ac32011-03-08 17:23:24 +00002060 if (!name)
2061 return NULL;
2062
2063 snprintf(name, name_len, "%s %s", prefix, long_name);
2064
2065 template.name = name;
2066 } else {
2067 template.name = long_name;
2068 }
2069
2070 kcontrol = snd_ctl_new1(&template, data);
2071
2072 kfree(name);
2073
2074 return kcontrol;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002075}
2076EXPORT_SYMBOL_GPL(snd_soc_cnew);
2077
Liam Girdwood022658b2012-02-03 17:43:09 +00002078static int snd_soc_add_controls(struct snd_card *card, struct device *dev,
2079 const struct snd_kcontrol_new *controls, int num_controls,
2080 const char *prefix, void *data)
2081{
2082 int err, i;
2083
2084 for (i = 0; i < num_controls; i++) {
2085 const struct snd_kcontrol_new *control = &controls[i];
2086 err = snd_ctl_add(card, snd_soc_cnew(control, data,
2087 control->name, prefix));
2088 if (err < 0) {
2089 dev_err(dev, "Failed to add %s: %d\n", control->name, err);
2090 return err;
2091 }
2092 }
2093
2094 return 0;
2095}
2096
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002097/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002098 * snd_soc_add_codec_controls - add an array of controls to a codec.
2099 * Convenience function to add a list of controls. Many codecs were
Ian Molton3e8e1952009-01-09 00:23:21 +00002100 * duplicating this code.
2101 *
2102 * @codec: codec to add controls to
2103 * @controls: array of controls to add
2104 * @num_controls: number of elements in the array
2105 *
2106 * Return 0 for success, else error.
2107 */
Liam Girdwood022658b2012-02-03 17:43:09 +00002108int snd_soc_add_codec_controls(struct snd_soc_codec *codec,
Ian Molton3e8e1952009-01-09 00:23:21 +00002109 const struct snd_kcontrol_new *controls, int num_controls)
2110{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002111 struct snd_card *card = codec->card->snd_card;
Ian Molton3e8e1952009-01-09 00:23:21 +00002112
Liam Girdwood022658b2012-02-03 17:43:09 +00002113 return snd_soc_add_controls(card, codec->dev, controls, num_controls,
2114 codec->name_prefix, codec);
Ian Molton3e8e1952009-01-09 00:23:21 +00002115}
Liam Girdwood022658b2012-02-03 17:43:09 +00002116EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls);
Ian Molton3e8e1952009-01-09 00:23:21 +00002117
2118/**
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002119 * snd_soc_add_platform_controls - add an array of controls to a platform.
Liam Girdwood022658b2012-02-03 17:43:09 +00002120 * Convenience function to add a list of controls.
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002121 *
2122 * @platform: platform to add controls to
2123 * @controls: array of controls to add
2124 * @num_controls: number of elements in the array
2125 *
2126 * Return 0 for success, else error.
2127 */
2128int snd_soc_add_platform_controls(struct snd_soc_platform *platform,
2129 const struct snd_kcontrol_new *controls, int num_controls)
2130{
2131 struct snd_card *card = platform->card->snd_card;
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002132
Liam Girdwood022658b2012-02-03 17:43:09 +00002133 return snd_soc_add_controls(card, platform->dev, controls, num_controls,
2134 NULL, platform);
Liam Girdwooda491a5c2011-07-04 22:10:51 +01002135}
2136EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls);
2137
2138/**
Liam Girdwood022658b2012-02-03 17:43:09 +00002139 * snd_soc_add_card_controls - add an array of controls to a SoC card.
2140 * Convenience function to add a list of controls.
2141 *
2142 * @soc_card: SoC card to add controls to
2143 * @controls: array of controls to add
2144 * @num_controls: number of elements in the array
2145 *
2146 * Return 0 for success, else error.
2147 */
2148int snd_soc_add_card_controls(struct snd_soc_card *soc_card,
2149 const struct snd_kcontrol_new *controls, int num_controls)
2150{
2151 struct snd_card *card = soc_card->snd_card;
2152
2153 return snd_soc_add_controls(card, soc_card->dev, controls, num_controls,
2154 NULL, soc_card);
2155}
2156EXPORT_SYMBOL_GPL(snd_soc_add_card_controls);
2157
2158/**
2159 * snd_soc_add_dai_controls - add an array of controls to a DAI.
2160 * Convienience function to add a list of controls.
2161 *
2162 * @dai: DAI to add controls to
2163 * @controls: array of controls to add
2164 * @num_controls: number of elements in the array
2165 *
2166 * Return 0 for success, else error.
2167 */
2168int snd_soc_add_dai_controls(struct snd_soc_dai *dai,
2169 const struct snd_kcontrol_new *controls, int num_controls)
2170{
2171 struct snd_card *card = dai->card->snd_card;
2172
2173 return snd_soc_add_controls(card, dai->dev, controls, num_controls,
2174 NULL, dai);
2175}
2176EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls);
2177
2178/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002179 * snd_soc_info_enum_double - enumerated double mixer info callback
2180 * @kcontrol: mixer control
2181 * @uinfo: control element information
2182 *
2183 * Callback to provide information about a double enumerated
2184 * mixer control.
2185 *
2186 * Returns 0 for success.
2187 */
2188int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_info *uinfo)
2190{
2191 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2192
2193 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2194 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002195 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002196
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002197 if (uinfo->value.enumerated.item > e->max - 1)
2198 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002199 strcpy(uinfo->value.enumerated.name,
2200 e->texts[uinfo->value.enumerated.item]);
2201 return 0;
2202}
2203EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
2204
2205/**
2206 * snd_soc_get_enum_double - enumerated double mixer get callback
2207 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002208 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002209 *
2210 * Callback to get the value of a double enumerated mixer.
2211 *
2212 * Returns 0 for success.
2213 */
2214int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_value *ucontrol)
2216{
2217 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2218 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002219 unsigned int val, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002220
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002221 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002222 ;
2223 val = snd_soc_read(codec, e->reg);
Mark Brown3ff3f642008-05-19 12:32:25 +02002224 ucontrol->value.enumerated.item[0]
2225 = (val >> e->shift_l) & (bitmask - 1);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002226 if (e->shift_l != e->shift_r)
2227 ucontrol->value.enumerated.item[1] =
2228 (val >> e->shift_r) & (bitmask - 1);
2229
2230 return 0;
2231}
2232EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
2233
2234/**
2235 * snd_soc_put_enum_double - enumerated double mixer put callback
2236 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002237 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002238 *
2239 * Callback to set the value of a double enumerated mixer.
2240 *
2241 * Returns 0 for success.
2242 */
2243int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
2244 struct snd_ctl_elem_value *ucontrol)
2245{
2246 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2247 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002248 unsigned int val;
2249 unsigned int mask, bitmask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002250
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002251 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002252 ;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002253 if (ucontrol->value.enumerated.item[0] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002254 return -EINVAL;
2255 val = ucontrol->value.enumerated.item[0] << e->shift_l;
2256 mask = (bitmask - 1) << e->shift_l;
2257 if (e->shift_l != e->shift_r) {
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002258 if (ucontrol->value.enumerated.item[1] > e->max - 1)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002259 return -EINVAL;
2260 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
2261 mask |= (bitmask - 1) << e->shift_r;
2262 }
2263
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002264 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002265}
2266EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2267
2268/**
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002269 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2270 * @kcontrol: mixer control
2271 * @ucontrol: control element information
2272 *
2273 * Callback to get the value of a double semi enumerated mixer.
2274 *
2275 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2276 * used for handling bitfield coded enumeration for example.
2277 *
2278 * Returns 0 for success.
2279 */
2280int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2282{
2283 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002284 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002285 unsigned int reg_val, val, mux;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002286
2287 reg_val = snd_soc_read(codec, e->reg);
2288 val = (reg_val >> e->shift_l) & e->mask;
2289 for (mux = 0; mux < e->max; mux++) {
2290 if (val == e->values[mux])
2291 break;
2292 }
2293 ucontrol->value.enumerated.item[0] = mux;
2294 if (e->shift_l != e->shift_r) {
2295 val = (reg_val >> e->shift_r) & e->mask;
2296 for (mux = 0; mux < e->max; mux++) {
2297 if (val == e->values[mux])
2298 break;
2299 }
2300 ucontrol->value.enumerated.item[1] = mux;
2301 }
2302
2303 return 0;
2304}
2305EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2306
2307/**
2308 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2309 * @kcontrol: mixer control
2310 * @ucontrol: control element information
2311 *
2312 * Callback to set the value of a double semi enumerated mixer.
2313 *
2314 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2315 * used for handling bitfield coded enumeration for example.
2316 *
2317 * Returns 0 for success.
2318 */
2319int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2320 struct snd_ctl_elem_value *ucontrol)
2321{
2322 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Peter Ujfalusi74155552009-01-08 13:34:29 +02002323 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002324 unsigned int val;
2325 unsigned int mask;
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002326
2327 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2328 return -EINVAL;
2329 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2330 mask = e->mask << e->shift_l;
2331 if (e->shift_l != e->shift_r) {
2332 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2333 return -EINVAL;
2334 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2335 mask |= e->mask << e->shift_r;
2336 }
2337
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002338 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
Peter Ujfalusi2e72f8e2009-01-05 09:54:57 +02002339}
2340EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2341
2342/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002343 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2344 * @kcontrol: mixer control
2345 * @uinfo: control element information
2346 *
2347 * Callback to provide information about an external enumerated
2348 * single mixer.
2349 *
2350 * Returns 0 for success.
2351 */
2352int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_info *uinfo)
2354{
2355 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2356
2357 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2358 uinfo->count = 1;
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002359 uinfo->value.enumerated.items = e->max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002360
Jon Smirlf8ba0b72008-07-29 11:42:27 +01002361 if (uinfo->value.enumerated.item > e->max - 1)
2362 uinfo->value.enumerated.item = e->max - 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002363 strcpy(uinfo->value.enumerated.name,
2364 e->texts[uinfo->value.enumerated.item]);
2365 return 0;
2366}
2367EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2368
2369/**
2370 * snd_soc_info_volsw_ext - external single mixer info callback
2371 * @kcontrol: mixer control
2372 * @uinfo: control element information
2373 *
2374 * Callback to provide information about a single external mixer control.
2375 *
2376 * Returns 0 for success.
2377 */
2378int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2379 struct snd_ctl_elem_info *uinfo)
2380{
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002381 int max = kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002382
Mark Brownfd5dfad2009-04-15 21:37:46 +01002383 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002384 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2385 else
2386 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2387
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002388 uinfo->count = 1;
2389 uinfo->value.integer.min = 0;
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002390 uinfo->value.integer.max = max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002391 return 0;
2392}
2393EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2394
2395/**
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002396 * snd_soc_info_volsw - single mixer info callback
2397 * @kcontrol: mixer control
2398 * @uinfo: control element information
2399 *
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002400 * Callback to provide information about a single mixer control, or a double
2401 * mixer control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002402 *
2403 * Returns 0 for success.
2404 */
2405int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2406 struct snd_ctl_elem_info *uinfo)
2407{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002408 struct soc_mixer_control *mc =
2409 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002410 int platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002411
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002412 if (!mc->platform_max)
2413 mc->platform_max = mc->max;
2414 platform_max = mc->platform_max;
2415
2416 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002417 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2418 else
2419 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2420
Peter Ujfalusie8f5a102011-10-05 10:29:23 +03002421 uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002422 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002423 uinfo->value.integer.max = platform_max;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002424 return 0;
2425}
2426EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2427
2428/**
2429 * snd_soc_get_volsw - single mixer get callback
2430 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002431 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002432 *
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002433 * Callback to get the value of a single mixer control, or a double mixer
2434 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002435 *
2436 * Returns 0 for success.
2437 */
2438int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002441 struct soc_mixer_control *mc =
2442 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002443 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002444 unsigned int reg = mc->reg;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002445 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002446 unsigned int shift = mc->shift;
2447 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002448 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002449 unsigned int mask = (1 << fls(max)) - 1;
2450 unsigned int invert = mc->invert;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002451
2452 ucontrol->value.integer.value[0] =
2453 (snd_soc_read(codec, reg) >> shift) & mask;
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002454 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002455 ucontrol->value.integer.value[0] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002456 max - ucontrol->value.integer.value[0];
Peter Ujfalusif7915d92011-10-05 10:29:24 +03002457
2458 if (snd_soc_volsw_is_stereo(mc)) {
2459 if (reg == reg2)
2460 ucontrol->value.integer.value[1] =
2461 (snd_soc_read(codec, reg) >> rshift) & mask;
2462 else
2463 ucontrol->value.integer.value[1] =
2464 (snd_soc_read(codec, reg2) >> shift) & mask;
2465 if (invert)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002466 ucontrol->value.integer.value[1] =
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002467 max - ucontrol->value.integer.value[1];
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002468 }
2469
2470 return 0;
2471}
2472EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2473
2474/**
2475 * snd_soc_put_volsw - single mixer put callback
2476 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002477 * @ucontrol: control element information
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002478 *
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002479 * Callback to set the value of a single mixer control, or a double mixer
2480 * control that spans 2 registers.
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002481 *
2482 * Returns 0 for success.
2483 */
2484int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002487 struct soc_mixer_control *mc =
2488 (struct soc_mixer_control *)kcontrol->private_value;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002489 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002490 unsigned int reg = mc->reg;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002491 unsigned int reg2 = mc->rreg;
Jon Smirl815ecf82008-07-29 10:22:24 -04002492 unsigned int shift = mc->shift;
2493 unsigned int rshift = mc->rshift;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002494 int max = mc->max;
Jon Smirl815ecf82008-07-29 10:22:24 -04002495 unsigned int mask = (1 << fls(max)) - 1;
2496 unsigned int invert = mc->invert;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002497 int err;
2498 bool type_2r = 0;
2499 unsigned int val2 = 0;
2500 unsigned int val, val_mask;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002501
2502 val = (ucontrol->value.integer.value[0] & mask);
2503 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002504 val = max - val;
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002505 val_mask = mask << shift;
2506 val = val << shift;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002507 if (snd_soc_volsw_is_stereo(mc)) {
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002508 val2 = (ucontrol->value.integer.value[1] & mask);
2509 if (invert)
Philipp Zabela7a4ac82008-01-10 14:37:42 +01002510 val2 = max - val2;
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002511 if (reg == reg2) {
2512 val_mask |= mask << rshift;
2513 val |= val2 << rshift;
2514 } else {
2515 val2 = val2 << shift;
2516 type_2r = 1;
2517 }
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002518 }
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002519 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
Mark Brown3ff3f642008-05-19 12:32:25 +02002520 if (err < 0)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002521 return err;
2522
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002523 if (type_2r)
2524 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2525
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002526 return err;
2527}
Peter Ujfalusi974815b2011-10-05 10:29:25 +03002528EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02002529
Mark Browne13ac2e2008-05-28 17:58:05 +01002530/**
2531 * snd_soc_info_volsw_s8 - signed mixer info callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2534 *
2535 * Callback to provide information about a signed mixer control.
2536 *
2537 * Returns 0 for success.
2538 */
2539int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2540 struct snd_ctl_elem_info *uinfo)
2541{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002542 struct soc_mixer_control *mc =
2543 (struct soc_mixer_control *)kcontrol->private_value;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002544 int platform_max;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002545 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002546
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002547 if (!mc->platform_max)
2548 mc->platform_max = mc->max;
2549 platform_max = mc->platform_max;
2550
Mark Browne13ac2e2008-05-28 17:58:05 +01002551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2552 uinfo->count = 2;
2553 uinfo->value.integer.min = 0;
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002554 uinfo->value.integer.max = platform_max - min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002555 return 0;
2556}
2557EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2558
2559/**
2560 * snd_soc_get_volsw_s8 - signed mixer get callback
2561 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002562 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002563 *
2564 * Callback to get the value of a signed mixer control.
2565 *
2566 * Returns 0 for success.
2567 */
2568int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2569 struct snd_ctl_elem_value *ucontrol)
2570{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002571 struct soc_mixer_control *mc =
2572 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002573 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002574 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002575 int min = mc->min;
Mark Browne13ac2e2008-05-28 17:58:05 +01002576 int val = snd_soc_read(codec, reg);
2577
2578 ucontrol->value.integer.value[0] =
2579 ((signed char)(val & 0xff))-min;
2580 ucontrol->value.integer.value[1] =
2581 ((signed char)((val >> 8) & 0xff))-min;
2582 return 0;
2583}
2584EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2585
2586/**
2587 * snd_soc_put_volsw_sgn - signed mixer put callback
2588 * @kcontrol: mixer control
Mark Brownac11a2b2009-01-01 12:18:17 +00002589 * @ucontrol: control element information
Mark Browne13ac2e2008-05-28 17:58:05 +01002590 *
2591 * Callback to set the value of a signed mixer control.
2592 *
2593 * Returns 0 for success.
2594 */
2595int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2596 struct snd_ctl_elem_value *ucontrol)
2597{
Jon Smirl4eaa9812008-07-29 11:42:26 +01002598 struct soc_mixer_control *mc =
2599 (struct soc_mixer_control *)kcontrol->private_value;
Mark Browne13ac2e2008-05-28 17:58:05 +01002600 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Jon Smirl815ecf82008-07-29 10:22:24 -04002601 unsigned int reg = mc->reg;
Jon Smirl4eaa9812008-07-29 11:42:26 +01002602 int min = mc->min;
Daniel Ribeiro46f58222009-06-07 02:49:11 -03002603 unsigned int val;
Mark Browne13ac2e2008-05-28 17:58:05 +01002604
2605 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2606 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2607
Eero Nurkkala6c508c62009-10-30 13:34:03 +02002608 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
Mark Browne13ac2e2008-05-28 17:58:05 +01002609}
2610EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2611
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002612/**
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002613 * snd_soc_limit_volume - Set new limit to an existing volume control.
2614 *
2615 * @codec: where to look for the control
2616 * @name: Name of the control
2617 * @max: new maximum limit
2618 *
2619 * Return 0 for success, else error.
2620 */
2621int snd_soc_limit_volume(struct snd_soc_codec *codec,
2622 const char *name, int max)
2623{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002624 struct snd_card *card = codec->card->snd_card;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002625 struct snd_kcontrol *kctl;
2626 struct soc_mixer_control *mc;
2627 int found = 0;
2628 int ret = -EINVAL;
2629
2630 /* Sanity check for name and max */
2631 if (unlikely(!name || max <= 0))
2632 return -EINVAL;
2633
2634 list_for_each_entry(kctl, &card->controls, list) {
2635 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2636 found = 1;
2637 break;
2638 }
2639 }
2640 if (found) {
2641 mc = (struct soc_mixer_control *)kctl->private_value;
2642 if (max <= mc->max) {
Peter Ujfalusid11bb4a2010-05-10 14:39:24 +03002643 mc->platform_max = max;
Peter Ujfalusi637d3842010-05-07 14:05:49 +03002644 ret = 0;
2645 }
2646 }
2647 return ret;
2648}
2649EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2650
2651/**
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002652 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2653 * mixer info callback
2654 * @kcontrol: mixer control
2655 * @uinfo: control element information
2656 *
2657 * Returns 0 for success.
2658 */
2659int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2660 struct snd_ctl_elem_info *uinfo)
2661{
2662 struct soc_mixer_control *mc =
2663 (struct soc_mixer_control *)kcontrol->private_value;
2664 int max = mc->max;
2665 int min = mc->min;
2666
2667 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2668 uinfo->count = 2;
2669 uinfo->value.integer.min = 0;
2670 uinfo->value.integer.max = max-min;
2671
2672 return 0;
2673}
2674EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2675
2676/**
2677 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2678 * mixer get callback
2679 * @kcontrol: mixer control
2680 * @uinfo: control element information
2681 *
2682 * Returns 0 for success.
2683 */
2684int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2685 struct snd_ctl_elem_value *ucontrol)
2686{
2687 struct soc_mixer_control *mc =
2688 (struct soc_mixer_control *)kcontrol->private_value;
2689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2690 unsigned int mask = (1<<mc->shift)-1;
2691 int min = mc->min;
2692 int val = snd_soc_read(codec, mc->reg) & mask;
2693 int valr = snd_soc_read(codec, mc->rreg) & mask;
2694
Stuart Longland20630c72010-06-18 12:56:10 +10002695 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2696 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002697 return 0;
2698}
2699EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2700
2701/**
2702 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2703 * mixer put callback
2704 * @kcontrol: mixer control
2705 * @uinfo: control element information
2706 *
2707 * Returns 0 for success.
2708 */
2709int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2710 struct snd_ctl_elem_value *ucontrol)
2711{
2712 struct soc_mixer_control *mc =
2713 (struct soc_mixer_control *)kcontrol->private_value;
2714 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2715 unsigned int mask = (1<<mc->shift)-1;
2716 int min = mc->min;
2717 int ret;
2718 unsigned int val, valr, oval, ovalr;
2719
2720 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2721 val &= mask;
2722 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2723 valr &= mask;
2724
2725 oval = snd_soc_read(codec, mc->reg) & mask;
2726 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2727
2728 ret = 0;
2729 if (oval != val) {
2730 ret = snd_soc_write(codec, mc->reg, val);
2731 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002732 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002733 }
2734 if (ovalr != valr) {
2735 ret = snd_soc_write(codec, mc->rreg, valr);
2736 if (ret < 0)
Mark Brownf1df5ae2010-06-15 15:14:31 +01002737 return ret;
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002738 }
2739
2740 return 0;
2741}
2742EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2743
Mark Brown71d08512011-10-10 18:31:26 +01002744int snd_soc_bytes_info(struct snd_kcontrol *kcontrol,
2745 struct snd_ctl_elem_info *uinfo)
2746{
2747 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2748 struct soc_bytes *params = (void *)kcontrol->private_value;
2749
2750 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
2751 uinfo->count = params->num_regs * codec->val_bytes;
2752
2753 return 0;
2754}
2755EXPORT_SYMBOL_GPL(snd_soc_bytes_info);
2756
2757int snd_soc_bytes_get(struct snd_kcontrol *kcontrol,
2758 struct snd_ctl_elem_value *ucontrol)
2759{
2760 struct soc_bytes *params = (void *)kcontrol->private_value;
2761 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2762 int ret;
2763
2764 if (codec->using_regmap)
2765 ret = regmap_raw_read(codec->control_data, params->base,
2766 ucontrol->value.bytes.data,
2767 params->num_regs * codec->val_bytes);
2768 else
2769 ret = -EINVAL;
2770
Mark Brownf831b052012-02-17 16:20:33 -08002771 /* Hide any masked bytes to ensure consistent data reporting */
2772 if (ret == 0 && params->mask) {
2773 switch (codec->val_bytes) {
2774 case 1:
2775 ucontrol->value.bytes.data[0] &= ~params->mask;
2776 break;
2777 case 2:
2778 ((u16 *)(&ucontrol->value.bytes.data))[0]
2779 &= ~params->mask;
2780 break;
2781 case 4:
2782 ((u32 *)(&ucontrol->value.bytes.data))[0]
2783 &= ~params->mask;
2784 break;
2785 default:
2786 return -EINVAL;
2787 }
2788 }
2789
Mark Brown71d08512011-10-10 18:31:26 +01002790 return ret;
2791}
2792EXPORT_SYMBOL_GPL(snd_soc_bytes_get);
2793
2794int snd_soc_bytes_put(struct snd_kcontrol *kcontrol,
2795 struct snd_ctl_elem_value *ucontrol)
2796{
2797 struct soc_bytes *params = (void *)kcontrol->private_value;
2798 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownf831b052012-02-17 16:20:33 -08002799 int ret, len;
2800 unsigned int val;
2801 void *data;
Mark Brown71d08512011-10-10 18:31:26 +01002802
Mark Brownf831b052012-02-17 16:20:33 -08002803 if (!codec->using_regmap)
2804 return -EINVAL;
2805
2806 data = ucontrol->value.bytes.data;
2807 len = params->num_regs * codec->val_bytes;
2808
2809 /*
2810 * If we've got a mask then we need to preserve the register
2811 * bits. We shouldn't modify the incoming data so take a
2812 * copy.
2813 */
2814 if (params->mask) {
2815 ret = regmap_read(codec->control_data, params->base, &val);
2816 if (ret != 0)
2817 return ret;
2818
2819 val &= params->mask;
2820
2821 data = kmemdup(data, len, GFP_KERNEL);
2822 if (!data)
2823 return -ENOMEM;
2824
2825 switch (codec->val_bytes) {
2826 case 1:
2827 ((u8 *)data)[0] &= ~params->mask;
2828 ((u8 *)data)[0] |= val;
2829 break;
2830 case 2:
2831 ((u16 *)data)[0] &= cpu_to_be16(~params->mask);
2832 ((u16 *)data)[0] |= cpu_to_be16(val);
2833 break;
2834 case 4:
2835 ((u32 *)data)[0] &= cpu_to_be32(~params->mask);
2836 ((u32 *)data)[0] |= cpu_to_be32(val);
2837 break;
2838 default:
2839 return -EINVAL;
2840 }
2841 }
2842
2843 ret = regmap_raw_write(codec->control_data, params->base,
2844 data, len);
2845
2846 if (params->mask)
2847 kfree(data);
Mark Brown71d08512011-10-10 18:31:26 +01002848
2849 return ret;
2850}
2851EXPORT_SYMBOL_GPL(snd_soc_bytes_put);
2852
apatard@mandriva.comb6f4bb32010-05-15 17:30:01 +02002853/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002854 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2855 * @dai: DAI
2856 * @clk_id: DAI specific clock ID
2857 * @freq: new clock frequency in Hz
2858 * @dir: new clock direction - input/output.
2859 *
2860 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2861 */
2862int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2863 unsigned int freq, int dir)
2864{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002865 if (dai->driver && dai->driver->ops->set_sysclk)
2866 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002867 else if (dai->codec && dai->codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002868 return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0,
Mark Brownec4ee522011-03-07 20:58:11 +00002869 freq, dir);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002870 else
2871 return -EINVAL;
2872}
2873EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2874
2875/**
Mark Brownec4ee522011-03-07 20:58:11 +00002876 * snd_soc_codec_set_sysclk - configure CODEC system or master clock.
2877 * @codec: CODEC
2878 * @clk_id: DAI specific clock ID
Mark Brownda1c6ea2011-08-24 20:09:01 +01002879 * @source: Source for the clock
Mark Brownec4ee522011-03-07 20:58:11 +00002880 * @freq: new clock frequency in Hz
2881 * @dir: new clock direction - input/output.
2882 *
2883 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
2884 */
2885int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Mark Brownda1c6ea2011-08-24 20:09:01 +01002886 int source, unsigned int freq, int dir)
Mark Brownec4ee522011-03-07 20:58:11 +00002887{
2888 if (codec->driver->set_sysclk)
Mark Brownda1c6ea2011-08-24 20:09:01 +01002889 return codec->driver->set_sysclk(codec, clk_id, source,
2890 freq, dir);
Mark Brownec4ee522011-03-07 20:58:11 +00002891 else
2892 return -EINVAL;
2893}
2894EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk);
2895
2896/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002897 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2898 * @dai: DAI
Mark Brownac11a2b2009-01-01 12:18:17 +00002899 * @div_id: DAI specific clock divider ID
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002900 * @div: new clock divisor.
2901 *
2902 * Configures the clock dividers. This is used to derive the best DAI bit and
2903 * frame clocks from the system or master clock. It's best to set the DAI bit
2904 * and frame clocks as low as possible to save system power.
2905 */
2906int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2907 int div_id, int div)
2908{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002909 if (dai->driver && dai->driver->ops->set_clkdiv)
2910 return dai->driver->ops->set_clkdiv(dai, div_id, div);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002911 else
2912 return -EINVAL;
2913}
2914EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2915
2916/**
2917 * snd_soc_dai_set_pll - configure DAI PLL.
2918 * @dai: DAI
2919 * @pll_id: DAI specific PLL ID
Mark Brown85488032009-09-05 18:52:16 +01002920 * @source: DAI specific source for the PLL
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002921 * @freq_in: PLL input clock frequency in Hz
2922 * @freq_out: requested PLL output clock frequency in Hz
2923 *
2924 * Configures and enables PLL to generate output clock based on input clock.
2925 */
Mark Brown85488032009-09-05 18:52:16 +01002926int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2927 unsigned int freq_in, unsigned int freq_out)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002928{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002929 if (dai->driver && dai->driver->ops->set_pll)
2930 return dai->driver->ops->set_pll(dai, pll_id, source,
Mark Brown85488032009-09-05 18:52:16 +01002931 freq_in, freq_out);
Mark Brownec4ee522011-03-07 20:58:11 +00002932 else if (dai->codec && dai->codec->driver->set_pll)
2933 return dai->codec->driver->set_pll(dai->codec, pll_id, source,
2934 freq_in, freq_out);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002935 else
2936 return -EINVAL;
2937}
2938EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2939
Mark Brownec4ee522011-03-07 20:58:11 +00002940/*
2941 * snd_soc_codec_set_pll - configure codec PLL.
2942 * @codec: CODEC
2943 * @pll_id: DAI specific PLL ID
2944 * @source: DAI specific source for the PLL
2945 * @freq_in: PLL input clock frequency in Hz
2946 * @freq_out: requested PLL output clock frequency in Hz
2947 *
2948 * Configures and enables PLL to generate output clock based on input clock.
2949 */
2950int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source,
2951 unsigned int freq_in, unsigned int freq_out)
2952{
2953 if (codec->driver->set_pll)
2954 return codec->driver->set_pll(codec, pll_id, source,
2955 freq_in, freq_out);
2956 else
2957 return -EINVAL;
2958}
2959EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll);
2960
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002961/**
2962 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2963 * @dai: DAI
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002964 * @fmt: SND_SOC_DAIFMT_ format value.
2965 *
2966 * Configures the DAI hardware format and clocking.
2967 */
2968int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2969{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002970 if (dai->driver && dai->driver->ops->set_fmt)
2971 return dai->driver->ops->set_fmt(dai, fmt);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002972 else
2973 return -EINVAL;
2974}
2975EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2976
2977/**
2978 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2979 * @dai: DAI
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002980 * @tx_mask: bitmask representing active TX slots.
2981 * @rx_mask: bitmask representing active RX slots.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002982 * @slots: Number of slots in use.
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002983 * @slot_width: Width in bits for each slot.
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002984 *
2985 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2986 * specific.
2987 */
2988int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002989 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002990{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002991 if (dai->driver && dai->driver->ops->set_tdm_slot)
2992 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
Daniel Ribeiroa5479e32009-06-15 21:44:31 -03002993 slots, slot_width);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01002994 else
2995 return -EINVAL;
2996}
2997EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2998
2999/**
Barry Song472df3c2009-09-12 01:16:29 +08003000 * snd_soc_dai_set_channel_map - configure DAI audio channel map
3001 * @dai: DAI
3002 * @tx_num: how many TX channels
3003 * @tx_slot: pointer to an array which imply the TX slot number channel
3004 * 0~num-1 uses
3005 * @rx_num: how many RX channels
3006 * @rx_slot: pointer to an array which imply the RX slot number channel
3007 * 0~num-1 uses
3008 *
3009 * configure the relationship between channel number and TDM slot number.
3010 */
3011int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
3012 unsigned int tx_num, unsigned int *tx_slot,
3013 unsigned int rx_num, unsigned int *rx_slot)
3014{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003015 if (dai->driver && dai->driver->ops->set_channel_map)
3016 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
Barry Song472df3c2009-09-12 01:16:29 +08003017 rx_num, rx_slot);
3018 else
3019 return -EINVAL;
3020}
3021EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
3022
3023/**
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003024 * snd_soc_dai_set_tristate - configure DAI system or master clock.
3025 * @dai: DAI
3026 * @tristate: tristate enable
3027 *
3028 * Tristates the DAI so that others can use it.
3029 */
3030int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
3031{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003032 if (dai->driver && dai->driver->ops->set_tristate)
3033 return dai->driver->ops->set_tristate(dai, tristate);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003034 else
3035 return -EINVAL;
3036}
3037EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
3038
3039/**
3040 * snd_soc_dai_digital_mute - configure DAI system or master clock.
3041 * @dai: DAI
3042 * @mute: mute enable
3043 *
3044 * Mutes the DAI DAC.
3045 */
3046int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
3047{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003048 if (dai->driver && dai->driver->ops->digital_mute)
3049 return dai->driver->ops->digital_mute(dai, mute);
Liam Girdwood8c6529d2008-07-08 13:19:13 +01003050 else
3051 return -EINVAL;
3052}
3053EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
3054
Mark Brownc5af3a22008-11-28 13:29:45 +00003055/**
3056 * snd_soc_register_card - Register a card with the ASoC core
3057 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003058 * @card: Card to register
Mark Brownc5af3a22008-11-28 13:29:45 +00003059 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003060 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303061int snd_soc_register_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003062{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003063 int i;
3064
Mark Brownc5af3a22008-11-28 13:29:45 +00003065 if (!card->name || !card->dev)
3066 return -EINVAL;
3067
Stephen Warren5a504962011-12-21 10:40:59 -07003068 for (i = 0; i < card->num_links; i++) {
3069 struct snd_soc_dai_link *link = &card->dai_link[i];
3070
3071 /*
3072 * Codec must be specified by 1 of name or OF node,
3073 * not both or neither.
3074 */
3075 if (!!link->codec_name == !!link->codec_of_node) {
3076 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003077 "Neither/both codec name/of_node are set for %s\n",
3078 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003079 return -EINVAL;
3080 }
3081
3082 /*
3083 * Platform may be specified by either name or OF node, but
3084 * can be left unspecified, and a dummy platform will be used.
3085 */
3086 if (link->platform_name && link->platform_of_node) {
3087 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003088 "Both platform name/of_node are set for %s\n", link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003089 return -EINVAL;
3090 }
3091
3092 /*
3093 * CPU DAI must be specified by 1 of name or OF node,
3094 * not both or neither.
3095 */
3096 if (!!link->cpu_dai_name == !!link->cpu_dai_of_node) {
3097 dev_err(card->dev,
Liam Girdwood3b09bb82012-01-09 12:09:29 +00003098 "Neither/both cpu_dai name/of_node are set for %s\n",
3099 link->name);
Stephen Warren5a504962011-12-21 10:40:59 -07003100 return -EINVAL;
3101 }
3102 }
3103
Mark Browned77cc12011-05-03 18:25:34 +01003104 dev_set_drvdata(card->dev, card);
3105
Stephen Warren111c6412011-01-28 14:26:35 -07003106 snd_soc_initialize_card_lists(card);
3107
Vinod Koul150dd2f2011-01-13 22:48:32 +05303108 soc_init_card_debugfs(card);
3109
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003110 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) *
3111 (card->num_links + card->num_aux_devs),
3112 GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003113 if (card->rtd == NULL)
3114 return -ENOMEM;
Jarkko Nikula2eea3922010-11-25 17:47:38 +02003115 card->rtd_aux = &card->rtd[card->num_links];
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003116
3117 for (i = 0; i < card->num_links; i++)
3118 card->rtd[i].dai_link = &card->dai_link[i];
3119
Mark Brownc5af3a22008-11-28 13:29:45 +00003120 INIT_LIST_HEAD(&card->list);
Mark Browndb432b42011-10-03 21:06:40 +01003121 INIT_LIST_HEAD(&card->dapm_dirty);
Mark Brownc5af3a22008-11-28 13:29:45 +00003122 card->instantiated = 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003123 mutex_init(&card->mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003124
3125 mutex_lock(&client_mutex);
3126 list_add(&card->list, &card_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003127 snd_soc_instantiate_cards();
Mark Brownc5af3a22008-11-28 13:29:45 +00003128 mutex_unlock(&client_mutex);
3129
3130 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
3131
3132 return 0;
3133}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303134EXPORT_SYMBOL_GPL(snd_soc_register_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003135
3136/**
3137 * snd_soc_unregister_card - Unregister a card with the ASoC core
3138 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003139 * @card: Card to unregister
Mark Brownc5af3a22008-11-28 13:29:45 +00003140 *
Mark Brownc5af3a22008-11-28 13:29:45 +00003141 */
Vinod Koul70a7ca32011-01-14 19:22:48 +05303142int snd_soc_unregister_card(struct snd_soc_card *card)
Mark Brownc5af3a22008-11-28 13:29:45 +00003143{
Vinod Koulb0e26482011-01-13 22:48:02 +05303144 if (card->instantiated)
3145 soc_cleanup_card_resources(card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003146 mutex_lock(&client_mutex);
3147 list_del(&card->list);
3148 mutex_unlock(&client_mutex);
Mark Brownc5af3a22008-11-28 13:29:45 +00003149 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
3150
3151 return 0;
3152}
Vinod Koul70a7ca32011-01-14 19:22:48 +05303153EXPORT_SYMBOL_GPL(snd_soc_unregister_card);
Mark Brownc5af3a22008-11-28 13:29:45 +00003154
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003155/*
3156 * Simplify DAI link configuration by removing ".-1" from device names
3157 * and sanitizing names.
3158 */
Dimitris Papastamos0b9a2142010-12-06 15:49:20 +00003159static char *fmt_single_name(struct device *dev, int *id)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003160{
3161 char *found, name[NAME_SIZE];
3162 int id1, id2;
3163
3164 if (dev_name(dev) == NULL)
3165 return NULL;
3166
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003167 strlcpy(name, dev_name(dev), NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003168
3169 /* are we a "%s.%d" name (platform and SPI components) */
3170 found = strstr(name, dev->driver->name);
3171 if (found) {
3172 /* get ID */
3173 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
3174
3175 /* discard ID from name if ID == -1 */
3176 if (*id == -1)
3177 found[strlen(dev->driver->name)] = '\0';
3178 }
3179
3180 } else {
3181 /* I2C component devices are named "bus-addr" */
3182 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
3183 char tmp[NAME_SIZE];
3184
3185 /* create unique ID number from I2C addr and bus */
Jarkko Nikula05899442010-10-19 11:10:45 +03003186 *id = ((id1 & 0xffff) << 16) + id2;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003187
3188 /* sanitize component name for DAI link creation */
3189 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
Dimitris Papastamos58818a72010-12-06 15:42:17 +00003190 strlcpy(name, tmp, NAME_SIZE);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003191 } else
3192 *id = 0;
3193 }
3194
3195 return kstrdup(name, GFP_KERNEL);
3196}
3197
3198/*
3199 * Simplify DAI link naming for single devices with multiple DAIs by removing
3200 * any ".-1" and using the DAI name (instead of device name).
3201 */
3202static inline char *fmt_multiple_name(struct device *dev,
3203 struct snd_soc_dai_driver *dai_drv)
3204{
3205 if (dai_drv->name == NULL) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003206 pr_err("asoc: error - multiple DAI %s registered with no name\n",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003207 dev_name(dev));
3208 return NULL;
3209 }
3210
3211 return kstrdup(dai_drv->name, GFP_KERNEL);
3212}
3213
Mark Brown91151712008-11-30 23:31:24 +00003214/**
3215 * snd_soc_register_dai - Register a DAI with the ASoC core
3216 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003217 * @dai: DAI to register
Mark Brown91151712008-11-30 23:31:24 +00003218 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003219int snd_soc_register_dai(struct device *dev,
3220 struct snd_soc_dai_driver *dai_drv)
Mark Brown91151712008-11-30 23:31:24 +00003221{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003222 struct snd_soc_dai *dai;
Mark Brown91151712008-11-30 23:31:24 +00003223
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003224 dev_dbg(dev, "dai register %s\n", dev_name(dev));
Mark Brown91151712008-11-30 23:31:24 +00003225
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003226 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
3227 if (dai == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003228 return -ENOMEM;
Eric Miao6335d052009-03-03 09:41:00 +08003229
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003230 /* create DAI component name */
3231 dai->name = fmt_single_name(dev, &dai->id);
3232 if (dai->name == NULL) {
3233 kfree(dai);
3234 return -ENOMEM;
3235 }
3236
3237 dai->dev = dev;
3238 dai->driver = dai_drv;
3239 if (!dai->driver->ops)
3240 dai->driver->ops = &null_dai_ops;
Mark Brown91151712008-11-30 23:31:24 +00003241
3242 mutex_lock(&client_mutex);
3243 list_add(&dai->list, &dai_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003244 snd_soc_instantiate_cards();
Mark Brown91151712008-11-30 23:31:24 +00003245 mutex_unlock(&client_mutex);
3246
3247 pr_debug("Registered DAI '%s'\n", dai->name);
3248
3249 return 0;
3250}
3251EXPORT_SYMBOL_GPL(snd_soc_register_dai);
3252
3253/**
3254 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
3255 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003256 * @dai: DAI to unregister
Mark Brown91151712008-11-30 23:31:24 +00003257 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003258void snd_soc_unregister_dai(struct device *dev)
Mark Brown91151712008-11-30 23:31:24 +00003259{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003260 struct snd_soc_dai *dai;
3261
3262 list_for_each_entry(dai, &dai_list, list) {
3263 if (dev == dai->dev)
3264 goto found;
3265 }
3266 return;
3267
3268found:
Mark Brown91151712008-11-30 23:31:24 +00003269 mutex_lock(&client_mutex);
3270 list_del(&dai->list);
3271 mutex_unlock(&client_mutex);
3272
3273 pr_debug("Unregistered DAI '%s'\n", dai->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003274 kfree(dai->name);
3275 kfree(dai);
Mark Brown91151712008-11-30 23:31:24 +00003276}
3277EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
3278
3279/**
3280 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
3281 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003282 * @dai: Array of DAIs to register
3283 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003284 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003285int snd_soc_register_dais(struct device *dev,
3286 struct snd_soc_dai_driver *dai_drv, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003287{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003288 struct snd_soc_dai *dai;
3289 int i, ret = 0;
3290
Liam Girdwood720ffa42010-08-18 00:30:30 +01003291 dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count);
Mark Brown91151712008-11-30 23:31:24 +00003292
3293 for (i = 0; i < count; i++) {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003294
3295 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
Axel Linc46e0072010-11-03 15:04:45 +08003296 if (dai == NULL) {
3297 ret = -ENOMEM;
3298 goto err;
3299 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003300
3301 /* create DAI component name */
3302 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
3303 if (dai->name == NULL) {
3304 kfree(dai);
3305 ret = -EINVAL;
Mark Brown91151712008-11-30 23:31:24 +00003306 goto err;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003307 }
3308
3309 dai->dev = dev;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003310 dai->driver = &dai_drv[i];
Mark Brown0f9141c2010-10-12 15:43:21 +01003311 if (dai->driver->id)
3312 dai->id = dai->driver->id;
3313 else
3314 dai->id = i;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003315 if (!dai->driver->ops)
3316 dai->driver->ops = &null_dai_ops;
3317
3318 mutex_lock(&client_mutex);
3319 list_add(&dai->list, &dai_list);
3320 mutex_unlock(&client_mutex);
3321
3322 pr_debug("Registered DAI '%s'\n", dai->name);
Mark Brown91151712008-11-30 23:31:24 +00003323 }
3324
Axel Lin1dcb4f32010-12-06 16:48:03 +08003325 mutex_lock(&client_mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003326 snd_soc_instantiate_cards();
Axel Lin1dcb4f32010-12-06 16:48:03 +08003327 mutex_unlock(&client_mutex);
Mark Brown91151712008-11-30 23:31:24 +00003328 return 0;
3329
3330err:
3331 for (i--; i >= 0; i--)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003332 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003333
3334 return ret;
3335}
3336EXPORT_SYMBOL_GPL(snd_soc_register_dais);
3337
3338/**
3339 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
3340 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003341 * @dai: Array of DAIs to unregister
3342 * @count: Number of DAIs
Mark Brown91151712008-11-30 23:31:24 +00003343 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003344void snd_soc_unregister_dais(struct device *dev, size_t count)
Mark Brown91151712008-11-30 23:31:24 +00003345{
3346 int i;
3347
3348 for (i = 0; i < count; i++)
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003349 snd_soc_unregister_dai(dev);
Mark Brown91151712008-11-30 23:31:24 +00003350}
3351EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
3352
Mark Brown12a48a8c2008-12-03 19:40:30 +00003353/**
3354 * snd_soc_register_platform - Register a platform with the ASoC core
3355 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003356 * @platform: platform to register
Mark Brown12a48a8c2008-12-03 19:40:30 +00003357 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003358int snd_soc_register_platform(struct device *dev,
3359 struct snd_soc_platform_driver *platform_drv)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003360{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003361 struct snd_soc_platform *platform;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003362
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003363 dev_dbg(dev, "platform register %s\n", dev_name(dev));
3364
3365 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
3366 if (platform == NULL)
Lu Guanquna7393622011-04-20 16:00:51 +08003367 return -ENOMEM;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003368
3369 /* create platform component name */
3370 platform->name = fmt_single_name(dev, &platform->id);
3371 if (platform->name == NULL) {
3372 kfree(platform);
3373 return -ENOMEM;
3374 }
3375
3376 platform->dev = dev;
3377 platform->driver = platform_drv;
Liam Girdwoodb7950642011-07-04 22:10:52 +01003378 platform->dapm.dev = dev;
3379 platform->dapm.platform = platform;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003380 platform->dapm.stream_event = platform_drv->stream_event;
Mark Brown12a48a8c2008-12-03 19:40:30 +00003381
3382 mutex_lock(&client_mutex);
3383 list_add(&platform->list, &platform_list);
Mark Brown435c5e22008-12-04 15:32:53 +00003384 snd_soc_instantiate_cards();
Mark Brown12a48a8c2008-12-03 19:40:30 +00003385 mutex_unlock(&client_mutex);
3386
3387 pr_debug("Registered platform '%s'\n", platform->name);
3388
3389 return 0;
3390}
3391EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3392
3393/**
3394 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3395 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003396 * @platform: platform to unregister
Mark Brown12a48a8c2008-12-03 19:40:30 +00003397 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003398void snd_soc_unregister_platform(struct device *dev)
Mark Brown12a48a8c2008-12-03 19:40:30 +00003399{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003400 struct snd_soc_platform *platform;
3401
3402 list_for_each_entry(platform, &platform_list, list) {
3403 if (dev == platform->dev)
3404 goto found;
3405 }
3406 return;
3407
3408found:
Mark Brown12a48a8c2008-12-03 19:40:30 +00003409 mutex_lock(&client_mutex);
3410 list_del(&platform->list);
3411 mutex_unlock(&client_mutex);
3412
3413 pr_debug("Unregistered platform '%s'\n", platform->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003414 kfree(platform->name);
3415 kfree(platform);
Mark Brown12a48a8c2008-12-03 19:40:30 +00003416}
3417EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3418
Mark Brown151ab222009-05-09 16:22:58 +01003419static u64 codec_format_map[] = {
3420 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3421 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3422 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3423 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3424 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3425 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3426 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3427 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3428 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3429 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3430 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3431 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3432 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3433 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3434 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3435 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3436};
3437
3438/* Fix up the DAI formats for endianness: codecs don't actually see
3439 * the endianness of the data but we're using the CPU format
3440 * definitions which do need to include endianness so we ensure that
3441 * codec DAIs always have both big and little endian variants set.
3442 */
3443static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3444{
3445 int i;
3446
3447 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3448 if (stream->formats & codec_format_map[i])
3449 stream->formats |= codec_format_map[i];
3450}
3451
Mark Brown0d0cf002008-12-10 14:32:45 +00003452/**
3453 * snd_soc_register_codec - Register a codec with the ASoC core
3454 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003455 * @codec: codec to register
Mark Brown0d0cf002008-12-10 14:32:45 +00003456 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003457int snd_soc_register_codec(struct device *dev,
Mark Brown001ae4c2010-12-02 16:21:08 +00003458 const struct snd_soc_codec_driver *codec_drv,
3459 struct snd_soc_dai_driver *dai_drv,
3460 int num_dai)
Mark Brown0d0cf002008-12-10 14:32:45 +00003461{
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003462 size_t reg_size;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003463 struct snd_soc_codec *codec;
3464 int ret, i;
Mark Brown151ab222009-05-09 16:22:58 +01003465
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003466 dev_dbg(dev, "codec register %s\n", dev_name(dev));
Mark Brown0d0cf002008-12-10 14:32:45 +00003467
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003468 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3469 if (codec == NULL)
3470 return -ENOMEM;
Mark Brown0d0cf002008-12-10 14:32:45 +00003471
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003472 /* create CODEC component name */
3473 codec->name = fmt_single_name(dev, &codec->id);
3474 if (codec->name == NULL) {
3475 kfree(codec);
3476 return -ENOMEM;
Mark Brown151ab222009-05-09 16:22:58 +01003477 }
3478
Dimitris Papastamos23bbce32010-12-02 14:53:01 +00003479 if (codec_drv->compress_type)
3480 codec->compress_type = codec_drv->compress_type;
3481 else
3482 codec->compress_type = SND_SOC_FLAT_COMPRESSION;
3483
Mark Brownc3acec22010-12-02 16:15:29 +00003484 codec->write = codec_drv->write;
3485 codec->read = codec_drv->read;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003486 codec->volatile_register = codec_drv->volatile_register;
3487 codec->readable_register = codec_drv->readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003488 codec->writable_register = codec_drv->writable_register;
Mark Brown5124e692012-02-08 13:20:50 +00003489 codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time;
Liam Girdwoodce6120c2010-11-05 15:53:46 +02003490 codec->dapm.bias_level = SND_SOC_BIAS_OFF;
3491 codec->dapm.dev = dev;
3492 codec->dapm.codec = codec;
Mark Brown474b62d2011-01-18 16:14:44 +00003493 codec->dapm.seq_notifier = codec_drv->seq_notifier;
Liam Girdwood64a648c2011-07-25 11:15:15 +01003494 codec->dapm.stream_event = codec_drv->stream_event;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003495 codec->dev = dev;
3496 codec->driver = codec_drv;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003497 codec->num_dai = num_dai;
3498 mutex_init(&codec->mutex);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003499
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003500 /* allocate CODEC register cache */
3501 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003502 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
Dimitris Papastamosaea170a2011-01-12 10:38:58 +00003503 codec->reg_size = reg_size;
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003504 /* it is necessary to make a copy of the default register cache
3505 * because in the case of using a compression type that requires
3506 * the default register cache to be marked as __devinitconst the
3507 * kernel might have freed the array by the time we initialize
3508 * the cache.
3509 */
Dimitris Papastamos2aa86322011-01-10 10:10:56 +00003510 if (codec_drv->reg_cache_default) {
3511 codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default,
3512 reg_size, GFP_KERNEL);
3513 if (!codec->reg_def_copy) {
3514 ret = -ENOMEM;
3515 goto fail;
3516 }
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003517 }
3518 }
3519
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003520 if (codec_drv->reg_access_size && codec_drv->reg_access_default) {
3521 if (!codec->volatile_register)
3522 codec->volatile_register = snd_soc_default_volatile_register;
3523 if (!codec->readable_register)
3524 codec->readable_register = snd_soc_default_readable_register;
Dimitris Papastamos80204542011-03-24 13:45:17 +00003525 if (!codec->writable_register)
3526 codec->writable_register = snd_soc_default_writable_register;
Dimitris Papastamos1500b7b2011-01-13 12:20:38 +00003527 }
3528
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003529 for (i = 0; i < num_dai; i++) {
3530 fixup_codec_formats(&dai_drv[i].playback);
3531 fixup_codec_formats(&dai_drv[i].capture);
3532 }
3533
Mark Brown26b01cc2010-08-18 20:20:55 +01003534 /* register any DAIs */
3535 if (num_dai) {
3536 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3537 if (ret < 0)
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003538 goto fail;
Mark Brown26b01cc2010-08-18 20:20:55 +01003539 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003540
Mark Brown0d0cf002008-12-10 14:32:45 +00003541 mutex_lock(&client_mutex);
3542 list_add(&codec->list, &codec_list);
3543 snd_soc_instantiate_cards();
3544 mutex_unlock(&client_mutex);
3545
3546 pr_debug("Registered codec '%s'\n", codec->name);
Mark Brown0d0cf002008-12-10 14:32:45 +00003547 return 0;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003548
Dimitris Papastamosfdf0f542010-12-02 16:11:06 +00003549fail:
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003550 kfree(codec->reg_def_copy);
3551 codec->reg_def_copy = NULL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003552 kfree(codec->name);
3553 kfree(codec);
3554 return ret;
Mark Brown0d0cf002008-12-10 14:32:45 +00003555}
3556EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3557
3558/**
3559 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3560 *
Mark Brownac11a2b2009-01-01 12:18:17 +00003561 * @codec: codec to unregister
Mark Brown0d0cf002008-12-10 14:32:45 +00003562 */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003563void snd_soc_unregister_codec(struct device *dev)
Mark Brown0d0cf002008-12-10 14:32:45 +00003564{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003565 struct snd_soc_codec *codec;
3566 int i;
3567
3568 list_for_each_entry(codec, &codec_list, list) {
3569 if (dev == codec->dev)
3570 goto found;
3571 }
3572 return;
3573
3574found:
Mark Brown26b01cc2010-08-18 20:20:55 +01003575 if (codec->num_dai)
3576 for (i = 0; i < codec->num_dai; i++)
3577 snd_soc_unregister_dai(dev);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003578
Mark Brown0d0cf002008-12-10 14:32:45 +00003579 mutex_lock(&client_mutex);
3580 list_del(&codec->list);
3581 mutex_unlock(&client_mutex);
3582
3583 pr_debug("Unregistered codec '%s'\n", codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003584
Dimitris Papastamos7a30a3d2010-11-11 10:04:57 +00003585 snd_soc_cache_exit(codec);
Dimitris Papastamos3335ddc2010-12-02 16:11:05 +00003586 kfree(codec->reg_def_copy);
Dimitris Papastamos1aafcd42010-10-21 13:19:45 +01003587 kfree(codec->name);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00003588 kfree(codec);
Mark Brown0d0cf002008-12-10 14:32:45 +00003589}
3590EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3591
Stephen Warrenbec4fa02011-12-12 15:55:34 -07003592/* Retrieve a card's name from device tree */
3593int snd_soc_of_parse_card_name(struct snd_soc_card *card,
3594 const char *propname)
3595{
3596 struct device_node *np = card->dev->of_node;
3597 int ret;
3598
3599 ret = of_property_read_string_index(np, propname, 0, &card->name);
3600 /*
3601 * EINVAL means the property does not exist. This is fine providing
3602 * card->name was previously set, which is checked later in
3603 * snd_soc_register_card.
3604 */
3605 if (ret < 0 && ret != -EINVAL) {
3606 dev_err(card->dev,
3607 "Property '%s' could not be read: %d\n",
3608 propname, ret);
3609 return ret;
3610 }
3611
3612 return 0;
3613}
3614EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name);
3615
Stephen Warrena4a54dd2011-12-12 15:55:35 -07003616int snd_soc_of_parse_audio_routing(struct snd_soc_card *card,
3617 const char *propname)
3618{
3619 struct device_node *np = card->dev->of_node;
3620 int num_routes;
3621 struct snd_soc_dapm_route *routes;
3622 int i, ret;
3623
3624 num_routes = of_property_count_strings(np, propname);
3625 if (num_routes & 1) {
3626 dev_err(card->dev,
3627 "Property '%s's length is not even\n",
3628 propname);
3629 return -EINVAL;
3630 }
3631 num_routes /= 2;
3632 if (!num_routes) {
3633 dev_err(card->dev,
3634 "Property '%s's length is zero\n",
3635 propname);
3636 return -EINVAL;
3637 }
3638
3639 routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes),
3640 GFP_KERNEL);
3641 if (!routes) {
3642 dev_err(card->dev,
3643 "Could not allocate DAPM route table\n");
3644 return -EINVAL;
3645 }
3646
3647 for (i = 0; i < num_routes; i++) {
3648 ret = of_property_read_string_index(np, propname,
3649 2 * i, &routes[i].sink);
3650 if (ret) {
3651 dev_err(card->dev,
3652 "Property '%s' index %d could not be read: %d\n",
3653 propname, 2 * i, ret);
3654 return -EINVAL;
3655 }
3656 ret = of_property_read_string_index(np, propname,
3657 (2 * i) + 1, &routes[i].source);
3658 if (ret) {
3659 dev_err(card->dev,
3660 "Property '%s' index %d could not be read: %d\n",
3661 propname, (2 * i) + 1, ret);
3662 return -EINVAL;
3663 }
3664 }
3665
3666 card->num_dapm_routes = num_routes;
3667 card->dapm_routes = routes;
3668
3669 return 0;
3670}
3671EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing);
3672
Takashi Iwaic9b3a402008-12-10 07:47:22 +01003673static int __init snd_soc_init(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003674{
Mark Brown384c89e2008-12-03 17:34:03 +00003675#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003676 snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL);
3677 if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) {
Fabio Estevam0837fc62012-02-17 19:40:38 -02003678 pr_warn("ASoC: Failed to create debugfs directory\n");
Mark Brown8a9dab12011-01-10 22:25:21 +00003679 snd_soc_debugfs_root = NULL;
Mark Brown384c89e2008-12-03 17:34:03 +00003680 }
Mark Brownc3c5a192010-09-15 18:15:14 +01003681
Mark Brown8a9dab12011-01-10 22:25:21 +00003682 if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL,
Mark Brownc3c5a192010-09-15 18:15:14 +01003683 &codec_list_fops))
3684 pr_warn("ASoC: Failed to create CODEC list debugfs file\n");
3685
Mark Brown8a9dab12011-01-10 22:25:21 +00003686 if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL,
Mark Brownf3208782010-09-15 18:19:07 +01003687 &dai_list_fops))
3688 pr_warn("ASoC: Failed to create DAI list debugfs file\n");
Mark Brown19c7ac22010-09-15 18:22:40 +01003689
Mark Brown8a9dab12011-01-10 22:25:21 +00003690 if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL,
Mark Brown19c7ac22010-09-15 18:22:40 +01003691 &platform_list_fops))
3692 pr_warn("ASoC: Failed to create platform list debugfs file\n");
Mark Brown384c89e2008-12-03 17:34:03 +00003693#endif
3694
Mark Brownfb257892011-04-28 10:57:54 +01003695 snd_soc_util_init();
3696
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003697 return platform_driver_register(&soc_driver);
3698}
Mark Brown4abe8e12010-10-12 17:41:03 +01003699module_init(snd_soc_init);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003700
Mark Brown7d8c16a2008-11-30 22:11:24 +00003701static void __exit snd_soc_exit(void)
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003702{
Mark Brownfb257892011-04-28 10:57:54 +01003703 snd_soc_util_exit();
3704
Mark Brown384c89e2008-12-03 17:34:03 +00003705#ifdef CONFIG_DEBUG_FS
Mark Brown8a9dab12011-01-10 22:25:21 +00003706 debugfs_remove_recursive(snd_soc_debugfs_root);
Mark Brown384c89e2008-12-03 17:34:03 +00003707#endif
Mark Brown3ff3f642008-05-19 12:32:25 +02003708 platform_driver_unregister(&soc_driver);
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003709}
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003710module_exit(snd_soc_exit);
3711
3712/* Module information */
Liam Girdwoodd3311242008-10-12 13:17:36 +01003713MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
Frank Mandarinodb2a4162006-10-06 18:31:09 +02003714MODULE_DESCRIPTION("ALSA SoC Core");
3715MODULE_LICENSE("GPL");
Kay Sievers8b45a202008-04-14 13:33:36 +02003716MODULE_ALIAS("platform:soc-audio");