Commit | Line | Data |
---|---|---|
db2a4165 FM |
1 | /* |
2 | * soc-core.c -- ALSA SoC Audio Layer | |
3 | * | |
4 | * Copyright 2005 Wolfson Microelectronics PLC. | |
0664d888 | 5 | * Copyright 2005 Openedhand Ltd. |
f0fba2ad LG |
6 | * Copyright (C) 2010 Slimlogic Ltd. |
7 | * Copyright (C) 2010 Texas Instruments Inc. | |
0664d888 | 8 | * |
d331124d | 9 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
0664d888 LG |
10 | * with code, comments and ideas from :- |
11 | * Richard Purdie <richard@openedhand.com> | |
db2a4165 FM |
12 | * |
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 | * | |
db2a4165 FM |
18 | * 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> | |
12ef193d | 31 | #include <linux/debugfs.h> |
db2a4165 | 32 | #include <linux/platform_device.h> |
f0e8ed85 | 33 | #include <linux/ctype.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
bec4fa05 | 35 | #include <linux/of.h> |
474828a4 | 36 | #include <sound/ac97_codec.h> |
db2a4165 | 37 | #include <sound/core.h> |
3028eb8c | 38 | #include <sound/jack.h> |
db2a4165 FM |
39 | #include <sound/pcm.h> |
40 | #include <sound/pcm_params.h> | |
41 | #include <sound/soc.h> | |
01d7584c | 42 | #include <sound/soc-dpcm.h> |
db2a4165 FM |
43 | #include <sound/initval.h> |
44 | ||
a8b1d34f MB |
45 | #define CREATE_TRACE_POINTS |
46 | #include <trace/events/asoc.h> | |
47 | ||
f0fba2ad LG |
48 | #define NAME_SIZE 32 |
49 | ||
db2a4165 FM |
50 | static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq); |
51 | ||
384c89e2 | 52 | #ifdef CONFIG_DEBUG_FS |
8a9dab1a MB |
53 | struct dentry *snd_soc_debugfs_root; |
54 | EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); | |
384c89e2 MB |
55 | #endif |
56 | ||
c5af3a2e | 57 | static DEFINE_MUTEX(client_mutex); |
9115171a | 58 | static LIST_HEAD(dai_list); |
12a48a8c | 59 | static LIST_HEAD(platform_list); |
0d0cf00a | 60 | static LIST_HEAD(codec_list); |
c5af3a2e | 61 | |
db2a4165 FM |
62 | /* |
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 | */ | |
67 | static int pmdown_time = 5000; | |
68 | module_param(pmdown_time, int, 0); | |
69 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); | |
70 | ||
2bc9a81e DP |
71 | /* returns the minimum number of bytes needed to represent |
72 | * a particular given value */ | |
73 | static 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 | ||
13fd179f DP |
89 | /* fill buf which is 'len' bytes with a formatted |
90 | * string of the form 'reg: value\n' */ | |
91 | static int format_register_str(struct snd_soc_codec *codec, | |
92 | unsigned int reg, char *buf, size_t len) | |
93 | { | |
00b317a4 SW |
94 | int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; |
95 | int regsize = codec->driver->reg_word_size * 2; | |
13fd179f DP |
96 | 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 | ||
25032c11 | 108 | ret = snd_soc_read(codec, reg); |
13fd179f DP |
109 | 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 | ||
2624d5fa | 124 | /* codec register dump */ |
13fd179f DP |
125 | static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, |
126 | size_t count, loff_t pos) | |
2624d5fa | 127 | { |
13fd179f | 128 | int i, step = 1; |
2bc9a81e | 129 | int wordsize, regsize; |
13fd179f DP |
130 | int len; |
131 | size_t total = 0; | |
132 | loff_t p = 0; | |
2bc9a81e | 133 | |
00b317a4 SW |
134 | wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; |
135 | regsize = codec->driver->reg_word_size * 2; | |
2624d5fa | 136 | |
13fd179f DP |
137 | len = wordsize + regsize + 2 + 1; |
138 | ||
f0fba2ad | 139 | if (!codec->driver->reg_cache_size) |
2624d5fa MB |
140 | return 0; |
141 | ||
f0fba2ad LG |
142 | if (codec->driver->reg_cache_step) |
143 | step = codec->driver->reg_cache_step; | |
2624d5fa | 144 | |
f0fba2ad | 145 | for (i = 0; i < codec->driver->reg_cache_size; i += step) { |
b92d150b | 146 | if (!snd_soc_codec_readable_register(codec, i)) |
2624d5fa | 147 | continue; |
f0fba2ad LG |
148 | if (codec->driver->display_register) { |
149 | count += codec->driver->display_register(codec, buf + count, | |
2624d5fa | 150 | PAGE_SIZE - count, i); |
5164d74d | 151 | } else { |
13fd179f DP |
152 | /* 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; | |
5164d74d | 161 | } |
2624d5fa MB |
162 | } |
163 | ||
13fd179f | 164 | total = min(total, count - 1); |
2624d5fa | 165 | |
13fd179f | 166 | return total; |
2624d5fa | 167 | } |
13fd179f | 168 | |
2624d5fa MB |
169 | static ssize_t codec_reg_show(struct device *dev, |
170 | struct device_attribute *attr, char *buf) | |
171 | { | |
36ae1a96 | 172 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
f0fba2ad | 173 | |
13fd179f | 174 | return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); |
2624d5fa MB |
175 | } |
176 | ||
177 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); | |
178 | ||
dbe21408 MB |
179 | static ssize_t pmdown_time_show(struct device *dev, |
180 | struct device_attribute *attr, char *buf) | |
181 | { | |
36ae1a96 | 182 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
dbe21408 | 183 | |
f0fba2ad | 184 | return sprintf(buf, "%ld\n", rtd->pmdown_time); |
dbe21408 MB |
185 | } |
186 | ||
187 | static ssize_t pmdown_time_set(struct device *dev, | |
188 | struct device_attribute *attr, | |
189 | const char *buf, size_t count) | |
190 | { | |
36ae1a96 | 191 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
c593b520 | 192 | int ret; |
dbe21408 | 193 | |
c593b520 MB |
194 | ret = strict_strtol(buf, 10, &rtd->pmdown_time); |
195 | if (ret) | |
196 | return ret; | |
dbe21408 MB |
197 | |
198 | return count; | |
199 | } | |
200 | ||
201 | static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); | |
202 | ||
2624d5fa | 203 | #ifdef CONFIG_DEBUG_FS |
2624d5fa | 204 | static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, |
13fd179f | 205 | size_t count, loff_t *ppos) |
2624d5fa MB |
206 | { |
207 | ssize_t ret; | |
208 | struct snd_soc_codec *codec = file->private_data; | |
13fd179f DP |
209 | char *buf; |
210 | ||
211 | if (*ppos < 0 || !count) | |
212 | return -EINVAL; | |
213 | ||
214 | buf = kmalloc(count, GFP_KERNEL); | |
2624d5fa MB |
215 | if (!buf) |
216 | return -ENOMEM; | |
13fd179f DP |
217 | |
218 | ret = soc_codec_reg_show(codec, buf, count, *ppos); | |
219 | if (ret >= 0) { | |
220 | if (copy_to_user(user_buf, buf, ret)) { | |
221 | kfree(buf); | |
222 | return -EFAULT; | |
223 | } | |
224 | *ppos += ret; | |
225 | } | |
226 | ||
2624d5fa MB |
227 | kfree(buf); |
228 | return ret; | |
229 | } | |
230 | ||
231 | static ssize_t codec_reg_write_file(struct file *file, | |
232 | const char __user *user_buf, size_t count, loff_t *ppos) | |
233 | { | |
234 | char buf[32]; | |
34e268d8 | 235 | size_t buf_size; |
2624d5fa MB |
236 | char *start = buf; |
237 | unsigned long reg, value; | |
2624d5fa MB |
238 | struct snd_soc_codec *codec = file->private_data; |
239 | ||
240 | buf_size = min(count, (sizeof(buf)-1)); | |
241 | if (copy_from_user(buf, user_buf, buf_size)) | |
242 | return -EFAULT; | |
243 | buf[buf_size] = 0; | |
2624d5fa MB |
244 | |
245 | while (*start == ' ') | |
246 | start++; | |
247 | reg = simple_strtoul(start, &start, 16); | |
2624d5fa MB |
248 | while (*start == ' ') |
249 | start++; | |
250 | if (strict_strtoul(start, 16, &value)) | |
251 | return -EINVAL; | |
0d51a9cb MB |
252 | |
253 | /* Userspace has been fiddling around behind the kernel's back */ | |
254 | add_taint(TAINT_USER); | |
255 | ||
e4f078d8 | 256 | snd_soc_write(codec, reg, value); |
2624d5fa MB |
257 | return buf_size; |
258 | } | |
259 | ||
260 | static const struct file_operations codec_reg_fops = { | |
234e3405 | 261 | .open = simple_open, |
2624d5fa MB |
262 | .read = codec_reg_read_file, |
263 | .write = codec_reg_write_file, | |
6038f373 | 264 | .llseek = default_llseek, |
2624d5fa MB |
265 | }; |
266 | ||
267 | static void soc_init_codec_debugfs(struct snd_soc_codec *codec) | |
268 | { | |
d6ce4cf3 JN |
269 | struct dentry *debugfs_card_root = codec->card->debugfs_card_root; |
270 | ||
271 | codec->debugfs_codec_root = debugfs_create_dir(codec->name, | |
272 | debugfs_card_root); | |
2624d5fa | 273 | if (!codec->debugfs_codec_root) { |
64e60f9f | 274 | dev_warn(codec->dev, "Failed to create codec debugfs directory\n"); |
2624d5fa MB |
275 | return; |
276 | } | |
277 | ||
aaee8ef1 MB |
278 | debugfs_create_bool("cache_sync", 0444, codec->debugfs_codec_root, |
279 | &codec->cache_sync); | |
280 | debugfs_create_bool("cache_only", 0444, codec->debugfs_codec_root, | |
281 | &codec->cache_only); | |
282 | ||
2624d5fa MB |
283 | codec->debugfs_reg = debugfs_create_file("codec_reg", 0644, |
284 | codec->debugfs_codec_root, | |
285 | codec, &codec_reg_fops); | |
286 | if (!codec->debugfs_reg) | |
64e60f9f | 287 | dev_warn(codec->dev, "Failed to create codec register debugfs file\n"); |
2624d5fa | 288 | |
8eecaf62 | 289 | snd_soc_dapm_debugfs_init(&codec->dapm, codec->debugfs_codec_root); |
2624d5fa MB |
290 | } |
291 | ||
292 | static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) | |
293 | { | |
294 | debugfs_remove_recursive(codec->debugfs_codec_root); | |
295 | } | |
296 | ||
731f1ab2 SG |
297 | static void soc_init_platform_debugfs(struct snd_soc_platform *platform) |
298 | { | |
299 | struct dentry *debugfs_card_root = platform->card->debugfs_card_root; | |
300 | ||
301 | platform->debugfs_platform_root = debugfs_create_dir(platform->name, | |
302 | debugfs_card_root); | |
303 | if (!platform->debugfs_platform_root) { | |
304 | dev_warn(platform->dev, | |
305 | "Failed to create platform debugfs directory\n"); | |
306 | return; | |
307 | } | |
308 | ||
309 | snd_soc_dapm_debugfs_init(&platform->dapm, | |
310 | platform->debugfs_platform_root); | |
311 | } | |
312 | ||
313 | static void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) | |
314 | { | |
315 | debugfs_remove_recursive(platform->debugfs_platform_root); | |
316 | } | |
317 | ||
c3c5a19a MB |
318 | static ssize_t codec_list_read_file(struct file *file, char __user *user_buf, |
319 | size_t count, loff_t *ppos) | |
320 | { | |
321 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
2b194f9d | 322 | ssize_t len, ret = 0; |
c3c5a19a MB |
323 | struct snd_soc_codec *codec; |
324 | ||
325 | if (!buf) | |
326 | return -ENOMEM; | |
327 | ||
2b194f9d MB |
328 | list_for_each_entry(codec, &codec_list, list) { |
329 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", | |
330 | codec->name); | |
331 | if (len >= 0) | |
332 | ret += len; | |
333 | if (ret > PAGE_SIZE) { | |
334 | ret = PAGE_SIZE; | |
335 | break; | |
336 | } | |
337 | } | |
c3c5a19a MB |
338 | |
339 | if (ret >= 0) | |
340 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
341 | ||
342 | kfree(buf); | |
343 | ||
344 | return ret; | |
345 | } | |
346 | ||
347 | static const struct file_operations codec_list_fops = { | |
348 | .read = codec_list_read_file, | |
349 | .llseek = default_llseek,/* read accesses f_pos */ | |
350 | }; | |
351 | ||
f3208780 MB |
352 | static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, |
353 | size_t count, loff_t *ppos) | |
354 | { | |
355 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
2b194f9d | 356 | ssize_t len, ret = 0; |
f3208780 MB |
357 | struct snd_soc_dai *dai; |
358 | ||
359 | if (!buf) | |
360 | return -ENOMEM; | |
361 | ||
2b194f9d MB |
362 | list_for_each_entry(dai, &dai_list, list) { |
363 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name); | |
364 | if (len >= 0) | |
365 | ret += len; | |
366 | if (ret > PAGE_SIZE) { | |
367 | ret = PAGE_SIZE; | |
368 | break; | |
369 | } | |
370 | } | |
f3208780 | 371 | |
2b194f9d | 372 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
f3208780 MB |
373 | |
374 | kfree(buf); | |
375 | ||
376 | return ret; | |
377 | } | |
378 | ||
379 | static const struct file_operations dai_list_fops = { | |
380 | .read = dai_list_read_file, | |
381 | .llseek = default_llseek,/* read accesses f_pos */ | |
382 | }; | |
383 | ||
19c7ac27 MB |
384 | static ssize_t platform_list_read_file(struct file *file, |
385 | char __user *user_buf, | |
386 | size_t count, loff_t *ppos) | |
387 | { | |
388 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
2b194f9d | 389 | ssize_t len, ret = 0; |
19c7ac27 MB |
390 | struct snd_soc_platform *platform; |
391 | ||
392 | if (!buf) | |
393 | return -ENOMEM; | |
394 | ||
2b194f9d MB |
395 | list_for_each_entry(platform, &platform_list, list) { |
396 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", | |
397 | platform->name); | |
398 | if (len >= 0) | |
399 | ret += len; | |
400 | if (ret > PAGE_SIZE) { | |
401 | ret = PAGE_SIZE; | |
402 | break; | |
403 | } | |
404 | } | |
19c7ac27 | 405 | |
2b194f9d | 406 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
19c7ac27 MB |
407 | |
408 | kfree(buf); | |
409 | ||
410 | return ret; | |
411 | } | |
412 | ||
413 | static const struct file_operations platform_list_fops = { | |
414 | .read = platform_list_read_file, | |
415 | .llseek = default_llseek,/* read accesses f_pos */ | |
416 | }; | |
417 | ||
a6052154 JN |
418 | static void soc_init_card_debugfs(struct snd_soc_card *card) |
419 | { | |
420 | card->debugfs_card_root = debugfs_create_dir(card->name, | |
8a9dab1a | 421 | snd_soc_debugfs_root); |
3a45b867 | 422 | if (!card->debugfs_card_root) { |
a6052154 | 423 | dev_warn(card->dev, |
7c08be84 | 424 | "ASoC: Failed to create card debugfs directory\n"); |
3a45b867 JN |
425 | return; |
426 | } | |
427 | ||
428 | card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, | |
429 | card->debugfs_card_root, | |
430 | &card->pop_time); | |
431 | if (!card->debugfs_pop_time) | |
432 | dev_warn(card->dev, | |
433 | "Failed to create pop time debugfs file\n"); | |
a6052154 JN |
434 | } |
435 | ||
436 | static void soc_cleanup_card_debugfs(struct snd_soc_card *card) | |
437 | { | |
438 | debugfs_remove_recursive(card->debugfs_card_root); | |
439 | } | |
440 | ||
2624d5fa MB |
441 | #else |
442 | ||
443 | static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec) | |
444 | { | |
445 | } | |
446 | ||
447 | static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec) | |
448 | { | |
449 | } | |
b95fccbc | 450 | |
731f1ab2 SG |
451 | static inline void soc_init_platform_debugfs(struct snd_soc_platform *platform) |
452 | { | |
453 | } | |
454 | ||
455 | static inline void soc_cleanup_platform_debugfs(struct snd_soc_platform *platform) | |
456 | { | |
457 | } | |
458 | ||
b95fccbc AL |
459 | static inline void soc_init_card_debugfs(struct snd_soc_card *card) |
460 | { | |
461 | } | |
462 | ||
463 | static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) | |
464 | { | |
465 | } | |
2624d5fa MB |
466 | #endif |
467 | ||
47c88fff LG |
468 | struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, |
469 | const char *dai_link, int stream) | |
470 | { | |
471 | int i; | |
472 | ||
473 | for (i = 0; i < card->num_links; i++) { | |
474 | if (card->rtd[i].dai_link->no_pcm && | |
475 | !strcmp(card->rtd[i].dai_link->name, dai_link)) | |
476 | return card->rtd[i].pcm->streams[stream].substream; | |
477 | } | |
478 | dev_dbg(card->dev, "failed to find dai link %s\n", dai_link); | |
479 | return NULL; | |
480 | } | |
481 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); | |
482 | ||
483 | struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, | |
484 | const char *dai_link) | |
485 | { | |
486 | int i; | |
487 | ||
488 | for (i = 0; i < card->num_links; i++) { | |
489 | if (!strcmp(card->rtd[i].dai_link->name, dai_link)) | |
490 | return &card->rtd[i]; | |
491 | } | |
492 | dev_dbg(card->dev, "failed to find rtd %s\n", dai_link); | |
493 | return NULL; | |
494 | } | |
495 | EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); | |
496 | ||
db2a4165 FM |
497 | #ifdef CONFIG_SND_SOC_AC97_BUS |
498 | /* unregister ac97 codec */ | |
499 | static int soc_ac97_dev_unregister(struct snd_soc_codec *codec) | |
500 | { | |
501 | if (codec->ac97->dev.bus) | |
502 | device_unregister(&codec->ac97->dev); | |
503 | return 0; | |
504 | } | |
505 | ||
506 | /* stop no dev release warning */ | |
507 | static void soc_ac97_device_release(struct device *dev){} | |
508 | ||
509 | /* register ac97 codec to bus */ | |
510 | static int soc_ac97_dev_register(struct snd_soc_codec *codec) | |
511 | { | |
512 | int err; | |
513 | ||
514 | codec->ac97->dev.bus = &ac97_bus_type; | |
4ac5c61f | 515 | codec->ac97->dev.parent = codec->card->dev; |
db2a4165 FM |
516 | codec->ac97->dev.release = soc_ac97_device_release; |
517 | ||
bb072bf0 | 518 | dev_set_name(&codec->ac97->dev, "%d-%d:%s", |
f0fba2ad | 519 | codec->card->snd_card->number, 0, codec->name); |
db2a4165 FM |
520 | err = device_register(&codec->ac97->dev); |
521 | if (err < 0) { | |
522 | snd_printk(KERN_ERR "Can't register ac97 bus\n"); | |
523 | codec->ac97->dev.bus = NULL; | |
524 | return err; | |
525 | } | |
526 | return 0; | |
527 | } | |
528 | #endif | |
529 | ||
6f8ab4ac | 530 | #ifdef CONFIG_PM_SLEEP |
db2a4165 | 531 | /* powers down audio subsystem for suspend */ |
6f8ab4ac | 532 | int snd_soc_suspend(struct device *dev) |
db2a4165 | 533 | { |
6f8ab4ac | 534 | struct snd_soc_card *card = dev_get_drvdata(dev); |
2eea392d | 535 | struct snd_soc_codec *codec; |
db2a4165 FM |
536 | int i; |
537 | ||
e3509ff0 DM |
538 | /* If the initialization of this soc device failed, there is no codec |
539 | * associated with it. Just bail out in this case. | |
540 | */ | |
f0fba2ad | 541 | if (list_empty(&card->codec_dev_list)) |
e3509ff0 DM |
542 | return 0; |
543 | ||
6ed25978 AG |
544 | /* Due to the resume being scheduled into a workqueue we could |
545 | * suspend before that's finished - wait for it to complete. | |
546 | */ | |
f0fba2ad LG |
547 | snd_power_lock(card->snd_card); |
548 | snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); | |
549 | snd_power_unlock(card->snd_card); | |
6ed25978 AG |
550 | |
551 | /* we're going to block userspace touching us until resume completes */ | |
f0fba2ad | 552 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); |
6ed25978 | 553 | |
a00f90f9 | 554 | /* mute any active DACs */ |
f0fba2ad LG |
555 | for (i = 0; i < card->num_rtd; i++) { |
556 | struct snd_soc_dai *dai = card->rtd[i].codec_dai; | |
557 | struct snd_soc_dai_driver *drv = dai->driver; | |
3efab7dc | 558 | |
f0fba2ad | 559 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
560 | continue; |
561 | ||
f0fba2ad LG |
562 | if (drv->ops->digital_mute && dai->playback_active) |
563 | drv->ops->digital_mute(dai, 1); | |
db2a4165 FM |
564 | } |
565 | ||
4ccab3e7 | 566 | /* suspend all pcms */ |
f0fba2ad LG |
567 | for (i = 0; i < card->num_rtd; i++) { |
568 | if (card->rtd[i].dai_link->ignore_suspend) | |
3efab7dc MB |
569 | continue; |
570 | ||
f0fba2ad | 571 | snd_pcm_suspend_all(card->rtd[i].pcm); |
3efab7dc | 572 | } |
4ccab3e7 | 573 | |
87506549 | 574 | if (card->suspend_pre) |
70b2ac12 | 575 | card->suspend_pre(card); |
db2a4165 | 576 | |
f0fba2ad LG |
577 | for (i = 0; i < card->num_rtd; i++) { |
578 | struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; | |
579 | struct snd_soc_platform *platform = card->rtd[i].platform; | |
3efab7dc | 580 | |
f0fba2ad | 581 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
582 | continue; |
583 | ||
f0fba2ad LG |
584 | if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control) |
585 | cpu_dai->driver->suspend(cpu_dai); | |
586 | if (platform->driver->suspend && !platform->suspended) { | |
587 | platform->driver->suspend(cpu_dai); | |
588 | platform->suspended = 1; | |
589 | } | |
db2a4165 FM |
590 | } |
591 | ||
592 | /* close any waiting streams and save state */ | |
f0fba2ad | 593 | for (i = 0; i < card->num_rtd; i++) { |
5b84ba26 | 594 | flush_delayed_work_sync(&card->rtd[i].delayed_work); |
ce6120cc | 595 | card->rtd[i].codec->dapm.suspend_bias_level = card->rtd[i].codec->dapm.bias_level; |
f0fba2ad | 596 | } |
db2a4165 | 597 | |
f0fba2ad | 598 | for (i = 0; i < card->num_rtd; i++) { |
3efab7dc | 599 | |
f0fba2ad | 600 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
601 | continue; |
602 | ||
7bd3a6f3 MB |
603 | snd_soc_dapm_stream_event(&card->rtd[i], |
604 | SNDRV_PCM_STREAM_PLAYBACK, | |
7bd3a6f3 | 605 | SND_SOC_DAPM_STREAM_SUSPEND); |
f0fba2ad | 606 | |
7bd3a6f3 MB |
607 | snd_soc_dapm_stream_event(&card->rtd[i], |
608 | SNDRV_PCM_STREAM_CAPTURE, | |
7bd3a6f3 | 609 | SND_SOC_DAPM_STREAM_SUSPEND); |
db2a4165 FM |
610 | } |
611 | ||
f0fba2ad | 612 | /* suspend all CODECs */ |
2eea392d | 613 | list_for_each_entry(codec, &card->codec_dev_list, card_list) { |
f0fba2ad LG |
614 | /* If there are paths active then the CODEC will be held with |
615 | * bias _ON and should not be suspended. */ | |
616 | if (!codec->suspended && codec->driver->suspend) { | |
ce6120cc | 617 | switch (codec->dapm.bias_level) { |
f0fba2ad | 618 | case SND_SOC_BIAS_STANDBY: |
125a25da MB |
619 | /* |
620 | * If the CODEC is capable of idle | |
621 | * bias off then being in STANDBY | |
622 | * means it's doing something, | |
623 | * otherwise fall through. | |
624 | */ | |
625 | if (codec->dapm.idle_bias_off) { | |
626 | dev_dbg(codec->dev, | |
627 | "idle_bias_off CODEC on over suspend\n"); | |
628 | break; | |
629 | } | |
f0fba2ad | 630 | case SND_SOC_BIAS_OFF: |
84b315ee | 631 | codec->driver->suspend(codec); |
f0fba2ad | 632 | codec->suspended = 1; |
7be4ba24 | 633 | codec->cache_sync = 1; |
f0fba2ad LG |
634 | break; |
635 | default: | |
636 | dev_dbg(codec->dev, "CODEC is on over suspend\n"); | |
637 | break; | |
638 | } | |
1547aba9 MB |
639 | } |
640 | } | |
db2a4165 | 641 | |
f0fba2ad LG |
642 | for (i = 0; i < card->num_rtd; i++) { |
643 | struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; | |
3efab7dc | 644 | |
f0fba2ad | 645 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
646 | continue; |
647 | ||
f0fba2ad LG |
648 | if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control) |
649 | cpu_dai->driver->suspend(cpu_dai); | |
db2a4165 FM |
650 | } |
651 | ||
87506549 | 652 | if (card->suspend_post) |
70b2ac12 | 653 | card->suspend_post(card); |
db2a4165 FM |
654 | |
655 | return 0; | |
656 | } | |
6f8ab4ac | 657 | EXPORT_SYMBOL_GPL(snd_soc_suspend); |
db2a4165 | 658 | |
6ed25978 AG |
659 | /* deferred resume work, so resume can complete before we finished |
660 | * setting our codec back up, which can be very slow on I2C | |
661 | */ | |
662 | static void soc_resume_deferred(struct work_struct *work) | |
db2a4165 | 663 | { |
f0fba2ad LG |
664 | struct snd_soc_card *card = |
665 | container_of(work, struct snd_soc_card, deferred_resume_work); | |
2eea392d | 666 | struct snd_soc_codec *codec; |
db2a4165 FM |
667 | int i; |
668 | ||
6ed25978 AG |
669 | /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
670 | * so userspace apps are blocked from touching us | |
671 | */ | |
672 | ||
f0fba2ad | 673 | dev_dbg(card->dev, "starting resume work\n"); |
6ed25978 | 674 | |
9949788b | 675 | /* Bring us up into D2 so that DAPM starts enabling things */ |
f0fba2ad | 676 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); |
9949788b | 677 | |
87506549 | 678 | if (card->resume_pre) |
70b2ac12 | 679 | card->resume_pre(card); |
db2a4165 | 680 | |
f0fba2ad LG |
681 | /* resume AC97 DAIs */ |
682 | for (i = 0; i < card->num_rtd; i++) { | |
683 | struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; | |
3efab7dc | 684 | |
f0fba2ad | 685 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
686 | continue; |
687 | ||
f0fba2ad LG |
688 | if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control) |
689 | cpu_dai->driver->resume(cpu_dai); | |
690 | } | |
691 | ||
2eea392d | 692 | list_for_each_entry(codec, &card->codec_dev_list, card_list) { |
f0fba2ad LG |
693 | /* If the CODEC was idle over suspend then it will have been |
694 | * left with bias OFF or STANDBY and suspended so we must now | |
695 | * resume. Otherwise the suspend was suppressed. | |
696 | */ | |
697 | if (codec->driver->resume && codec->suspended) { | |
ce6120cc | 698 | switch (codec->dapm.bias_level) { |
f0fba2ad LG |
699 | case SND_SOC_BIAS_STANDBY: |
700 | case SND_SOC_BIAS_OFF: | |
701 | codec->driver->resume(codec); | |
702 | codec->suspended = 0; | |
703 | break; | |
704 | default: | |
705 | dev_dbg(codec->dev, "CODEC was on over suspend\n"); | |
706 | break; | |
707 | } | |
1547aba9 MB |
708 | } |
709 | } | |
db2a4165 | 710 | |
f0fba2ad | 711 | for (i = 0; i < card->num_rtd; i++) { |
3efab7dc | 712 | |
f0fba2ad | 713 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
714 | continue; |
715 | ||
7bd3a6f3 | 716 | snd_soc_dapm_stream_event(&card->rtd[i], |
d9b0951b | 717 | SNDRV_PCM_STREAM_PLAYBACK, |
7bd3a6f3 | 718 | SND_SOC_DAPM_STREAM_RESUME); |
f0fba2ad | 719 | |
7bd3a6f3 | 720 | snd_soc_dapm_stream_event(&card->rtd[i], |
d9b0951b | 721 | SNDRV_PCM_STREAM_CAPTURE, |
7bd3a6f3 | 722 | SND_SOC_DAPM_STREAM_RESUME); |
db2a4165 FM |
723 | } |
724 | ||
3ff3f64b | 725 | /* unmute any active DACs */ |
f0fba2ad LG |
726 | for (i = 0; i < card->num_rtd; i++) { |
727 | struct snd_soc_dai *dai = card->rtd[i].codec_dai; | |
728 | struct snd_soc_dai_driver *drv = dai->driver; | |
3efab7dc | 729 | |
f0fba2ad | 730 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
731 | continue; |
732 | ||
f0fba2ad LG |
733 | if (drv->ops->digital_mute && dai->playback_active) |
734 | drv->ops->digital_mute(dai, 0); | |
db2a4165 FM |
735 | } |
736 | ||
f0fba2ad LG |
737 | for (i = 0; i < card->num_rtd; i++) { |
738 | struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; | |
739 | struct snd_soc_platform *platform = card->rtd[i].platform; | |
3efab7dc | 740 | |
f0fba2ad | 741 | if (card->rtd[i].dai_link->ignore_suspend) |
3efab7dc MB |
742 | continue; |
743 | ||
f0fba2ad LG |
744 | if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control) |
745 | cpu_dai->driver->resume(cpu_dai); | |
746 | if (platform->driver->resume && platform->suspended) { | |
747 | platform->driver->resume(cpu_dai); | |
748 | platform->suspended = 0; | |
749 | } | |
db2a4165 FM |
750 | } |
751 | ||
87506549 | 752 | if (card->resume_post) |
70b2ac12 | 753 | card->resume_post(card); |
db2a4165 | 754 | |
f0fba2ad | 755 | dev_dbg(card->dev, "resume work completed\n"); |
6ed25978 AG |
756 | |
757 | /* userspace can access us now we are back as we were before */ | |
f0fba2ad | 758 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); |
6ed25978 AG |
759 | } |
760 | ||
761 | /* powers up audio subsystem after a suspend */ | |
6f8ab4ac | 762 | int snd_soc_resume(struct device *dev) |
6ed25978 | 763 | { |
6f8ab4ac | 764 | struct snd_soc_card *card = dev_get_drvdata(dev); |
82e14e8b | 765 | int i, ac97_control = 0; |
b9dd94a8 | 766 | |
5ff1ddf2 EM |
767 | /* If the initialization of this soc device failed, there is no codec |
768 | * associated with it. Just bail out in this case. | |
769 | */ | |
770 | if (list_empty(&card->codec_dev_list)) | |
771 | return 0; | |
772 | ||
64ab9baa MB |
773 | /* AC97 devices might have other drivers hanging off them so |
774 | * need to resume immediately. Other drivers don't have that | |
775 | * problem and may take a substantial amount of time to resume | |
776 | * due to I/O costs and anti-pop so handle them out of line. | |
777 | */ | |
f0fba2ad LG |
778 | for (i = 0; i < card->num_rtd; i++) { |
779 | struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai; | |
82e14e8b SW |
780 | ac97_control |= cpu_dai->driver->ac97_control; |
781 | } | |
782 | if (ac97_control) { | |
783 | dev_dbg(dev, "Resuming AC97 immediately\n"); | |
784 | soc_resume_deferred(&card->deferred_resume_work); | |
785 | } else { | |
786 | dev_dbg(dev, "Scheduling resume work\n"); | |
787 | if (!schedule_work(&card->deferred_resume_work)) | |
788 | dev_err(dev, "resume work item may be lost\n"); | |
64ab9baa | 789 | } |
6ed25978 | 790 | |
db2a4165 FM |
791 | return 0; |
792 | } | |
6f8ab4ac | 793 | EXPORT_SYMBOL_GPL(snd_soc_resume); |
db2a4165 | 794 | #else |
6f8ab4ac MB |
795 | #define snd_soc_suspend NULL |
796 | #define snd_soc_resume NULL | |
db2a4165 FM |
797 | #endif |
798 | ||
85e7652d | 799 | static const struct snd_soc_dai_ops null_dai_ops = { |
02a06d30 BS |
800 | }; |
801 | ||
f0fba2ad | 802 | static int soc_bind_dai_link(struct snd_soc_card *card, int num) |
db2a4165 | 803 | { |
f0fba2ad LG |
804 | struct snd_soc_dai_link *dai_link = &card->dai_link[num]; |
805 | struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; | |
fe3e78e0 | 806 | struct snd_soc_codec *codec; |
435c5e25 | 807 | struct snd_soc_platform *platform; |
f0fba2ad | 808 | struct snd_soc_dai *codec_dai, *cpu_dai; |
848dd8be | 809 | const char *platform_name; |
435c5e25 | 810 | |
f0fba2ad | 811 | dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num); |
435c5e25 | 812 | |
b19e6e7b | 813 | /* Find CPU DAI from registered DAIs*/ |
f0fba2ad | 814 | list_for_each_entry(cpu_dai, &dai_list, list) { |
bc92657a SW |
815 | if (dai_link->cpu_of_node && |
816 | (cpu_dai->dev->of_node != dai_link->cpu_of_node)) | |
817 | continue; | |
818 | if (dai_link->cpu_name && | |
819 | strcmp(dev_name(cpu_dai->dev), dai_link->cpu_name)) | |
820 | continue; | |
821 | if (dai_link->cpu_dai_name && | |
822 | strcmp(cpu_dai->name, dai_link->cpu_dai_name)) | |
823 | continue; | |
2610ab77 SW |
824 | |
825 | rtd->cpu_dai = cpu_dai; | |
435c5e25 | 826 | } |
6308419a | 827 | |
b19e6e7b MB |
828 | if (!rtd->cpu_dai) { |
829 | dev_dbg(card->dev, "CPU DAI %s not registered\n", | |
830 | dai_link->cpu_dai_name); | |
831 | return -EPROBE_DEFER; | |
f0fba2ad LG |
832 | } |
833 | ||
b19e6e7b | 834 | /* Find CODEC from registered CODECs */ |
f0fba2ad | 835 | list_for_each_entry(codec, &codec_list, list) { |
5a504963 SW |
836 | if (dai_link->codec_of_node) { |
837 | if (codec->dev->of_node != dai_link->codec_of_node) | |
838 | continue; | |
839 | } else { | |
840 | if (strcmp(codec->name, dai_link->codec_name)) | |
841 | continue; | |
842 | } | |
2610ab77 SW |
843 | |
844 | rtd->codec = codec; | |
845 | ||
846 | /* | |
847 | * CODEC found, so find CODEC DAI from registered DAIs from | |
848 | * this CODEC | |
849 | */ | |
850 | list_for_each_entry(codec_dai, &dai_list, list) { | |
851 | if (codec->dev == codec_dai->dev && | |
852 | !strcmp(codec_dai->name, | |
853 | dai_link->codec_dai_name)) { | |
f0fba2ad | 854 | |
2610ab77 | 855 | rtd->codec_dai = codec_dai; |
2610ab77 | 856 | } |
435c5e25 | 857 | } |
2610ab77 | 858 | |
b19e6e7b MB |
859 | if (!rtd->codec_dai) { |
860 | dev_dbg(card->dev, "CODEC DAI %s not registered\n", | |
861 | dai_link->codec_dai_name); | |
862 | return -EPROBE_DEFER; | |
863 | } | |
f0fba2ad | 864 | } |
6b05eda6 | 865 | |
b19e6e7b MB |
866 | if (!rtd->codec) { |
867 | dev_dbg(card->dev, "CODEC %s not registered\n", | |
868 | dai_link->codec_name); | |
869 | return -EPROBE_DEFER; | |
870 | } | |
848dd8be MB |
871 | |
872 | /* if there's no platform we match on the empty platform */ | |
873 | platform_name = dai_link->platform_name; | |
5a504963 | 874 | if (!platform_name && !dai_link->platform_of_node) |
848dd8be MB |
875 | platform_name = "snd-soc-dummy"; |
876 | ||
b19e6e7b | 877 | /* find one from the set of registered platforms */ |
f0fba2ad | 878 | list_for_each_entry(platform, &platform_list, list) { |
5a504963 SW |
879 | if (dai_link->platform_of_node) { |
880 | if (platform->dev->of_node != | |
881 | dai_link->platform_of_node) | |
882 | continue; | |
883 | } else { | |
884 | if (strcmp(platform->name, platform_name)) | |
885 | continue; | |
886 | } | |
2610ab77 SW |
887 | |
888 | rtd->platform = platform; | |
02a06d30 | 889 | } |
b19e6e7b MB |
890 | if (!rtd->platform) { |
891 | dev_dbg(card->dev, "platform %s not registered\n", | |
f0fba2ad | 892 | dai_link->platform_name); |
b19e6e7b | 893 | return -EPROBE_DEFER; |
f0fba2ad | 894 | } |
b19e6e7b MB |
895 | |
896 | card->num_rtd++; | |
897 | ||
898 | return 0; | |
f0fba2ad LG |
899 | } |
900 | ||
589c3563 JN |
901 | static void soc_remove_codec(struct snd_soc_codec *codec) |
902 | { | |
903 | int err; | |
904 | ||
905 | if (codec->driver->remove) { | |
906 | err = codec->driver->remove(codec); | |
907 | if (err < 0) | |
908 | dev_err(codec->dev, | |
909 | "asoc: failed to remove %s: %d\n", | |
910 | codec->name, err); | |
911 | } | |
912 | ||
913 | /* Make sure all DAPM widgets are freed */ | |
914 | snd_soc_dapm_free(&codec->dapm); | |
915 | ||
916 | soc_cleanup_codec_debugfs(codec); | |
917 | codec->probed = 0; | |
918 | list_del(&codec->card_list); | |
919 | module_put(codec->dev->driver->owner); | |
920 | } | |
921 | ||
0168bf0d | 922 | static void soc_remove_dai_link(struct snd_soc_card *card, int num, int order) |
f0fba2ad LG |
923 | { |
924 | struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; | |
925 | struct snd_soc_codec *codec = rtd->codec; | |
926 | struct snd_soc_platform *platform = rtd->platform; | |
927 | struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai; | |
928 | int err; | |
929 | ||
930 | /* unregister the rtd device */ | |
931 | if (rtd->dev_registered) { | |
36ae1a96 MB |
932 | device_remove_file(rtd->dev, &dev_attr_pmdown_time); |
933 | device_remove_file(rtd->dev, &dev_attr_codec_reg); | |
934 | device_unregister(rtd->dev); | |
f0fba2ad LG |
935 | rtd->dev_registered = 0; |
936 | } | |
937 | ||
938 | /* remove the CODEC DAI */ | |
0168bf0d LG |
939 | if (codec_dai && codec_dai->probed && |
940 | codec_dai->driver->remove_order == order) { | |
f0fba2ad LG |
941 | if (codec_dai->driver->remove) { |
942 | err = codec_dai->driver->remove(codec_dai); | |
943 | if (err < 0) | |
0837fc62 FE |
944 | pr_err("asoc: failed to remove %s: %d\n", |
945 | codec_dai->name, err); | |
6b05eda6 | 946 | } |
f0fba2ad LG |
947 | codec_dai->probed = 0; |
948 | list_del(&codec_dai->card_list); | |
949 | } | |
6b05eda6 | 950 | |
f0fba2ad | 951 | /* remove the platform */ |
0168bf0d LG |
952 | if (platform && platform->probed && |
953 | platform->driver->remove_order == order) { | |
f0fba2ad LG |
954 | if (platform->driver->remove) { |
955 | err = platform->driver->remove(platform); | |
956 | if (err < 0) | |
0837fc62 FE |
957 | pr_err("asoc: failed to remove %s: %d\n", |
958 | platform->name, err); | |
f0fba2ad | 959 | } |
675c496b LG |
960 | |
961 | /* Make sure all DAPM widgets are freed */ | |
962 | snd_soc_dapm_free(&platform->dapm); | |
963 | ||
731f1ab2 | 964 | soc_cleanup_platform_debugfs(platform); |
f0fba2ad LG |
965 | platform->probed = 0; |
966 | list_del(&platform->card_list); | |
967 | module_put(platform->dev->driver->owner); | |
968 | } | |
435c5e25 | 969 | |
f0fba2ad | 970 | /* remove the CODEC */ |
0168bf0d LG |
971 | if (codec && codec->probed && |
972 | codec->driver->remove_order == order) | |
589c3563 | 973 | soc_remove_codec(codec); |
db2a4165 | 974 | |
f0fba2ad | 975 | /* remove the cpu_dai */ |
0168bf0d LG |
976 | if (cpu_dai && cpu_dai->probed && |
977 | cpu_dai->driver->remove_order == order) { | |
f0fba2ad LG |
978 | if (cpu_dai->driver->remove) { |
979 | err = cpu_dai->driver->remove(cpu_dai); | |
980 | if (err < 0) | |
0837fc62 FE |
981 | pr_err("asoc: failed to remove %s: %d\n", |
982 | cpu_dai->name, err); | |
db2a4165 | 983 | } |
f0fba2ad LG |
984 | cpu_dai->probed = 0; |
985 | list_del(&cpu_dai->card_list); | |
986 | module_put(cpu_dai->dev->driver->owner); | |
db2a4165 | 987 | } |
f0fba2ad | 988 | } |
db2a4165 | 989 | |
0671fd8e KM |
990 | static void soc_remove_dai_links(struct snd_soc_card *card) |
991 | { | |
0168bf0d | 992 | int dai, order; |
0671fd8e | 993 | |
0168bf0d LG |
994 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; |
995 | order++) { | |
996 | for (dai = 0; dai < card->num_rtd; dai++) | |
997 | soc_remove_dai_link(card, dai, order); | |
998 | } | |
0671fd8e KM |
999 | card->num_rtd = 0; |
1000 | } | |
1001 | ||
ead9b919 JN |
1002 | static void soc_set_name_prefix(struct snd_soc_card *card, |
1003 | struct snd_soc_codec *codec) | |
1004 | { | |
1005 | int i; | |
1006 | ||
ff819b83 | 1007 | if (card->codec_conf == NULL) |
ead9b919 JN |
1008 | return; |
1009 | ||
ff819b83 DP |
1010 | for (i = 0; i < card->num_configs; i++) { |
1011 | struct snd_soc_codec_conf *map = &card->codec_conf[i]; | |
ead9b919 JN |
1012 | if (map->dev_name && !strcmp(codec->name, map->dev_name)) { |
1013 | codec->name_prefix = map->name_prefix; | |
1014 | break; | |
1015 | } | |
1016 | } | |
1017 | } | |
1018 | ||
589c3563 JN |
1019 | static int soc_probe_codec(struct snd_soc_card *card, |
1020 | struct snd_soc_codec *codec) | |
1021 | { | |
1022 | int ret = 0; | |
89b95ac0 | 1023 | const struct snd_soc_codec_driver *driver = codec->driver; |
888df395 | 1024 | struct snd_soc_dai *dai; |
589c3563 JN |
1025 | |
1026 | codec->card = card; | |
1027 | codec->dapm.card = card; | |
1028 | soc_set_name_prefix(card, codec); | |
1029 | ||
70d29331 JN |
1030 | if (!try_module_get(codec->dev->driver->owner)) |
1031 | return -ENODEV; | |
1032 | ||
d5d1e0be LPC |
1033 | soc_init_codec_debugfs(codec); |
1034 | ||
77530150 LPC |
1035 | if (driver->dapm_widgets) |
1036 | snd_soc_dapm_new_controls(&codec->dapm, driver->dapm_widgets, | |
1037 | driver->num_dapm_widgets); | |
1038 | ||
888df395 MB |
1039 | /* Create DAPM widgets for each DAI stream */ |
1040 | list_for_each_entry(dai, &dai_list, list) { | |
1041 | if (dai->dev != codec->dev) | |
1042 | continue; | |
1043 | ||
1044 | snd_soc_dapm_new_dai_widgets(&codec->dapm, dai); | |
1045 | } | |
1046 | ||
33c5f969 MB |
1047 | codec->dapm.idle_bias_off = driver->idle_bias_off; |
1048 | ||
89b95ac0 MB |
1049 | if (driver->probe) { |
1050 | ret = driver->probe(codec); | |
589c3563 JN |
1051 | if (ret < 0) { |
1052 | dev_err(codec->dev, | |
1053 | "asoc: failed to probe CODEC %s: %d\n", | |
1054 | codec->name, ret); | |
70d29331 | 1055 | goto err_probe; |
589c3563 JN |
1056 | } |
1057 | } | |
1058 | ||
b7af1daf | 1059 | if (driver->controls) |
022658be | 1060 | snd_soc_add_codec_controls(codec, driver->controls, |
b7af1daf | 1061 | driver->num_controls); |
89b95ac0 MB |
1062 | if (driver->dapm_routes) |
1063 | snd_soc_dapm_add_routes(&codec->dapm, driver->dapm_routes, | |
1064 | driver->num_dapm_routes); | |
1065 | ||
589c3563 JN |
1066 | /* mark codec as probed and add to card codec list */ |
1067 | codec->probed = 1; | |
1068 | list_add(&codec->card_list, &card->codec_dev_list); | |
7be31be8 | 1069 | list_add(&codec->dapm.list, &card->dapm_list); |
589c3563 | 1070 | |
70d29331 JN |
1071 | return 0; |
1072 | ||
1073 | err_probe: | |
d5d1e0be | 1074 | soc_cleanup_codec_debugfs(codec); |
70d29331 JN |
1075 | module_put(codec->dev->driver->owner); |
1076 | ||
589c3563 JN |
1077 | return ret; |
1078 | } | |
1079 | ||
956245e9 LG |
1080 | static int soc_probe_platform(struct snd_soc_card *card, |
1081 | struct snd_soc_platform *platform) | |
1082 | { | |
1083 | int ret = 0; | |
1084 | const struct snd_soc_platform_driver *driver = platform->driver; | |
be09ad90 | 1085 | struct snd_soc_dai *dai; |
956245e9 LG |
1086 | |
1087 | platform->card = card; | |
b7950641 | 1088 | platform->dapm.card = card; |
956245e9 LG |
1089 | |
1090 | if (!try_module_get(platform->dev->driver->owner)) | |
1091 | return -ENODEV; | |
1092 | ||
731f1ab2 SG |
1093 | soc_init_platform_debugfs(platform); |
1094 | ||
cb2cf612 LG |
1095 | if (driver->dapm_widgets) |
1096 | snd_soc_dapm_new_controls(&platform->dapm, | |
1097 | driver->dapm_widgets, driver->num_dapm_widgets); | |
1098 | ||
be09ad90 LG |
1099 | /* Create DAPM widgets for each DAI stream */ |
1100 | list_for_each_entry(dai, &dai_list, list) { | |
1101 | if (dai->dev != platform->dev) | |
1102 | continue; | |
1103 | ||
1104 | snd_soc_dapm_new_dai_widgets(&platform->dapm, dai); | |
1105 | } | |
1106 | ||
3fec6b6d SW |
1107 | platform->dapm.idle_bias_off = 1; |
1108 | ||
956245e9 LG |
1109 | if (driver->probe) { |
1110 | ret = driver->probe(platform); | |
1111 | if (ret < 0) { | |
1112 | dev_err(platform->dev, | |
1113 | "asoc: failed to probe platform %s: %d\n", | |
1114 | platform->name, ret); | |
1115 | goto err_probe; | |
1116 | } | |
1117 | } | |
1118 | ||
cb2cf612 LG |
1119 | if (driver->controls) |
1120 | snd_soc_add_platform_controls(platform, driver->controls, | |
1121 | driver->num_controls); | |
1122 | if (driver->dapm_routes) | |
1123 | snd_soc_dapm_add_routes(&platform->dapm, driver->dapm_routes, | |
1124 | driver->num_dapm_routes); | |
1125 | ||
956245e9 LG |
1126 | /* mark platform as probed and add to card platform list */ |
1127 | platform->probed = 1; | |
1128 | list_add(&platform->card_list, &card->platform_dev_list); | |
b7950641 | 1129 | list_add(&platform->dapm.list, &card->dapm_list); |
956245e9 LG |
1130 | |
1131 | return 0; | |
1132 | ||
1133 | err_probe: | |
02db1103 | 1134 | soc_cleanup_platform_debugfs(platform); |
956245e9 LG |
1135 | module_put(platform->dev->driver->owner); |
1136 | ||
1137 | return ret; | |
1138 | } | |
1139 | ||
36ae1a96 MB |
1140 | static void rtd_release(struct device *dev) |
1141 | { | |
1142 | kfree(dev); | |
1143 | } | |
f0fba2ad | 1144 | |
589c3563 JN |
1145 | static int soc_post_component_init(struct snd_soc_card *card, |
1146 | struct snd_soc_codec *codec, | |
1147 | int num, int dailess) | |
1148 | { | |
1149 | struct snd_soc_dai_link *dai_link = NULL; | |
1150 | struct snd_soc_aux_dev *aux_dev = NULL; | |
1151 | struct snd_soc_pcm_runtime *rtd; | |
1152 | const char *temp, *name; | |
1153 | int ret = 0; | |
1154 | ||
1155 | if (!dailess) { | |
1156 | dai_link = &card->dai_link[num]; | |
1157 | rtd = &card->rtd[num]; | |
1158 | name = dai_link->name; | |
1159 | } else { | |
1160 | aux_dev = &card->aux_dev[num]; | |
1161 | rtd = &card->rtd_aux[num]; | |
1162 | name = aux_dev->name; | |
1163 | } | |
0962bb21 | 1164 | rtd->card = card; |
589c3563 | 1165 | |
4b1cfcb4 MB |
1166 | /* Make sure all DAPM widgets are instantiated */ |
1167 | snd_soc_dapm_new_widgets(&codec->dapm); | |
1168 | ||
589c3563 JN |
1169 | /* machine controls, routes and widgets are not prefixed */ |
1170 | temp = codec->name_prefix; | |
1171 | codec->name_prefix = NULL; | |
1172 | ||
1173 | /* do machine specific initialization */ | |
1174 | if (!dailess && dai_link->init) | |
1175 | ret = dai_link->init(rtd); | |
1176 | else if (dailess && aux_dev->init) | |
1177 | ret = aux_dev->init(&codec->dapm); | |
1178 | if (ret < 0) { | |
1179 | dev_err(card->dev, "asoc: failed to init %s: %d\n", name, ret); | |
1180 | return ret; | |
1181 | } | |
1182 | codec->name_prefix = temp; | |
1183 | ||
589c3563 JN |
1184 | /* register the rtd device */ |
1185 | rtd->codec = codec; | |
36ae1a96 MB |
1186 | |
1187 | rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
1188 | if (!rtd->dev) | |
1189 | return -ENOMEM; | |
1190 | device_initialize(rtd->dev); | |
1191 | rtd->dev->parent = card->dev; | |
1192 | rtd->dev->release = rtd_release; | |
1193 | rtd->dev->init_name = name; | |
1194 | dev_set_drvdata(rtd->dev, rtd); | |
b8c0dab9 | 1195 | mutex_init(&rtd->pcm_mutex); |
01d7584c LG |
1196 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); |
1197 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); | |
1198 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); | |
1199 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); | |
36ae1a96 | 1200 | ret = device_add(rtd->dev); |
589c3563 JN |
1201 | if (ret < 0) { |
1202 | dev_err(card->dev, | |
1203 | "asoc: failed to register runtime device: %d\n", ret); | |
1204 | return ret; | |
1205 | } | |
1206 | rtd->dev_registered = 1; | |
1207 | ||
1208 | /* add DAPM sysfs entries for this codec */ | |
36ae1a96 | 1209 | ret = snd_soc_dapm_sys_add(rtd->dev); |
589c3563 JN |
1210 | if (ret < 0) |
1211 | dev_err(codec->dev, | |
1212 | "asoc: failed to add codec dapm sysfs entries: %d\n", | |
1213 | ret); | |
1214 | ||
1215 | /* add codec sysfs entries */ | |
36ae1a96 | 1216 | ret = device_create_file(rtd->dev, &dev_attr_codec_reg); |
589c3563 JN |
1217 | if (ret < 0) |
1218 | dev_err(codec->dev, | |
1219 | "asoc: failed to add codec sysfs files: %d\n", ret); | |
1220 | ||
f86dcef8 LG |
1221 | #ifdef CONFIG_DEBUG_FS |
1222 | /* add DPCM sysfs entries */ | |
cd0f8911 | 1223 | if (!dailess && !dai_link->dynamic) |
f86dcef8 LG |
1224 | goto out; |
1225 | ||
1226 | ret = soc_dpcm_debugfs_add(rtd); | |
1227 | if (ret < 0) | |
1228 | dev_err(rtd->dev, "asoc: failed to add dpcm sysfs entries: %d\n", ret); | |
1229 | ||
1230 | out: | |
1231 | #endif | |
589c3563 JN |
1232 | return 0; |
1233 | } | |
1234 | ||
0168bf0d | 1235 | static int soc_probe_dai_link(struct snd_soc_card *card, int num, int order) |
f0fba2ad LG |
1236 | { |
1237 | struct snd_soc_dai_link *dai_link = &card->dai_link[num]; | |
1238 | struct snd_soc_pcm_runtime *rtd = &card->rtd[num]; | |
1239 | struct snd_soc_codec *codec = rtd->codec; | |
1240 | struct snd_soc_platform *platform = rtd->platform; | |
c74184ed MB |
1241 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
1242 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
1243 | struct snd_soc_dapm_widget *play_w, *capture_w; | |
f0fba2ad LG |
1244 | int ret; |
1245 | ||
0168bf0d LG |
1246 | dev_dbg(card->dev, "probe %s dai link %d late %d\n", |
1247 | card->name, num, order); | |
f0fba2ad LG |
1248 | |
1249 | /* config components */ | |
f0fba2ad | 1250 | cpu_dai->platform = platform; |
f0fba2ad LG |
1251 | codec_dai->card = card; |
1252 | cpu_dai->card = card; | |
1253 | ||
1254 | /* set default power off timeout */ | |
1255 | rtd->pmdown_time = pmdown_time; | |
1256 | ||
1257 | /* probe the cpu_dai */ | |
0168bf0d LG |
1258 | if (!cpu_dai->probed && |
1259 | cpu_dai->driver->probe_order == order) { | |
be09ad90 | 1260 | cpu_dai->dapm.card = card; |
61b61e3c LG |
1261 | if (!try_module_get(cpu_dai->dev->driver->owner)) |
1262 | return -ENODEV; | |
1263 | ||
be09ad90 LG |
1264 | snd_soc_dapm_new_dai_widgets(&cpu_dai->dapm, cpu_dai); |
1265 | ||
f0fba2ad LG |
1266 | if (cpu_dai->driver->probe) { |
1267 | ret = cpu_dai->driver->probe(cpu_dai); | |
1268 | if (ret < 0) { | |
0837fc62 FE |
1269 | pr_err("asoc: failed to probe CPU DAI %s: %d\n", |
1270 | cpu_dai->name, ret); | |
61b61e3c | 1271 | module_put(cpu_dai->dev->driver->owner); |
f0fba2ad LG |
1272 | return ret; |
1273 | } | |
1274 | } | |
1275 | cpu_dai->probed = 1; | |
1c8371d6 | 1276 | /* mark cpu_dai as probed and add to card dai list */ |
f0fba2ad | 1277 | list_add(&cpu_dai->card_list, &card->dai_dev_list); |
db2a4165 FM |
1278 | } |
1279 | ||
f0fba2ad | 1280 | /* probe the CODEC */ |
0168bf0d LG |
1281 | if (!codec->probed && |
1282 | codec->driver->probe_order == order) { | |
589c3563 JN |
1283 | ret = soc_probe_codec(card, codec); |
1284 | if (ret < 0) | |
1285 | return ret; | |
db2a4165 FM |
1286 | } |
1287 | ||
f0fba2ad | 1288 | /* probe the platform */ |
0168bf0d LG |
1289 | if (!platform->probed && |
1290 | platform->driver->probe_order == order) { | |
956245e9 LG |
1291 | ret = soc_probe_platform(card, platform); |
1292 | if (ret < 0) | |
1293 | return ret; | |
f0fba2ad | 1294 | } |
6ed25978 | 1295 | |
f0fba2ad | 1296 | /* probe the CODEC DAI */ |
0168bf0d | 1297 | if (!codec_dai->probed && codec_dai->driver->probe_order == order) { |
f0fba2ad LG |
1298 | if (codec_dai->driver->probe) { |
1299 | ret = codec_dai->driver->probe(codec_dai); | |
fe3e78e0 | 1300 | if (ret < 0) { |
0837fc62 FE |
1301 | pr_err("asoc: failed to probe CODEC DAI %s: %d\n", |
1302 | codec_dai->name, ret); | |
f0fba2ad | 1303 | return ret; |
fe3e78e0 MB |
1304 | } |
1305 | } | |
f0fba2ad | 1306 | |
1c8371d6 | 1307 | /* mark codec_dai as probed and add to card dai list */ |
f0fba2ad LG |
1308 | codec_dai->probed = 1; |
1309 | list_add(&codec_dai->card_list, &card->dai_dev_list); | |
fe3e78e0 MB |
1310 | } |
1311 | ||
0168bf0d LG |
1312 | /* complete DAI probe during last probe */ |
1313 | if (order != SND_SOC_COMP_ORDER_LAST) | |
1314 | return 0; | |
1315 | ||
589c3563 JN |
1316 | ret = soc_post_component_init(card, codec, num, 0); |
1317 | if (ret) | |
f0fba2ad | 1318 | return ret; |
fe3e78e0 | 1319 | |
36ae1a96 | 1320 | ret = device_create_file(rtd->dev, &dev_attr_pmdown_time); |
f0fba2ad | 1321 | if (ret < 0) |
0837fc62 | 1322 | pr_warn("asoc: failed to add pmdown_time sysfs:%d\n", ret); |
f0fba2ad | 1323 | |
c74184ed MB |
1324 | if (!dai_link->params) { |
1325 | /* create the pcm */ | |
1326 | ret = soc_new_pcm(rtd, num); | |
1327 | if (ret < 0) { | |
1328 | pr_err("asoc: can't create pcm %s :%d\n", | |
1329 | dai_link->stream_name, ret); | |
1330 | return ret; | |
1331 | } | |
1332 | } else { | |
1333 | /* link the DAI widgets */ | |
1334 | play_w = codec_dai->playback_widget; | |
1335 | capture_w = cpu_dai->capture_widget; | |
1336 | if (play_w && capture_w) { | |
1337 | ret = snd_soc_dapm_new_pcm(card, dai_link->params, | |
1338 | capture_w, play_w); | |
1339 | if (ret != 0) { | |
1340 | dev_err(card->dev, "Can't link %s to %s: %d\n", | |
1341 | play_w->name, capture_w->name, ret); | |
1342 | return ret; | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | play_w = cpu_dai->playback_widget; | |
1347 | capture_w = codec_dai->capture_widget; | |
1348 | if (play_w && capture_w) { | |
1349 | ret = snd_soc_dapm_new_pcm(card, dai_link->params, | |
1350 | capture_w, play_w); | |
1351 | if (ret != 0) { | |
1352 | dev_err(card->dev, "Can't link %s to %s: %d\n", | |
1353 | play_w->name, capture_w->name, ret); | |
1354 | return ret; | |
1355 | } | |
1356 | } | |
f0fba2ad LG |
1357 | } |
1358 | ||
1359 | /* add platform data for AC97 devices */ | |
1360 | if (rtd->codec_dai->driver->ac97_control) | |
1361 | snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata); | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | ||
fe3e78e0 | 1366 | #ifdef CONFIG_SND_SOC_AC97_BUS |
f0fba2ad LG |
1367 | static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd) |
1368 | { | |
1369 | int ret; | |
1370 | ||
fe3e78e0 MB |
1371 | /* Only instantiate AC97 if not already done by the adaptor |
1372 | * for the generic AC97 subsystem. | |
1373 | */ | |
f0fba2ad | 1374 | if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) { |
0562f788 MW |
1375 | /* |
1376 | * It is possible that the AC97 device is already registered to | |
1377 | * the device subsystem. This happens when the device is created | |
1378 | * via snd_ac97_mixer(). Currently only SoC codec that does so | |
1379 | * is the generic AC97 glue but others migh emerge. | |
1380 | * | |
1381 | * In those cases we don't try to register the device again. | |
1382 | */ | |
1383 | if (!rtd->codec->ac97_created) | |
1384 | return 0; | |
f0fba2ad LG |
1385 | |
1386 | ret = soc_ac97_dev_register(rtd->codec); | |
fe3e78e0 | 1387 | if (ret < 0) { |
0837fc62 | 1388 | pr_err("asoc: AC97 device register failed:%d\n", ret); |
f0fba2ad | 1389 | return ret; |
fe3e78e0 | 1390 | } |
f0fba2ad LG |
1391 | |
1392 | rtd->codec->ac97_registered = 1; | |
fe3e78e0 | 1393 | } |
f0fba2ad LG |
1394 | return 0; |
1395 | } | |
1396 | ||
1397 | static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec) | |
1398 | { | |
1399 | if (codec->ac97_registered) { | |
1400 | soc_ac97_dev_unregister(codec); | |
1401 | codec->ac97_registered = 0; | |
1402 | } | |
1403 | } | |
fe3e78e0 MB |
1404 | #endif |
1405 | ||
b19e6e7b MB |
1406 | static int soc_check_aux_dev(struct snd_soc_card *card, int num) |
1407 | { | |
1408 | struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; | |
1409 | struct snd_soc_codec *codec; | |
1410 | ||
1411 | /* find CODEC from registered CODECs*/ | |
1412 | list_for_each_entry(codec, &codec_list, list) { | |
1413 | if (!strcmp(codec->name, aux_dev->codec_name)) | |
1414 | return 0; | |
1415 | } | |
1416 | ||
1417 | return -EPROBE_DEFER; | |
1418 | } | |
1419 | ||
2eea392d JN |
1420 | static int soc_probe_aux_dev(struct snd_soc_card *card, int num) |
1421 | { | |
1422 | struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; | |
2eea392d | 1423 | struct snd_soc_codec *codec; |
676ad98a | 1424 | int ret = -ENODEV; |
2eea392d JN |
1425 | |
1426 | /* find CODEC from registered CODECs*/ | |
1427 | list_for_each_entry(codec, &codec_list, list) { | |
1428 | if (!strcmp(codec->name, aux_dev->codec_name)) { | |
1429 | if (codec->probed) { | |
1430 | dev_err(codec->dev, | |
1431 | "asoc: codec already probed"); | |
1432 | ret = -EBUSY; | |
1433 | goto out; | |
1434 | } | |
676ad98a | 1435 | goto found; |
2eea392d JN |
1436 | } |
1437 | } | |
676ad98a JN |
1438 | /* codec not found */ |
1439 | dev_err(card->dev, "asoc: codec %s not found", aux_dev->codec_name); | |
b19e6e7b | 1440 | return -EPROBE_DEFER; |
2eea392d | 1441 | |
676ad98a | 1442 | found: |
589c3563 | 1443 | ret = soc_probe_codec(card, codec); |
2eea392d | 1444 | if (ret < 0) |
589c3563 | 1445 | return ret; |
2eea392d | 1446 | |
589c3563 | 1447 | ret = soc_post_component_init(card, codec, num, 1); |
2eea392d JN |
1448 | |
1449 | out: | |
1450 | return ret; | |
1451 | } | |
1452 | ||
1453 | static void soc_remove_aux_dev(struct snd_soc_card *card, int num) | |
1454 | { | |
1455 | struct snd_soc_pcm_runtime *rtd = &card->rtd_aux[num]; | |
1456 | struct snd_soc_codec *codec = rtd->codec; | |
2eea392d JN |
1457 | |
1458 | /* unregister the rtd device */ | |
1459 | if (rtd->dev_registered) { | |
36ae1a96 MB |
1460 | device_remove_file(rtd->dev, &dev_attr_codec_reg); |
1461 | device_del(rtd->dev); | |
2eea392d JN |
1462 | rtd->dev_registered = 0; |
1463 | } | |
1464 | ||
589c3563 JN |
1465 | if (codec && codec->probed) |
1466 | soc_remove_codec(codec); | |
2eea392d JN |
1467 | } |
1468 | ||
fdf0f54d DP |
1469 | static int snd_soc_init_codec_cache(struct snd_soc_codec *codec, |
1470 | enum snd_soc_compress_type compress_type) | |
1471 | { | |
1472 | int ret; | |
1473 | ||
1474 | if (codec->cache_init) | |
1475 | return 0; | |
1476 | ||
1477 | /* override the compress_type if necessary */ | |
1478 | if (compress_type && codec->compress_type != compress_type) | |
1479 | codec->compress_type = compress_type; | |
fdf0f54d DP |
1480 | ret = snd_soc_cache_init(codec); |
1481 | if (ret < 0) { | |
1482 | dev_err(codec->dev, "Failed to set cache compression type: %d\n", | |
1483 | ret); | |
1484 | return ret; | |
1485 | } | |
1486 | codec->cache_init = 1; | |
1487 | return 0; | |
1488 | } | |
1489 | ||
b19e6e7b | 1490 | static int snd_soc_instantiate_card(struct snd_soc_card *card) |
f0fba2ad | 1491 | { |
fdf0f54d DP |
1492 | struct snd_soc_codec *codec; |
1493 | struct snd_soc_codec_conf *codec_conf; | |
1494 | enum snd_soc_compress_type compress_type; | |
75d9ac46 | 1495 | struct snd_soc_dai_link *dai_link; |
f04209a7 | 1496 | int ret, i, order, dai_fmt; |
fe3e78e0 | 1497 | |
01b9d99a | 1498 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); |
dbe21408 | 1499 | |
f0fba2ad | 1500 | /* bind DAIs */ |
b19e6e7b MB |
1501 | for (i = 0; i < card->num_links; i++) { |
1502 | ret = soc_bind_dai_link(card, i); | |
1503 | if (ret != 0) | |
1504 | goto base_error; | |
1505 | } | |
fe3e78e0 | 1506 | |
b19e6e7b MB |
1507 | /* check aux_devs too */ |
1508 | for (i = 0; i < card->num_aux_devs; i++) { | |
1509 | ret = soc_check_aux_dev(card, i); | |
1510 | if (ret != 0) | |
1511 | goto base_error; | |
f0fba2ad | 1512 | } |
435c5e25 | 1513 | |
fdf0f54d DP |
1514 | /* initialize the register cache for each available codec */ |
1515 | list_for_each_entry(codec, &codec_list, list) { | |
1516 | if (codec->cache_init) | |
1517 | continue; | |
861f2faf DP |
1518 | /* by default we don't override the compress_type */ |
1519 | compress_type = 0; | |
fdf0f54d DP |
1520 | /* check to see if we need to override the compress_type */ |
1521 | for (i = 0; i < card->num_configs; ++i) { | |
1522 | codec_conf = &card->codec_conf[i]; | |
1523 | if (!strcmp(codec->name, codec_conf->dev_name)) { | |
1524 | compress_type = codec_conf->compress_type; | |
1525 | if (compress_type && compress_type | |
1526 | != codec->compress_type) | |
1527 | break; | |
1528 | } | |
1529 | } | |
fdf0f54d | 1530 | ret = snd_soc_init_codec_cache(codec, compress_type); |
b19e6e7b MB |
1531 | if (ret < 0) |
1532 | goto base_error; | |
fdf0f54d DP |
1533 | } |
1534 | ||
f0fba2ad LG |
1535 | /* card bind complete so register a sound card */ |
1536 | ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, | |
1537 | card->owner, 0, &card->snd_card); | |
1538 | if (ret < 0) { | |
0837fc62 FE |
1539 | pr_err("asoc: can't create sound card for card %s: %d\n", |
1540 | card->name, ret); | |
b19e6e7b | 1541 | goto base_error; |
f0fba2ad LG |
1542 | } |
1543 | card->snd_card->dev = card->dev; | |
1544 | ||
e37a4970 MB |
1545 | card->dapm.bias_level = SND_SOC_BIAS_OFF; |
1546 | card->dapm.dev = card->dev; | |
1547 | card->dapm.card = card; | |
1548 | list_add(&card->dapm.list, &card->dapm_list); | |
1549 | ||
d5d1e0be LPC |
1550 | #ifdef CONFIG_DEBUG_FS |
1551 | snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); | |
1552 | #endif | |
1553 | ||
88ee1c61 | 1554 | #ifdef CONFIG_PM_SLEEP |
f0fba2ad LG |
1555 | /* deferred resume work */ |
1556 | INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); | |
1557 | #endif | |
db2a4165 | 1558 | |
9a841ebb MB |
1559 | if (card->dapm_widgets) |
1560 | snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, | |
1561 | card->num_dapm_widgets); | |
1562 | ||
f0fba2ad LG |
1563 | /* initialise the sound card only once */ |
1564 | if (card->probe) { | |
e7361ec4 | 1565 | ret = card->probe(card); |
f0fba2ad LG |
1566 | if (ret < 0) |
1567 | goto card_probe_error; | |
1568 | } | |
fe3e78e0 | 1569 | |
0168bf0d LG |
1570 | /* early DAI link probe */ |
1571 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; | |
1572 | order++) { | |
1573 | for (i = 0; i < card->num_links; i++) { | |
1574 | ret = soc_probe_dai_link(card, i, order); | |
1575 | if (ret < 0) { | |
1576 | pr_err("asoc: failed to instantiate card %s: %d\n", | |
321de0d0 | 1577 | card->name, ret); |
0168bf0d LG |
1578 | goto probe_dai_err; |
1579 | } | |
f0fba2ad LG |
1580 | } |
1581 | } | |
db2a4165 | 1582 | |
2eea392d JN |
1583 | for (i = 0; i < card->num_aux_devs; i++) { |
1584 | ret = soc_probe_aux_dev(card, i); | |
1585 | if (ret < 0) { | |
1586 | pr_err("asoc: failed to add auxiliary devices %s: %d\n", | |
1587 | card->name, ret); | |
1588 | goto probe_aux_dev_err; | |
1589 | } | |
1590 | } | |
1591 | ||
888df395 MB |
1592 | snd_soc_dapm_link_dai_widgets(card); |
1593 | ||
b7af1daf | 1594 | if (card->controls) |
022658be | 1595 | snd_soc_add_card_controls(card, card->controls, card->num_controls); |
b7af1daf | 1596 | |
b8ad29de MB |
1597 | if (card->dapm_routes) |
1598 | snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, | |
1599 | card->num_dapm_routes); | |
1600 | ||
b90d2f90 MB |
1601 | snd_soc_dapm_new_widgets(&card->dapm); |
1602 | ||
75d9ac46 MB |
1603 | for (i = 0; i < card->num_links; i++) { |
1604 | dai_link = &card->dai_link[i]; | |
f04209a7 | 1605 | dai_fmt = dai_link->dai_fmt; |
75d9ac46 | 1606 | |
f04209a7 | 1607 | if (dai_fmt) { |
75d9ac46 | 1608 | ret = snd_soc_dai_set_fmt(card->rtd[i].codec_dai, |
f04209a7 | 1609 | dai_fmt); |
5e4ba569 | 1610 | if (ret != 0 && ret != -ENOTSUPP) |
75d9ac46 MB |
1611 | dev_warn(card->rtd[i].codec_dai->dev, |
1612 | "Failed to set DAI format: %d\n", | |
1613 | ret); | |
f04209a7 MB |
1614 | } |
1615 | ||
1616 | /* If this is a regular CPU link there will be a platform */ | |
fe33d4c5 SW |
1617 | if (dai_fmt && |
1618 | (dai_link->platform_name || dai_link->platform_of_node)) { | |
f04209a7 MB |
1619 | ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, |
1620 | dai_fmt); | |
1621 | if (ret != 0 && ret != -ENOTSUPP) | |
1622 | dev_warn(card->rtd[i].cpu_dai->dev, | |
1623 | "Failed to set DAI format: %d\n", | |
1624 | ret); | |
1625 | } else if (dai_fmt) { | |
1626 | /* Flip the polarity for the "CPU" end */ | |
1627 | dai_fmt &= ~SND_SOC_DAIFMT_MASTER_MASK; | |
1628 | switch (dai_link->dai_fmt & | |
1629 | SND_SOC_DAIFMT_MASTER_MASK) { | |
1630 | case SND_SOC_DAIFMT_CBM_CFM: | |
1631 | dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; | |
1632 | break; | |
1633 | case SND_SOC_DAIFMT_CBM_CFS: | |
1634 | dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; | |
1635 | break; | |
1636 | case SND_SOC_DAIFMT_CBS_CFM: | |
1637 | dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; | |
1638 | break; | |
1639 | case SND_SOC_DAIFMT_CBS_CFS: | |
1640 | dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; | |
1641 | break; | |
1642 | } | |
75d9ac46 MB |
1643 | |
1644 | ret = snd_soc_dai_set_fmt(card->rtd[i].cpu_dai, | |
f04209a7 | 1645 | dai_fmt); |
5e4ba569 | 1646 | if (ret != 0 && ret != -ENOTSUPP) |
75d9ac46 MB |
1647 | dev_warn(card->rtd[i].cpu_dai->dev, |
1648 | "Failed to set DAI format: %d\n", | |
1649 | ret); | |
1650 | } | |
1651 | } | |
1652 | ||
f0fba2ad | 1653 | snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), |
f0fba2ad | 1654 | "%s", card->name); |
22de71ba LG |
1655 | snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), |
1656 | "%s", card->long_name ? card->long_name : card->name); | |
f0e8ed85 MB |
1657 | snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), |
1658 | "%s", card->driver_name ? card->driver_name : card->name); | |
1659 | for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { | |
1660 | switch (card->snd_card->driver[i]) { | |
1661 | case '_': | |
1662 | case '-': | |
1663 | case '\0': | |
1664 | break; | |
1665 | default: | |
1666 | if (!isalnum(card->snd_card->driver[i])) | |
1667 | card->snd_card->driver[i] = '_'; | |
1668 | break; | |
1669 | } | |
1670 | } | |
f0fba2ad | 1671 | |
28e9ad92 MB |
1672 | if (card->late_probe) { |
1673 | ret = card->late_probe(card); | |
1674 | if (ret < 0) { | |
1675 | dev_err(card->dev, "%s late_probe() failed: %d\n", | |
1676 | card->name, ret); | |
1677 | goto probe_aux_dev_err; | |
1678 | } | |
1679 | } | |
1680 | ||
2dc00213 MB |
1681 | snd_soc_dapm_new_widgets(&card->dapm); |
1682 | ||
1633281b | 1683 | if (card->fully_routed) |
b05d8dc1 | 1684 | list_for_each_entry(codec, &card->codec_dev_list, card_list) |
1633281b SW |
1685 | snd_soc_dapm_auto_nc_codec_pins(codec); |
1686 | ||
f0fba2ad LG |
1687 | ret = snd_card_register(card->snd_card); |
1688 | if (ret < 0) { | |
0837fc62 FE |
1689 | pr_err("asoc: failed to register soundcard for %s: %d\n", |
1690 | card->name, ret); | |
6b3ed785 | 1691 | goto probe_aux_dev_err; |
db2a4165 FM |
1692 | } |
1693 | ||
f0fba2ad LG |
1694 | #ifdef CONFIG_SND_SOC_AC97_BUS |
1695 | /* register any AC97 codecs */ | |
1696 | for (i = 0; i < card->num_rtd; i++) { | |
6b3ed785 AL |
1697 | ret = soc_register_ac97_dai_link(&card->rtd[i]); |
1698 | if (ret < 0) { | |
0837fc62 FE |
1699 | pr_err("asoc: failed to register AC97 %s: %d\n", |
1700 | card->name, ret); | |
6b3ed785 | 1701 | while (--i >= 0) |
7d8316df | 1702 | soc_unregister_ac97_dai_link(card->rtd[i].codec); |
6b3ed785 | 1703 | goto probe_aux_dev_err; |
f0fba2ad | 1704 | } |
6b3ed785 | 1705 | } |
f0fba2ad LG |
1706 | #endif |
1707 | ||
1708 | card->instantiated = 1; | |
4f4c0072 | 1709 | snd_soc_dapm_sync(&card->dapm); |
f0fba2ad | 1710 | mutex_unlock(&card->mutex); |
b19e6e7b MB |
1711 | |
1712 | return 0; | |
f0fba2ad | 1713 | |
2eea392d JN |
1714 | probe_aux_dev_err: |
1715 | for (i = 0; i < card->num_aux_devs; i++) | |
1716 | soc_remove_aux_dev(card, i); | |
1717 | ||
f0fba2ad | 1718 | probe_dai_err: |
0671fd8e | 1719 | soc_remove_dai_links(card); |
f0fba2ad LG |
1720 | |
1721 | card_probe_error: | |
87506549 | 1722 | if (card->remove) |
e7361ec4 | 1723 | card->remove(card); |
f0fba2ad LG |
1724 | |
1725 | snd_card_free(card->snd_card); | |
1726 | ||
b19e6e7b | 1727 | base_error: |
f0fba2ad | 1728 | mutex_unlock(&card->mutex); |
db2a4165 | 1729 | |
b19e6e7b | 1730 | return ret; |
435c5e25 MB |
1731 | } |
1732 | ||
1733 | /* probes a new socdev */ | |
1734 | static int soc_probe(struct platform_device *pdev) | |
1735 | { | |
f0fba2ad | 1736 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
435c5e25 | 1737 | int ret = 0; |
435c5e25 | 1738 | |
70a7ca34 VK |
1739 | /* |
1740 | * no card, so machine driver should be registering card | |
1741 | * we should not be here in that case so ret error | |
1742 | */ | |
1743 | if (!card) | |
1744 | return -EINVAL; | |
1745 | ||
fe4085e8 MB |
1746 | dev_warn(&pdev->dev, |
1747 | "ASoC machine %s should use snd_soc_register_card()\n", | |
1748 | card->name); | |
1749 | ||
435c5e25 MB |
1750 | /* Bodge while we unpick instantiation */ |
1751 | card->dev = &pdev->dev; | |
f0fba2ad | 1752 | |
435c5e25 MB |
1753 | ret = snd_soc_register_card(card); |
1754 | if (ret != 0) { | |
1755 | dev_err(&pdev->dev, "Failed to register card\n"); | |
1756 | return ret; | |
1757 | } | |
1758 | ||
1759 | return 0; | |
db2a4165 FM |
1760 | } |
1761 | ||
b0e26485 | 1762 | static int soc_cleanup_card_resources(struct snd_soc_card *card) |
db2a4165 FM |
1763 | { |
1764 | int i; | |
db2a4165 | 1765 | |
b0e26485 VK |
1766 | /* make sure any delayed work runs */ |
1767 | for (i = 0; i < card->num_rtd; i++) { | |
1768 | struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; | |
1769 | flush_delayed_work_sync(&rtd->delayed_work); | |
1770 | } | |
914dc182 | 1771 | |
b0e26485 VK |
1772 | /* remove auxiliary devices */ |
1773 | for (i = 0; i < card->num_aux_devs; i++) | |
1774 | soc_remove_aux_dev(card, i); | |
db2a4165 | 1775 | |
b0e26485 | 1776 | /* remove and free each DAI */ |
0671fd8e | 1777 | soc_remove_dai_links(card); |
2eea392d | 1778 | |
b0e26485 | 1779 | soc_cleanup_card_debugfs(card); |
f0fba2ad | 1780 | |
b0e26485 VK |
1781 | /* remove the card */ |
1782 | if (card->remove) | |
e7361ec4 | 1783 | card->remove(card); |
a6052154 | 1784 | |
0aaae527 LPC |
1785 | snd_soc_dapm_free(&card->dapm); |
1786 | ||
b0e26485 VK |
1787 | snd_card_free(card->snd_card); |
1788 | return 0; | |
1789 | ||
1790 | } | |
1791 | ||
1792 | /* removes a socdev */ | |
1793 | static int soc_remove(struct platform_device *pdev) | |
1794 | { | |
1795 | struct snd_soc_card *card = platform_get_drvdata(pdev); | |
db2a4165 | 1796 | |
c5af3a2e | 1797 | snd_soc_unregister_card(card); |
db2a4165 FM |
1798 | return 0; |
1799 | } | |
1800 | ||
6f8ab4ac | 1801 | int snd_soc_poweroff(struct device *dev) |
51737470 | 1802 | { |
6f8ab4ac | 1803 | struct snd_soc_card *card = dev_get_drvdata(dev); |
f0fba2ad | 1804 | int i; |
51737470 MB |
1805 | |
1806 | if (!card->instantiated) | |
416356fc | 1807 | return 0; |
51737470 MB |
1808 | |
1809 | /* Flush out pmdown_time work - we actually do want to run it | |
1810 | * now, we're shutting down so no imminent restart. */ | |
f0fba2ad LG |
1811 | for (i = 0; i < card->num_rtd; i++) { |
1812 | struct snd_soc_pcm_runtime *rtd = &card->rtd[i]; | |
5b84ba26 | 1813 | flush_delayed_work_sync(&rtd->delayed_work); |
f0fba2ad | 1814 | } |
51737470 | 1815 | |
f0fba2ad | 1816 | snd_soc_dapm_shutdown(card); |
416356fc MB |
1817 | |
1818 | return 0; | |
51737470 | 1819 | } |
6f8ab4ac | 1820 | EXPORT_SYMBOL_GPL(snd_soc_poweroff); |
51737470 | 1821 | |
6f8ab4ac | 1822 | const struct dev_pm_ops snd_soc_pm_ops = { |
b1dd5897 VK |
1823 | .suspend = snd_soc_suspend, |
1824 | .resume = snd_soc_resume, | |
1825 | .freeze = snd_soc_suspend, | |
1826 | .thaw = snd_soc_resume, | |
6f8ab4ac | 1827 | .poweroff = snd_soc_poweroff, |
b1dd5897 | 1828 | .restore = snd_soc_resume, |
416356fc | 1829 | }; |
deb2607e | 1830 | EXPORT_SYMBOL_GPL(snd_soc_pm_ops); |
416356fc | 1831 | |
db2a4165 FM |
1832 | /* ASoC platform driver */ |
1833 | static struct platform_driver soc_driver = { | |
1834 | .driver = { | |
1835 | .name = "soc-audio", | |
8b45a209 | 1836 | .owner = THIS_MODULE, |
6f8ab4ac | 1837 | .pm = &snd_soc_pm_ops, |
db2a4165 FM |
1838 | }, |
1839 | .probe = soc_probe, | |
1840 | .remove = soc_remove, | |
db2a4165 FM |
1841 | }; |
1842 | ||
096e49d5 MB |
1843 | /** |
1844 | * snd_soc_codec_volatile_register: Report if a register is volatile. | |
1845 | * | |
1846 | * @codec: CODEC to query. | |
1847 | * @reg: Register to query. | |
1848 | * | |
1849 | * Boolean function indiciating if a CODEC register is volatile. | |
1850 | */ | |
181e055e MB |
1851 | int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, |
1852 | unsigned int reg) | |
096e49d5 | 1853 | { |
1500b7b5 DP |
1854 | if (codec->volatile_register) |
1855 | return codec->volatile_register(codec, reg); | |
096e49d5 MB |
1856 | else |
1857 | return 0; | |
1858 | } | |
1859 | EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register); | |
1860 | ||
239c9706 DP |
1861 | /** |
1862 | * snd_soc_codec_readable_register: Report if a register is readable. | |
1863 | * | |
1864 | * @codec: CODEC to query. | |
1865 | * @reg: Register to query. | |
1866 | * | |
1867 | * Boolean function indicating if a CODEC register is readable. | |
1868 | */ | |
1869 | int snd_soc_codec_readable_register(struct snd_soc_codec *codec, | |
1870 | unsigned int reg) | |
1871 | { | |
1872 | if (codec->readable_register) | |
1873 | return codec->readable_register(codec, reg); | |
1874 | else | |
63fa0a28 | 1875 | return 1; |
239c9706 DP |
1876 | } |
1877 | EXPORT_SYMBOL_GPL(snd_soc_codec_readable_register); | |
1878 | ||
1879 | /** | |
1880 | * snd_soc_codec_writable_register: Report if a register is writable. | |
1881 | * | |
1882 | * @codec: CODEC to query. | |
1883 | * @reg: Register to query. | |
1884 | * | |
1885 | * Boolean function indicating if a CODEC register is writable. | |
1886 | */ | |
1887 | int snd_soc_codec_writable_register(struct snd_soc_codec *codec, | |
1888 | unsigned int reg) | |
1889 | { | |
1890 | if (codec->writable_register) | |
1891 | return codec->writable_register(codec, reg); | |
1892 | else | |
63fa0a28 | 1893 | return 1; |
239c9706 DP |
1894 | } |
1895 | EXPORT_SYMBOL_GPL(snd_soc_codec_writable_register); | |
1896 | ||
f1442bc1 LG |
1897 | int snd_soc_platform_read(struct snd_soc_platform *platform, |
1898 | unsigned int reg) | |
1899 | { | |
1900 | unsigned int ret; | |
1901 | ||
1902 | if (!platform->driver->read) { | |
1903 | dev_err(platform->dev, "platform has no read back\n"); | |
1904 | return -1; | |
1905 | } | |
1906 | ||
1907 | ret = platform->driver->read(platform, reg); | |
1908 | dev_dbg(platform->dev, "read %x => %x\n", reg, ret); | |
a82ce2ae | 1909 | trace_snd_soc_preg_read(platform, reg, ret); |
f1442bc1 LG |
1910 | |
1911 | return ret; | |
1912 | } | |
1913 | EXPORT_SYMBOL_GPL(snd_soc_platform_read); | |
1914 | ||
1915 | int snd_soc_platform_write(struct snd_soc_platform *platform, | |
1916 | unsigned int reg, unsigned int val) | |
1917 | { | |
1918 | if (!platform->driver->write) { | |
1919 | dev_err(platform->dev, "platform has no write back\n"); | |
1920 | return -1; | |
1921 | } | |
1922 | ||
1923 | dev_dbg(platform->dev, "write %x = %x\n", reg, val); | |
a82ce2ae | 1924 | trace_snd_soc_preg_write(platform, reg, val); |
f1442bc1 LG |
1925 | return platform->driver->write(platform, reg, val); |
1926 | } | |
1927 | EXPORT_SYMBOL_GPL(snd_soc_platform_write); | |
1928 | ||
db2a4165 FM |
1929 | /** |
1930 | * snd_soc_new_ac97_codec - initailise AC97 device | |
1931 | * @codec: audio codec | |
1932 | * @ops: AC97 bus operations | |
1933 | * @num: AC97 codec number | |
1934 | * | |
1935 | * Initialises AC97 codec resources for use by ad-hoc devices only. | |
1936 | */ | |
1937 | int snd_soc_new_ac97_codec(struct snd_soc_codec *codec, | |
1938 | struct snd_ac97_bus_ops *ops, int num) | |
1939 | { | |
1940 | mutex_lock(&codec->mutex); | |
1941 | ||
1942 | codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL); | |
1943 | if (codec->ac97 == NULL) { | |
1944 | mutex_unlock(&codec->mutex); | |
1945 | return -ENOMEM; | |
1946 | } | |
1947 | ||
1948 | codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL); | |
1949 | if (codec->ac97->bus == NULL) { | |
1950 | kfree(codec->ac97); | |
1951 | codec->ac97 = NULL; | |
1952 | mutex_unlock(&codec->mutex); | |
1953 | return -ENOMEM; | |
1954 | } | |
1955 | ||
1956 | codec->ac97->bus->ops = ops; | |
1957 | codec->ac97->num = num; | |
0562f788 MW |
1958 | |
1959 | /* | |
1960 | * Mark the AC97 device to be created by us. This way we ensure that the | |
1961 | * device will be registered with the device subsystem later on. | |
1962 | */ | |
1963 | codec->ac97_created = 1; | |
1964 | ||
db2a4165 FM |
1965 | mutex_unlock(&codec->mutex); |
1966 | return 0; | |
1967 | } | |
1968 | EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec); | |
1969 | ||
1970 | /** | |
1971 | * snd_soc_free_ac97_codec - free AC97 codec device | |
1972 | * @codec: audio codec | |
1973 | * | |
1974 | * Frees AC97 codec device resources. | |
1975 | */ | |
1976 | void snd_soc_free_ac97_codec(struct snd_soc_codec *codec) | |
1977 | { | |
1978 | mutex_lock(&codec->mutex); | |
f0fba2ad LG |
1979 | #ifdef CONFIG_SND_SOC_AC97_BUS |
1980 | soc_unregister_ac97_dai_link(codec); | |
1981 | #endif | |
db2a4165 FM |
1982 | kfree(codec->ac97->bus); |
1983 | kfree(codec->ac97); | |
1984 | codec->ac97 = NULL; | |
0562f788 | 1985 | codec->ac97_created = 0; |
db2a4165 FM |
1986 | mutex_unlock(&codec->mutex); |
1987 | } | |
1988 | EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec); | |
1989 | ||
c3753707 MB |
1990 | unsigned int snd_soc_read(struct snd_soc_codec *codec, unsigned int reg) |
1991 | { | |
1992 | unsigned int ret; | |
1993 | ||
c3acec26 | 1994 | ret = codec->read(codec, reg); |
c3753707 | 1995 | dev_dbg(codec->dev, "read %x => %x\n", reg, ret); |
a8b1d34f | 1996 | trace_snd_soc_reg_read(codec, reg, ret); |
c3753707 MB |
1997 | |
1998 | return ret; | |
1999 | } | |
2000 | EXPORT_SYMBOL_GPL(snd_soc_read); | |
2001 | ||
2002 | unsigned int snd_soc_write(struct snd_soc_codec *codec, | |
2003 | unsigned int reg, unsigned int val) | |
2004 | { | |
2005 | dev_dbg(codec->dev, "write %x = %x\n", reg, val); | |
a8b1d34f | 2006 | trace_snd_soc_reg_write(codec, reg, val); |
c3acec26 | 2007 | return codec->write(codec, reg, val); |
c3753707 MB |
2008 | } |
2009 | EXPORT_SYMBOL_GPL(snd_soc_write); | |
2010 | ||
5fb609d4 DP |
2011 | unsigned int snd_soc_bulk_write_raw(struct snd_soc_codec *codec, |
2012 | unsigned int reg, const void *data, size_t len) | |
2013 | { | |
2014 | return codec->bulk_write_raw(codec, reg, data, len); | |
2015 | } | |
2016 | EXPORT_SYMBOL_GPL(snd_soc_bulk_write_raw); | |
2017 | ||
db2a4165 FM |
2018 | /** |
2019 | * snd_soc_update_bits - update codec register bits | |
2020 | * @codec: audio codec | |
2021 | * @reg: codec register | |
2022 | * @mask: register mask | |
2023 | * @value: new value | |
2024 | * | |
2025 | * Writes new register value. | |
2026 | * | |
180c329d | 2027 | * Returns 1 for change, 0 for no change, or negative error code. |
db2a4165 FM |
2028 | */ |
2029 | int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg, | |
46f5822f | 2030 | unsigned int mask, unsigned int value) |
db2a4165 | 2031 | { |
8a713da8 | 2032 | bool change; |
46f5822f | 2033 | unsigned int old, new; |
180c329d TT |
2034 | int ret; |
2035 | ||
8a713da8 MB |
2036 | if (codec->using_regmap) { |
2037 | ret = regmap_update_bits_check(codec->control_data, reg, | |
2038 | mask, value, &change); | |
2039 | } else { | |
2040 | ret = snd_soc_read(codec, reg); | |
180c329d TT |
2041 | if (ret < 0) |
2042 | return ret; | |
8a713da8 MB |
2043 | |
2044 | old = ret; | |
2045 | new = (old & ~mask) | (value & mask); | |
2046 | change = old != new; | |
2047 | if (change) | |
2048 | ret = snd_soc_write(codec, reg, new); | |
180c329d | 2049 | } |
db2a4165 | 2050 | |
8a713da8 MB |
2051 | if (ret < 0) |
2052 | return ret; | |
2053 | ||
db2a4165 FM |
2054 | return change; |
2055 | } | |
2056 | EXPORT_SYMBOL_GPL(snd_soc_update_bits); | |
2057 | ||
6c508c62 EN |
2058 | /** |
2059 | * snd_soc_update_bits_locked - update codec register bits | |
2060 | * @codec: audio codec | |
2061 | * @reg: codec register | |
2062 | * @mask: register mask | |
2063 | * @value: new value | |
2064 | * | |
2065 | * Writes new register value, and takes the codec mutex. | |
2066 | * | |
2067 | * Returns 1 for change else 0. | |
2068 | */ | |
dd1b3d53 MB |
2069 | int snd_soc_update_bits_locked(struct snd_soc_codec *codec, |
2070 | unsigned short reg, unsigned int mask, | |
2071 | unsigned int value) | |
6c508c62 EN |
2072 | { |
2073 | int change; | |
2074 | ||
2075 | mutex_lock(&codec->mutex); | |
2076 | change = snd_soc_update_bits(codec, reg, mask, value); | |
2077 | mutex_unlock(&codec->mutex); | |
2078 | ||
2079 | return change; | |
2080 | } | |
dd1b3d53 | 2081 | EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked); |
6c508c62 | 2082 | |
db2a4165 FM |
2083 | /** |
2084 | * snd_soc_test_bits - test register for change | |
2085 | * @codec: audio codec | |
2086 | * @reg: codec register | |
2087 | * @mask: register mask | |
2088 | * @value: new value | |
2089 | * | |
2090 | * Tests a register with a new value and checks if the new value is | |
2091 | * different from the old value. | |
2092 | * | |
2093 | * Returns 1 for change else 0. | |
2094 | */ | |
2095 | int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg, | |
46f5822f | 2096 | unsigned int mask, unsigned int value) |
db2a4165 FM |
2097 | { |
2098 | int change; | |
46f5822f | 2099 | unsigned int old, new; |
db2a4165 | 2100 | |
db2a4165 FM |
2101 | old = snd_soc_read(codec, reg); |
2102 | new = (old & ~mask) | value; | |
2103 | change = old != new; | |
db2a4165 FM |
2104 | |
2105 | return change; | |
2106 | } | |
2107 | EXPORT_SYMBOL_GPL(snd_soc_test_bits); | |
2108 | ||
db2a4165 FM |
2109 | /** |
2110 | * snd_soc_set_runtime_hwparams - set the runtime hardware parameters | |
2111 | * @substream: the pcm substream | |
2112 | * @hw: the hardware parameters | |
2113 | * | |
2114 | * Sets the substream runtime hardware parameters. | |
2115 | */ | |
2116 | int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream, | |
2117 | const struct snd_pcm_hardware *hw) | |
2118 | { | |
2119 | struct snd_pcm_runtime *runtime = substream->runtime; | |
2120 | runtime->hw.info = hw->info; | |
2121 | runtime->hw.formats = hw->formats; | |
2122 | runtime->hw.period_bytes_min = hw->period_bytes_min; | |
2123 | runtime->hw.period_bytes_max = hw->period_bytes_max; | |
2124 | runtime->hw.periods_min = hw->periods_min; | |
2125 | runtime->hw.periods_max = hw->periods_max; | |
2126 | runtime->hw.buffer_bytes_max = hw->buffer_bytes_max; | |
2127 | runtime->hw.fifo_size = hw->fifo_size; | |
2128 | return 0; | |
2129 | } | |
2130 | EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams); | |
2131 | ||
2132 | /** | |
2133 | * snd_soc_cnew - create new control | |
2134 | * @_template: control template | |
2135 | * @data: control private data | |
ac11a2b3 | 2136 | * @long_name: control long name |
efb7ac3f | 2137 | * @prefix: control name prefix |
db2a4165 FM |
2138 | * |
2139 | * Create a new mixer control from a template control. | |
2140 | * | |
2141 | * Returns 0 for success, else error. | |
2142 | */ | |
2143 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, | |
3056557f | 2144 | void *data, const char *long_name, |
efb7ac3f | 2145 | const char *prefix) |
db2a4165 FM |
2146 | { |
2147 | struct snd_kcontrol_new template; | |
efb7ac3f MB |
2148 | struct snd_kcontrol *kcontrol; |
2149 | char *name = NULL; | |
2150 | int name_len; | |
db2a4165 FM |
2151 | |
2152 | memcpy(&template, _template, sizeof(template)); | |
db2a4165 FM |
2153 | template.index = 0; |
2154 | ||
efb7ac3f MB |
2155 | if (!long_name) |
2156 | long_name = template.name; | |
2157 | ||
2158 | if (prefix) { | |
2159 | name_len = strlen(long_name) + strlen(prefix) + 2; | |
57cf9d45 | 2160 | name = kmalloc(name_len, GFP_KERNEL); |
efb7ac3f MB |
2161 | if (!name) |
2162 | return NULL; | |
2163 | ||
2164 | snprintf(name, name_len, "%s %s", prefix, long_name); | |
2165 | ||
2166 | template.name = name; | |
2167 | } else { | |
2168 | template.name = long_name; | |
2169 | } | |
2170 | ||
2171 | kcontrol = snd_ctl_new1(&template, data); | |
2172 | ||
2173 | kfree(name); | |
2174 | ||
2175 | return kcontrol; | |
db2a4165 FM |
2176 | } |
2177 | EXPORT_SYMBOL_GPL(snd_soc_cnew); | |
2178 | ||
022658be LG |
2179 | static int snd_soc_add_controls(struct snd_card *card, struct device *dev, |
2180 | const struct snd_kcontrol_new *controls, int num_controls, | |
2181 | const char *prefix, void *data) | |
2182 | { | |
2183 | int err, i; | |
2184 | ||
2185 | for (i = 0; i < num_controls; i++) { | |
2186 | const struct snd_kcontrol_new *control = &controls[i]; | |
2187 | err = snd_ctl_add(card, snd_soc_cnew(control, data, | |
2188 | control->name, prefix)); | |
2189 | if (err < 0) { | |
2190 | dev_err(dev, "Failed to add %s: %d\n", control->name, err); | |
2191 | return err; | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | return 0; | |
2196 | } | |
2197 | ||
3e8e1952 | 2198 | /** |
022658be LG |
2199 | * snd_soc_add_codec_controls - add an array of controls to a codec. |
2200 | * Convenience function to add a list of controls. Many codecs were | |
3e8e1952 IM |
2201 | * duplicating this code. |
2202 | * | |
2203 | * @codec: codec to add controls to | |
2204 | * @controls: array of controls to add | |
2205 | * @num_controls: number of elements in the array | |
2206 | * | |
2207 | * Return 0 for success, else error. | |
2208 | */ | |
022658be | 2209 | int snd_soc_add_codec_controls(struct snd_soc_codec *codec, |
3e8e1952 IM |
2210 | const struct snd_kcontrol_new *controls, int num_controls) |
2211 | { | |
f0fba2ad | 2212 | struct snd_card *card = codec->card->snd_card; |
3e8e1952 | 2213 | |
022658be LG |
2214 | return snd_soc_add_controls(card, codec->dev, controls, num_controls, |
2215 | codec->name_prefix, codec); | |
3e8e1952 | 2216 | } |
022658be | 2217 | EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); |
3e8e1952 | 2218 | |
a491a5c8 LG |
2219 | /** |
2220 | * snd_soc_add_platform_controls - add an array of controls to a platform. | |
022658be | 2221 | * Convenience function to add a list of controls. |
a491a5c8 LG |
2222 | * |
2223 | * @platform: platform to add controls to | |
2224 | * @controls: array of controls to add | |
2225 | * @num_controls: number of elements in the array | |
2226 | * | |
2227 | * Return 0 for success, else error. | |
2228 | */ | |
2229 | int snd_soc_add_platform_controls(struct snd_soc_platform *platform, | |
2230 | const struct snd_kcontrol_new *controls, int num_controls) | |
2231 | { | |
2232 | struct snd_card *card = platform->card->snd_card; | |
a491a5c8 | 2233 | |
022658be LG |
2234 | return snd_soc_add_controls(card, platform->dev, controls, num_controls, |
2235 | NULL, platform); | |
a491a5c8 LG |
2236 | } |
2237 | EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); | |
2238 | ||
022658be LG |
2239 | /** |
2240 | * snd_soc_add_card_controls - add an array of controls to a SoC card. | |
2241 | * Convenience function to add a list of controls. | |
2242 | * | |
2243 | * @soc_card: SoC card to add controls to | |
2244 | * @controls: array of controls to add | |
2245 | * @num_controls: number of elements in the array | |
2246 | * | |
2247 | * Return 0 for success, else error. | |
2248 | */ | |
2249 | int snd_soc_add_card_controls(struct snd_soc_card *soc_card, | |
2250 | const struct snd_kcontrol_new *controls, int num_controls) | |
2251 | { | |
2252 | struct snd_card *card = soc_card->snd_card; | |
2253 | ||
2254 | return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, | |
2255 | NULL, soc_card); | |
2256 | } | |
2257 | EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); | |
2258 | ||
2259 | /** | |
2260 | * snd_soc_add_dai_controls - add an array of controls to a DAI. | |
2261 | * Convienience function to add a list of controls. | |
2262 | * | |
2263 | * @dai: DAI to add controls to | |
2264 | * @controls: array of controls to add | |
2265 | * @num_controls: number of elements in the array | |
2266 | * | |
2267 | * Return 0 for success, else error. | |
2268 | */ | |
2269 | int snd_soc_add_dai_controls(struct snd_soc_dai *dai, | |
2270 | const struct snd_kcontrol_new *controls, int num_controls) | |
2271 | { | |
2272 | struct snd_card *card = dai->card->snd_card; | |
2273 | ||
2274 | return snd_soc_add_controls(card, dai->dev, controls, num_controls, | |
2275 | NULL, dai); | |
2276 | } | |
2277 | EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); | |
2278 | ||
db2a4165 FM |
2279 | /** |
2280 | * snd_soc_info_enum_double - enumerated double mixer info callback | |
2281 | * @kcontrol: mixer control | |
2282 | * @uinfo: control element information | |
2283 | * | |
2284 | * Callback to provide information about a double enumerated | |
2285 | * mixer control. | |
2286 | * | |
2287 | * Returns 0 for success. | |
2288 | */ | |
2289 | int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol, | |
2290 | struct snd_ctl_elem_info *uinfo) | |
2291 | { | |
2292 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
2293 | ||
2294 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
2295 | uinfo->count = e->shift_l == e->shift_r ? 1 : 2; | |
f8ba0b7b | 2296 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 2297 | |
f8ba0b7b JS |
2298 | if (uinfo->value.enumerated.item > e->max - 1) |
2299 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
2300 | strcpy(uinfo->value.enumerated.name, |
2301 | e->texts[uinfo->value.enumerated.item]); | |
2302 | return 0; | |
2303 | } | |
2304 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_double); | |
2305 | ||
2306 | /** | |
2307 | * snd_soc_get_enum_double - enumerated double mixer get callback | |
2308 | * @kcontrol: mixer control | |
ac11a2b3 | 2309 | * @ucontrol: control element information |
db2a4165 FM |
2310 | * |
2311 | * Callback to get the value of a double enumerated mixer. | |
2312 | * | |
2313 | * Returns 0 for success. | |
2314 | */ | |
2315 | int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol, | |
2316 | struct snd_ctl_elem_value *ucontrol) | |
2317 | { | |
2318 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2319 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
46f5822f | 2320 | unsigned int val, bitmask; |
db2a4165 | 2321 | |
f8ba0b7b | 2322 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 FM |
2323 | ; |
2324 | val = snd_soc_read(codec, e->reg); | |
3ff3f64b MB |
2325 | ucontrol->value.enumerated.item[0] |
2326 | = (val >> e->shift_l) & (bitmask - 1); | |
db2a4165 FM |
2327 | if (e->shift_l != e->shift_r) |
2328 | ucontrol->value.enumerated.item[1] = | |
2329 | (val >> e->shift_r) & (bitmask - 1); | |
2330 | ||
2331 | return 0; | |
2332 | } | |
2333 | EXPORT_SYMBOL_GPL(snd_soc_get_enum_double); | |
2334 | ||
2335 | /** | |
2336 | * snd_soc_put_enum_double - enumerated double mixer put callback | |
2337 | * @kcontrol: mixer control | |
ac11a2b3 | 2338 | * @ucontrol: control element information |
db2a4165 FM |
2339 | * |
2340 | * Callback to set the value of a double enumerated mixer. | |
2341 | * | |
2342 | * Returns 0 for success. | |
2343 | */ | |
2344 | int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol, | |
2345 | struct snd_ctl_elem_value *ucontrol) | |
2346 | { | |
2347 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2348 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
46f5822f DR |
2349 | unsigned int val; |
2350 | unsigned int mask, bitmask; | |
db2a4165 | 2351 | |
f8ba0b7b | 2352 | for (bitmask = 1; bitmask < e->max; bitmask <<= 1) |
db2a4165 | 2353 | ; |
f8ba0b7b | 2354 | if (ucontrol->value.enumerated.item[0] > e->max - 1) |
db2a4165 FM |
2355 | return -EINVAL; |
2356 | val = ucontrol->value.enumerated.item[0] << e->shift_l; | |
2357 | mask = (bitmask - 1) << e->shift_l; | |
2358 | if (e->shift_l != e->shift_r) { | |
f8ba0b7b | 2359 | if (ucontrol->value.enumerated.item[1] > e->max - 1) |
db2a4165 FM |
2360 | return -EINVAL; |
2361 | val |= ucontrol->value.enumerated.item[1] << e->shift_r; | |
2362 | mask |= (bitmask - 1) << e->shift_r; | |
2363 | } | |
2364 | ||
6c508c62 | 2365 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
db2a4165 FM |
2366 | } |
2367 | EXPORT_SYMBOL_GPL(snd_soc_put_enum_double); | |
2368 | ||
2e72f8e3 PU |
2369 | /** |
2370 | * snd_soc_get_value_enum_double - semi enumerated double mixer get callback | |
2371 | * @kcontrol: mixer control | |
2372 | * @ucontrol: control element information | |
2373 | * | |
2374 | * Callback to get the value of a double semi enumerated mixer. | |
2375 | * | |
2376 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
2377 | * used for handling bitfield coded enumeration for example. | |
2378 | * | |
2379 | * Returns 0 for success. | |
2380 | */ | |
2381 | int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol, | |
2382 | struct snd_ctl_elem_value *ucontrol) | |
2383 | { | |
2384 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
74155556 | 2385 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f | 2386 | unsigned int reg_val, val, mux; |
2e72f8e3 PU |
2387 | |
2388 | reg_val = snd_soc_read(codec, e->reg); | |
2389 | val = (reg_val >> e->shift_l) & e->mask; | |
2390 | for (mux = 0; mux < e->max; mux++) { | |
2391 | if (val == e->values[mux]) | |
2392 | break; | |
2393 | } | |
2394 | ucontrol->value.enumerated.item[0] = mux; | |
2395 | if (e->shift_l != e->shift_r) { | |
2396 | val = (reg_val >> e->shift_r) & e->mask; | |
2397 | for (mux = 0; mux < e->max; mux++) { | |
2398 | if (val == e->values[mux]) | |
2399 | break; | |
2400 | } | |
2401 | ucontrol->value.enumerated.item[1] = mux; | |
2402 | } | |
2403 | ||
2404 | return 0; | |
2405 | } | |
2406 | EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double); | |
2407 | ||
2408 | /** | |
2409 | * snd_soc_put_value_enum_double - semi enumerated double mixer put callback | |
2410 | * @kcontrol: mixer control | |
2411 | * @ucontrol: control element information | |
2412 | * | |
2413 | * Callback to set the value of a double semi enumerated mixer. | |
2414 | * | |
2415 | * Semi enumerated mixer: the enumerated items are referred as values. Can be | |
2416 | * used for handling bitfield coded enumeration for example. | |
2417 | * | |
2418 | * Returns 0 for success. | |
2419 | */ | |
2420 | int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol, | |
2421 | struct snd_ctl_elem_value *ucontrol) | |
2422 | { | |
2423 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
74155556 | 2424 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; |
46f5822f DR |
2425 | unsigned int val; |
2426 | unsigned int mask; | |
2e72f8e3 PU |
2427 | |
2428 | if (ucontrol->value.enumerated.item[0] > e->max - 1) | |
2429 | return -EINVAL; | |
2430 | val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l; | |
2431 | mask = e->mask << e->shift_l; | |
2432 | if (e->shift_l != e->shift_r) { | |
2433 | if (ucontrol->value.enumerated.item[1] > e->max - 1) | |
2434 | return -EINVAL; | |
2435 | val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r; | |
2436 | mask |= e->mask << e->shift_r; | |
2437 | } | |
2438 | ||
6c508c62 | 2439 | return snd_soc_update_bits_locked(codec, e->reg, mask, val); |
2e72f8e3 PU |
2440 | } |
2441 | EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double); | |
2442 | ||
db2a4165 FM |
2443 | /** |
2444 | * snd_soc_info_enum_ext - external enumerated single mixer info callback | |
2445 | * @kcontrol: mixer control | |
2446 | * @uinfo: control element information | |
2447 | * | |
2448 | * Callback to provide information about an external enumerated | |
2449 | * single mixer. | |
2450 | * | |
2451 | * Returns 0 for success. | |
2452 | */ | |
2453 | int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol, | |
2454 | struct snd_ctl_elem_info *uinfo) | |
2455 | { | |
2456 | struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; | |
2457 | ||
2458 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
2459 | uinfo->count = 1; | |
f8ba0b7b | 2460 | uinfo->value.enumerated.items = e->max; |
db2a4165 | 2461 | |
f8ba0b7b JS |
2462 | if (uinfo->value.enumerated.item > e->max - 1) |
2463 | uinfo->value.enumerated.item = e->max - 1; | |
db2a4165 FM |
2464 | strcpy(uinfo->value.enumerated.name, |
2465 | e->texts[uinfo->value.enumerated.item]); | |
2466 | return 0; | |
2467 | } | |
2468 | EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext); | |
2469 | ||
2470 | /** | |
2471 | * snd_soc_info_volsw_ext - external single mixer info callback | |
2472 | * @kcontrol: mixer control | |
2473 | * @uinfo: control element information | |
2474 | * | |
2475 | * Callback to provide information about a single external mixer control. | |
2476 | * | |
2477 | * Returns 0 for success. | |
2478 | */ | |
2479 | int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol, | |
2480 | struct snd_ctl_elem_info *uinfo) | |
2481 | { | |
a7a4ac86 PZ |
2482 | int max = kcontrol->private_value; |
2483 | ||
fd5dfad9 | 2484 | if (max == 1 && !strstr(kcontrol->id.name, " Volume")) |
a7a4ac86 PZ |
2485 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
2486 | else | |
2487 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
db2a4165 | 2488 | |
db2a4165 FM |
2489 | uinfo->count = 1; |
2490 | uinfo->value.integer.min = 0; | |
a7a4ac86 | 2491 | uinfo->value.integer.max = max; |
db2a4165 FM |
2492 | return 0; |
2493 | } | |
2494 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext); | |
2495 | ||
db2a4165 FM |
2496 | /** |
2497 | * snd_soc_info_volsw - single mixer info callback | |
2498 | * @kcontrol: mixer control | |
2499 | * @uinfo: control element information | |
2500 | * | |
e8f5a103 PU |
2501 | * Callback to provide information about a single mixer control, or a double |
2502 | * mixer control that spans 2 registers. | |
db2a4165 FM |
2503 | * |
2504 | * Returns 0 for success. | |
2505 | */ | |
2506 | int snd_soc_info_volsw(struct snd_kcontrol *kcontrol, | |
2507 | struct snd_ctl_elem_info *uinfo) | |
2508 | { | |
4eaa9819 JS |
2509 | struct soc_mixer_control *mc = |
2510 | (struct soc_mixer_control *)kcontrol->private_value; | |
d11bb4a9 | 2511 | int platform_max; |
db2a4165 | 2512 | |
d11bb4a9 PU |
2513 | if (!mc->platform_max) |
2514 | mc->platform_max = mc->max; | |
2515 | platform_max = mc->platform_max; | |
2516 | ||
2517 | if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume")) | |
a7a4ac86 PZ |
2518 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
2519 | else | |
2520 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2521 | ||
e8f5a103 | 2522 | uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1; |
db2a4165 | 2523 | uinfo->value.integer.min = 0; |
d11bb4a9 | 2524 | uinfo->value.integer.max = platform_max; |
db2a4165 FM |
2525 | return 0; |
2526 | } | |
2527 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw); | |
2528 | ||
2529 | /** | |
2530 | * snd_soc_get_volsw - single mixer get callback | |
2531 | * @kcontrol: mixer control | |
ac11a2b3 | 2532 | * @ucontrol: control element information |
db2a4165 | 2533 | * |
f7915d99 PU |
2534 | * Callback to get the value of a single mixer control, or a double mixer |
2535 | * control that spans 2 registers. | |
db2a4165 FM |
2536 | * |
2537 | * Returns 0 for success. | |
2538 | */ | |
2539 | int snd_soc_get_volsw(struct snd_kcontrol *kcontrol, | |
2540 | struct snd_ctl_elem_value *ucontrol) | |
2541 | { | |
4eaa9819 JS |
2542 | struct soc_mixer_control *mc = |
2543 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 2544 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2545 | unsigned int reg = mc->reg; |
f7915d99 | 2546 | unsigned int reg2 = mc->rreg; |
815ecf8d JS |
2547 | unsigned int shift = mc->shift; |
2548 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 2549 | int max = mc->max; |
815ecf8d JS |
2550 | unsigned int mask = (1 << fls(max)) - 1; |
2551 | unsigned int invert = mc->invert; | |
db2a4165 FM |
2552 | |
2553 | ucontrol->value.integer.value[0] = | |
2554 | (snd_soc_read(codec, reg) >> shift) & mask; | |
f7915d99 | 2555 | if (invert) |
db2a4165 | 2556 | ucontrol->value.integer.value[0] = |
a7a4ac86 | 2557 | max - ucontrol->value.integer.value[0]; |
f7915d99 PU |
2558 | |
2559 | if (snd_soc_volsw_is_stereo(mc)) { | |
2560 | if (reg == reg2) | |
2561 | ucontrol->value.integer.value[1] = | |
2562 | (snd_soc_read(codec, reg) >> rshift) & mask; | |
2563 | else | |
2564 | ucontrol->value.integer.value[1] = | |
2565 | (snd_soc_read(codec, reg2) >> shift) & mask; | |
2566 | if (invert) | |
db2a4165 | 2567 | ucontrol->value.integer.value[1] = |
a7a4ac86 | 2568 | max - ucontrol->value.integer.value[1]; |
db2a4165 FM |
2569 | } |
2570 | ||
2571 | return 0; | |
2572 | } | |
2573 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw); | |
2574 | ||
2575 | /** | |
2576 | * snd_soc_put_volsw - single mixer put callback | |
2577 | * @kcontrol: mixer control | |
ac11a2b3 | 2578 | * @ucontrol: control element information |
db2a4165 | 2579 | * |
974815ba PU |
2580 | * Callback to set the value of a single mixer control, or a double mixer |
2581 | * control that spans 2 registers. | |
db2a4165 FM |
2582 | * |
2583 | * Returns 0 for success. | |
2584 | */ | |
2585 | int snd_soc_put_volsw(struct snd_kcontrol *kcontrol, | |
2586 | struct snd_ctl_elem_value *ucontrol) | |
2587 | { | |
4eaa9819 JS |
2588 | struct soc_mixer_control *mc = |
2589 | (struct soc_mixer_control *)kcontrol->private_value; | |
db2a4165 | 2590 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2591 | unsigned int reg = mc->reg; |
974815ba | 2592 | unsigned int reg2 = mc->rreg; |
815ecf8d JS |
2593 | unsigned int shift = mc->shift; |
2594 | unsigned int rshift = mc->rshift; | |
4eaa9819 | 2595 | int max = mc->max; |
815ecf8d JS |
2596 | unsigned int mask = (1 << fls(max)) - 1; |
2597 | unsigned int invert = mc->invert; | |
974815ba PU |
2598 | int err; |
2599 | bool type_2r = 0; | |
2600 | unsigned int val2 = 0; | |
2601 | unsigned int val, val_mask; | |
db2a4165 FM |
2602 | |
2603 | val = (ucontrol->value.integer.value[0] & mask); | |
2604 | if (invert) | |
a7a4ac86 | 2605 | val = max - val; |
db2a4165 FM |
2606 | val_mask = mask << shift; |
2607 | val = val << shift; | |
974815ba | 2608 | if (snd_soc_volsw_is_stereo(mc)) { |
db2a4165 FM |
2609 | val2 = (ucontrol->value.integer.value[1] & mask); |
2610 | if (invert) | |
a7a4ac86 | 2611 | val2 = max - val2; |
974815ba PU |
2612 | if (reg == reg2) { |
2613 | val_mask |= mask << rshift; | |
2614 | val |= val2 << rshift; | |
2615 | } else { | |
2616 | val2 = val2 << shift; | |
2617 | type_2r = 1; | |
2618 | } | |
db2a4165 | 2619 | } |
6c508c62 | 2620 | err = snd_soc_update_bits_locked(codec, reg, val_mask, val); |
3ff3f64b | 2621 | if (err < 0) |
db2a4165 FM |
2622 | return err; |
2623 | ||
974815ba PU |
2624 | if (type_2r) |
2625 | err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2); | |
2626 | ||
db2a4165 FM |
2627 | return err; |
2628 | } | |
974815ba | 2629 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw); |
db2a4165 | 2630 | |
1d99f243 BA |
2631 | /** |
2632 | * snd_soc_get_volsw_sx - single mixer get callback | |
2633 | * @kcontrol: mixer control | |
2634 | * @ucontrol: control element information | |
2635 | * | |
2636 | * Callback to get the value of a single mixer control, or a double mixer | |
2637 | * control that spans 2 registers. | |
2638 | * | |
2639 | * Returns 0 for success. | |
2640 | */ | |
2641 | int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol, | |
2642 | struct snd_ctl_elem_value *ucontrol) | |
2643 | { | |
2644 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2645 | struct soc_mixer_control *mc = | |
2646 | (struct soc_mixer_control *)kcontrol->private_value; | |
2647 | ||
2648 | unsigned int reg = mc->reg; | |
2649 | unsigned int reg2 = mc->rreg; | |
2650 | unsigned int shift = mc->shift; | |
2651 | unsigned int rshift = mc->rshift; | |
2652 | int max = mc->max; | |
2653 | int min = mc->min; | |
2654 | int mask = (1 << (fls(min + max) - 1)) - 1; | |
2655 | ||
2656 | ucontrol->value.integer.value[0] = | |
2657 | ((snd_soc_read(codec, reg) >> shift) - min) & mask; | |
2658 | ||
2659 | if (snd_soc_volsw_is_stereo(mc)) | |
2660 | ucontrol->value.integer.value[1] = | |
2661 | ((snd_soc_read(codec, reg2) >> rshift) - min) & mask; | |
2662 | ||
2663 | return 0; | |
2664 | } | |
2665 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx); | |
2666 | ||
2667 | /** | |
2668 | * snd_soc_put_volsw_sx - double mixer set callback | |
2669 | * @kcontrol: mixer control | |
2670 | * @uinfo: control element information | |
2671 | * | |
2672 | * Callback to set the value of a double mixer control that spans 2 registers. | |
2673 | * | |
2674 | * Returns 0 for success. | |
2675 | */ | |
2676 | int snd_soc_put_volsw_sx(struct snd_kcontrol *kcontrol, | |
2677 | struct snd_ctl_elem_value *ucontrol) | |
2678 | { | |
2679 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2680 | struct soc_mixer_control *mc = | |
2681 | (struct soc_mixer_control *)kcontrol->private_value; | |
2682 | ||
2683 | unsigned int reg = mc->reg; | |
2684 | unsigned int reg2 = mc->rreg; | |
2685 | unsigned int shift = mc->shift; | |
2686 | unsigned int rshift = mc->rshift; | |
2687 | int max = mc->max; | |
2688 | int min = mc->min; | |
2689 | int mask = (1 << (fls(min + max) - 1)) - 1; | |
27f1d759 | 2690 | int err = 0; |
1d99f243 BA |
2691 | unsigned short val, val_mask, val2 = 0; |
2692 | ||
2693 | val_mask = mask << shift; | |
2694 | val = (ucontrol->value.integer.value[0] + min) & mask; | |
2695 | val = val << shift; | |
2696 | ||
2697 | if (snd_soc_update_bits_locked(codec, reg, val_mask, val)) | |
2698 | return err; | |
2699 | ||
2700 | if (snd_soc_volsw_is_stereo(mc)) { | |
2701 | val_mask = mask << rshift; | |
2702 | val2 = (ucontrol->value.integer.value[1] + min) & mask; | |
2703 | val2 = val2 << rshift; | |
2704 | ||
2705 | if (snd_soc_update_bits_locked(codec, reg2, val_mask, val2)) | |
2706 | return err; | |
2707 | } | |
2708 | return 0; | |
2709 | } | |
2710 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_sx); | |
2711 | ||
e13ac2e9 MB |
2712 | /** |
2713 | * snd_soc_info_volsw_s8 - signed mixer info callback | |
2714 | * @kcontrol: mixer control | |
2715 | * @uinfo: control element information | |
2716 | * | |
2717 | * Callback to provide information about a signed mixer control. | |
2718 | * | |
2719 | * Returns 0 for success. | |
2720 | */ | |
2721 | int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol, | |
2722 | struct snd_ctl_elem_info *uinfo) | |
2723 | { | |
4eaa9819 JS |
2724 | struct soc_mixer_control *mc = |
2725 | (struct soc_mixer_control *)kcontrol->private_value; | |
d11bb4a9 | 2726 | int platform_max; |
4eaa9819 | 2727 | int min = mc->min; |
e13ac2e9 | 2728 | |
d11bb4a9 PU |
2729 | if (!mc->platform_max) |
2730 | mc->platform_max = mc->max; | |
2731 | platform_max = mc->platform_max; | |
2732 | ||
e13ac2e9 MB |
2733 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
2734 | uinfo->count = 2; | |
2735 | uinfo->value.integer.min = 0; | |
d11bb4a9 | 2736 | uinfo->value.integer.max = platform_max - min; |
e13ac2e9 MB |
2737 | return 0; |
2738 | } | |
2739 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8); | |
2740 | ||
2741 | /** | |
2742 | * snd_soc_get_volsw_s8 - signed mixer get callback | |
2743 | * @kcontrol: mixer control | |
ac11a2b3 | 2744 | * @ucontrol: control element information |
e13ac2e9 MB |
2745 | * |
2746 | * Callback to get the value of a signed mixer control. | |
2747 | * | |
2748 | * Returns 0 for success. | |
2749 | */ | |
2750 | int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol, | |
2751 | struct snd_ctl_elem_value *ucontrol) | |
2752 | { | |
4eaa9819 JS |
2753 | struct soc_mixer_control *mc = |
2754 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 2755 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2756 | unsigned int reg = mc->reg; |
4eaa9819 | 2757 | int min = mc->min; |
e13ac2e9 MB |
2758 | int val = snd_soc_read(codec, reg); |
2759 | ||
2760 | ucontrol->value.integer.value[0] = | |
2761 | ((signed char)(val & 0xff))-min; | |
2762 | ucontrol->value.integer.value[1] = | |
2763 | ((signed char)((val >> 8) & 0xff))-min; | |
2764 | return 0; | |
2765 | } | |
2766 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8); | |
2767 | ||
2768 | /** | |
2769 | * snd_soc_put_volsw_sgn - signed mixer put callback | |
2770 | * @kcontrol: mixer control | |
ac11a2b3 | 2771 | * @ucontrol: control element information |
e13ac2e9 MB |
2772 | * |
2773 | * Callback to set the value of a signed mixer control. | |
2774 | * | |
2775 | * Returns 0 for success. | |
2776 | */ | |
2777 | int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol, | |
2778 | struct snd_ctl_elem_value *ucontrol) | |
2779 | { | |
4eaa9819 JS |
2780 | struct soc_mixer_control *mc = |
2781 | (struct soc_mixer_control *)kcontrol->private_value; | |
e13ac2e9 | 2782 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
815ecf8d | 2783 | unsigned int reg = mc->reg; |
4eaa9819 | 2784 | int min = mc->min; |
46f5822f | 2785 | unsigned int val; |
e13ac2e9 MB |
2786 | |
2787 | val = (ucontrol->value.integer.value[0]+min) & 0xff; | |
2788 | val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8; | |
2789 | ||
6c508c62 | 2790 | return snd_soc_update_bits_locked(codec, reg, 0xffff, val); |
e13ac2e9 MB |
2791 | } |
2792 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8); | |
2793 | ||
6c9d8cf6 AT |
2794 | /** |
2795 | * snd_soc_info_volsw_range - single mixer info callback with range. | |
2796 | * @kcontrol: mixer control | |
2797 | * @uinfo: control element information | |
2798 | * | |
2799 | * Callback to provide information, within a range, about a single | |
2800 | * mixer control. | |
2801 | * | |
2802 | * returns 0 for success. | |
2803 | */ | |
2804 | int snd_soc_info_volsw_range(struct snd_kcontrol *kcontrol, | |
2805 | struct snd_ctl_elem_info *uinfo) | |
2806 | { | |
2807 | struct soc_mixer_control *mc = | |
2808 | (struct soc_mixer_control *)kcontrol->private_value; | |
2809 | int platform_max; | |
2810 | int min = mc->min; | |
2811 | ||
2812 | if (!mc->platform_max) | |
2813 | mc->platform_max = mc->max; | |
2814 | platform_max = mc->platform_max; | |
2815 | ||
2816 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
2817 | uinfo->count = 1; | |
2818 | uinfo->value.integer.min = 0; | |
2819 | uinfo->value.integer.max = platform_max - min; | |
2820 | ||
2821 | return 0; | |
2822 | } | |
2823 | EXPORT_SYMBOL_GPL(snd_soc_info_volsw_range); | |
2824 | ||
2825 | /** | |
2826 | * snd_soc_put_volsw_range - single mixer put value callback with range. | |
2827 | * @kcontrol: mixer control | |
2828 | * @ucontrol: control element information | |
2829 | * | |
2830 | * Callback to set the value, within a range, for a single mixer control. | |
2831 | * | |
2832 | * Returns 0 for success. | |
2833 | */ | |
2834 | int snd_soc_put_volsw_range(struct snd_kcontrol *kcontrol, | |
2835 | struct snd_ctl_elem_value *ucontrol) | |
2836 | { | |
2837 | struct soc_mixer_control *mc = | |
2838 | (struct soc_mixer_control *)kcontrol->private_value; | |
2839 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2840 | unsigned int reg = mc->reg; | |
2841 | unsigned int shift = mc->shift; | |
2842 | int min = mc->min; | |
2843 | int max = mc->max; | |
2844 | unsigned int mask = (1 << fls(max)) - 1; | |
2845 | unsigned int invert = mc->invert; | |
2846 | unsigned int val, val_mask; | |
2847 | ||
2848 | val = ((ucontrol->value.integer.value[0] + min) & mask); | |
2849 | if (invert) | |
2850 | val = max - val; | |
2851 | val_mask = mask << shift; | |
2852 | val = val << shift; | |
2853 | ||
2854 | return snd_soc_update_bits_locked(codec, reg, val_mask, val); | |
2855 | } | |
2856 | EXPORT_SYMBOL_GPL(snd_soc_put_volsw_range); | |
2857 | ||
2858 | /** | |
2859 | * snd_soc_get_volsw_range - single mixer get callback with range | |
2860 | * @kcontrol: mixer control | |
2861 | * @ucontrol: control element information | |
2862 | * | |
2863 | * Callback to get the value, within a range, of a single mixer control. | |
2864 | * | |
2865 | * Returns 0 for success. | |
2866 | */ | |
2867 | int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol, | |
2868 | struct snd_ctl_elem_value *ucontrol) | |
2869 | { | |
2870 | struct soc_mixer_control *mc = | |
2871 | (struct soc_mixer_control *)kcontrol->private_value; | |
2872 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2873 | unsigned int reg = mc->reg; | |
2874 | unsigned int shift = mc->shift; | |
2875 | int min = mc->min; | |
2876 | int max = mc->max; | |
2877 | unsigned int mask = (1 << fls(max)) - 1; | |
2878 | unsigned int invert = mc->invert; | |
2879 | ||
2880 | ucontrol->value.integer.value[0] = | |
2881 | (snd_soc_read(codec, reg) >> shift) & mask; | |
2882 | if (invert) | |
2883 | ucontrol->value.integer.value[0] = | |
2884 | max - ucontrol->value.integer.value[0]; | |
2885 | ucontrol->value.integer.value[0] = | |
2886 | ucontrol->value.integer.value[0] - min; | |
2887 | ||
2888 | return 0; | |
2889 | } | |
2890 | EXPORT_SYMBOL_GPL(snd_soc_get_volsw_range); | |
2891 | ||
637d3847 PU |
2892 | /** |
2893 | * snd_soc_limit_volume - Set new limit to an existing volume control. | |
2894 | * | |
2895 | * @codec: where to look for the control | |
2896 | * @name: Name of the control | |
2897 | * @max: new maximum limit | |
2898 | * | |
2899 | * Return 0 for success, else error. | |
2900 | */ | |
2901 | int snd_soc_limit_volume(struct snd_soc_codec *codec, | |
2902 | const char *name, int max) | |
2903 | { | |
f0fba2ad | 2904 | struct snd_card *card = codec->card->snd_card; |
637d3847 PU |
2905 | struct snd_kcontrol *kctl; |
2906 | struct soc_mixer_control *mc; | |
2907 | int found = 0; | |
2908 | int ret = -EINVAL; | |
2909 | ||
2910 | /* Sanity check for name and max */ | |
2911 | if (unlikely(!name || max <= 0)) | |
2912 | return -EINVAL; | |
2913 | ||
2914 | list_for_each_entry(kctl, &card->controls, list) { | |
2915 | if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) { | |
2916 | found = 1; | |
2917 | break; | |
2918 | } | |
2919 | } | |
2920 | if (found) { | |
2921 | mc = (struct soc_mixer_control *)kctl->private_value; | |
2922 | if (max <= mc->max) { | |
d11bb4a9 | 2923 | mc->platform_max = max; |
637d3847 PU |
2924 | ret = 0; |
2925 | } | |
2926 | } | |
2927 | return ret; | |
2928 | } | |
2929 | EXPORT_SYMBOL_GPL(snd_soc_limit_volume); | |
2930 | ||
71d08516 MB |
2931 | int snd_soc_bytes_info(struct snd_kcontrol *kcontrol, |
2932 | struct snd_ctl_elem_info *uinfo) | |
2933 | { | |
2934 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2935 | struct soc_bytes *params = (void *)kcontrol->private_value; | |
2936 | ||
2937 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; | |
2938 | uinfo->count = params->num_regs * codec->val_bytes; | |
2939 | ||
2940 | return 0; | |
2941 | } | |
2942 | EXPORT_SYMBOL_GPL(snd_soc_bytes_info); | |
2943 | ||
2944 | int snd_soc_bytes_get(struct snd_kcontrol *kcontrol, | |
2945 | struct snd_ctl_elem_value *ucontrol) | |
2946 | { | |
2947 | struct soc_bytes *params = (void *)kcontrol->private_value; | |
2948 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
2949 | int ret; | |
2950 | ||
2951 | if (codec->using_regmap) | |
2952 | ret = regmap_raw_read(codec->control_data, params->base, | |
2953 | ucontrol->value.bytes.data, | |
2954 | params->num_regs * codec->val_bytes); | |
2955 | else | |
2956 | ret = -EINVAL; | |
2957 | ||
f831b055 MB |
2958 | /* Hide any masked bytes to ensure consistent data reporting */ |
2959 | if (ret == 0 && params->mask) { | |
2960 | switch (codec->val_bytes) { | |
2961 | case 1: | |
2962 | ucontrol->value.bytes.data[0] &= ~params->mask; | |
2963 | break; | |
2964 | case 2: | |
2965 | ((u16 *)(&ucontrol->value.bytes.data))[0] | |
2966 | &= ~params->mask; | |
2967 | break; | |
2968 | case 4: | |
2969 | ((u32 *)(&ucontrol->value.bytes.data))[0] | |
2970 | &= ~params->mask; | |
2971 | break; | |
2972 | default: | |
2973 | return -EINVAL; | |
2974 | } | |
2975 | } | |
2976 | ||
71d08516 MB |
2977 | return ret; |
2978 | } | |
2979 | EXPORT_SYMBOL_GPL(snd_soc_bytes_get); | |
2980 | ||
2981 | int snd_soc_bytes_put(struct snd_kcontrol *kcontrol, | |
2982 | struct snd_ctl_elem_value *ucontrol) | |
2983 | { | |
2984 | struct soc_bytes *params = (void *)kcontrol->private_value; | |
2985 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
f831b055 MB |
2986 | int ret, len; |
2987 | unsigned int val; | |
2988 | void *data; | |
71d08516 | 2989 | |
f831b055 MB |
2990 | if (!codec->using_regmap) |
2991 | return -EINVAL; | |
2992 | ||
2993 | data = ucontrol->value.bytes.data; | |
2994 | len = params->num_regs * codec->val_bytes; | |
2995 | ||
2996 | /* | |
2997 | * If we've got a mask then we need to preserve the register | |
2998 | * bits. We shouldn't modify the incoming data so take a | |
2999 | * copy. | |
3000 | */ | |
3001 | if (params->mask) { | |
3002 | ret = regmap_read(codec->control_data, params->base, &val); | |
3003 | if (ret != 0) | |
3004 | return ret; | |
3005 | ||
3006 | val &= params->mask; | |
3007 | ||
3008 | data = kmemdup(data, len, GFP_KERNEL); | |
3009 | if (!data) | |
3010 | return -ENOMEM; | |
3011 | ||
3012 | switch (codec->val_bytes) { | |
3013 | case 1: | |
3014 | ((u8 *)data)[0] &= ~params->mask; | |
3015 | ((u8 *)data)[0] |= val; | |
3016 | break; | |
3017 | case 2: | |
3018 | ((u16 *)data)[0] &= cpu_to_be16(~params->mask); | |
3019 | ((u16 *)data)[0] |= cpu_to_be16(val); | |
3020 | break; | |
3021 | case 4: | |
3022 | ((u32 *)data)[0] &= cpu_to_be32(~params->mask); | |
3023 | ((u32 *)data)[0] |= cpu_to_be32(val); | |
3024 | break; | |
3025 | default: | |
3026 | return -EINVAL; | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | ret = regmap_raw_write(codec->control_data, params->base, | |
3031 | data, len); | |
3032 | ||
3033 | if (params->mask) | |
3034 | kfree(data); | |
71d08516 MB |
3035 | |
3036 | return ret; | |
3037 | } | |
3038 | EXPORT_SYMBOL_GPL(snd_soc_bytes_put); | |
3039 | ||
4183eed2 KK |
3040 | /** |
3041 | * snd_soc_info_xr_sx - signed multi register info callback | |
3042 | * @kcontrol: mreg control | |
3043 | * @uinfo: control element information | |
3044 | * | |
3045 | * Callback to provide information of a control that can | |
3046 | * span multiple codec registers which together | |
3047 | * forms a single signed value in a MSB/LSB manner. | |
3048 | * | |
3049 | * Returns 0 for success. | |
3050 | */ | |
3051 | int snd_soc_info_xr_sx(struct snd_kcontrol *kcontrol, | |
3052 | struct snd_ctl_elem_info *uinfo) | |
3053 | { | |
3054 | struct soc_mreg_control *mc = | |
3055 | (struct soc_mreg_control *)kcontrol->private_value; | |
3056 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
3057 | uinfo->count = 1; | |
3058 | uinfo->value.integer.min = mc->min; | |
3059 | uinfo->value.integer.max = mc->max; | |
3060 | ||
3061 | return 0; | |
3062 | } | |
3063 | EXPORT_SYMBOL_GPL(snd_soc_info_xr_sx); | |
3064 | ||
3065 | /** | |
3066 | * snd_soc_get_xr_sx - signed multi register get callback | |
3067 | * @kcontrol: mreg control | |
3068 | * @ucontrol: control element information | |
3069 | * | |
3070 | * Callback to get the value of a control that can span | |
3071 | * multiple codec registers which together forms a single | |
3072 | * signed value in a MSB/LSB manner. The control supports | |
3073 | * specifying total no of bits used to allow for bitfields | |
3074 | * across the multiple codec registers. | |
3075 | * | |
3076 | * Returns 0 for success. | |
3077 | */ | |
3078 | int snd_soc_get_xr_sx(struct snd_kcontrol *kcontrol, | |
3079 | struct snd_ctl_elem_value *ucontrol) | |
3080 | { | |
3081 | struct soc_mreg_control *mc = | |
3082 | (struct soc_mreg_control *)kcontrol->private_value; | |
3083 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
3084 | unsigned int regbase = mc->regbase; | |
3085 | unsigned int regcount = mc->regcount; | |
3086 | unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE; | |
3087 | unsigned int regwmask = (1<<regwshift)-1; | |
3088 | unsigned int invert = mc->invert; | |
3089 | unsigned long mask = (1UL<<mc->nbits)-1; | |
3090 | long min = mc->min; | |
3091 | long max = mc->max; | |
3092 | long val = 0; | |
3093 | unsigned long regval; | |
3094 | unsigned int i; | |
3095 | ||
3096 | for (i = 0; i < regcount; i++) { | |
3097 | regval = snd_soc_read(codec, regbase+i) & regwmask; | |
3098 | val |= regval << (regwshift*(regcount-i-1)); | |
3099 | } | |
3100 | val &= mask; | |
3101 | if (min < 0 && val > max) | |
3102 | val |= ~mask; | |
3103 | if (invert) | |
3104 | val = max - val; | |
3105 | ucontrol->value.integer.value[0] = val; | |
3106 | ||
3107 | return 0; | |
3108 | } | |
3109 | EXPORT_SYMBOL_GPL(snd_soc_get_xr_sx); | |
3110 | ||
3111 | /** | |
3112 | * snd_soc_put_xr_sx - signed multi register get callback | |
3113 | * @kcontrol: mreg control | |
3114 | * @ucontrol: control element information | |
3115 | * | |
3116 | * Callback to set the value of a control that can span | |
3117 | * multiple codec registers which together forms a single | |
3118 | * signed value in a MSB/LSB manner. The control supports | |
3119 | * specifying total no of bits used to allow for bitfields | |
3120 | * across the multiple codec registers. | |
3121 | * | |
3122 | * Returns 0 for success. | |
3123 | */ | |
3124 | int snd_soc_put_xr_sx(struct snd_kcontrol *kcontrol, | |
3125 | struct snd_ctl_elem_value *ucontrol) | |
3126 | { | |
3127 | struct soc_mreg_control *mc = | |
3128 | (struct soc_mreg_control *)kcontrol->private_value; | |
3129 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
3130 | unsigned int regbase = mc->regbase; | |
3131 | unsigned int regcount = mc->regcount; | |
3132 | unsigned int regwshift = codec->driver->reg_word_size * BITS_PER_BYTE; | |
3133 | unsigned int regwmask = (1<<regwshift)-1; | |
3134 | unsigned int invert = mc->invert; | |
3135 | unsigned long mask = (1UL<<mc->nbits)-1; | |
4183eed2 KK |
3136 | long max = mc->max; |
3137 | long val = ucontrol->value.integer.value[0]; | |
3138 | unsigned int i, regval, regmask; | |
3139 | int err; | |
3140 | ||
3141 | if (invert) | |
3142 | val = max - val; | |
3143 | val &= mask; | |
3144 | for (i = 0; i < regcount; i++) { | |
3145 | regval = (val >> (regwshift*(regcount-i-1))) & regwmask; | |
3146 | regmask = (mask >> (regwshift*(regcount-i-1))) & regwmask; | |
3147 | err = snd_soc_update_bits_locked(codec, regbase+i, | |
3148 | regmask, regval); | |
3149 | if (err < 0) | |
3150 | return err; | |
3151 | } | |
3152 | ||
3153 | return 0; | |
3154 | } | |
3155 | EXPORT_SYMBOL_GPL(snd_soc_put_xr_sx); | |
3156 | ||
dd7b10b3 KK |
3157 | /** |
3158 | * snd_soc_get_strobe - strobe get callback | |
3159 | * @kcontrol: mixer control | |
3160 | * @ucontrol: control element information | |
3161 | * | |
3162 | * Callback get the value of a strobe mixer control. | |
3163 | * | |
3164 | * Returns 0 for success. | |
3165 | */ | |
3166 | int snd_soc_get_strobe(struct snd_kcontrol *kcontrol, | |
3167 | struct snd_ctl_elem_value *ucontrol) | |
3168 | { | |
3169 | struct soc_mixer_control *mc = | |
3170 | (struct soc_mixer_control *)kcontrol->private_value; | |
3171 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
3172 | unsigned int reg = mc->reg; | |
3173 | unsigned int shift = mc->shift; | |
3174 | unsigned int mask = 1 << shift; | |
3175 | unsigned int invert = mc->invert != 0; | |
3176 | unsigned int val = snd_soc_read(codec, reg) & mask; | |
3177 | ||
3178 | if (shift != 0 && val != 0) | |
3179 | val = val >> shift; | |
3180 | ucontrol->value.enumerated.item[0] = val ^ invert; | |
3181 | ||
3182 | return 0; | |
3183 | } | |
3184 | EXPORT_SYMBOL_GPL(snd_soc_get_strobe); | |
3185 | ||
3186 | /** | |
3187 | * snd_soc_put_strobe - strobe put callback | |
3188 | * @kcontrol: mixer control | |
3189 | * @ucontrol: control element information | |
3190 | * | |
3191 | * Callback strobe a register bit to high then low (or the inverse) | |
3192 | * in one pass of a single mixer enum control. | |
3193 | * | |
3194 | * Returns 1 for success. | |
3195 | */ | |
3196 | int snd_soc_put_strobe(struct snd_kcontrol *kcontrol, | |
3197 | struct snd_ctl_elem_value *ucontrol) | |
3198 | { | |
3199 | struct soc_mixer_control *mc = | |
3200 | (struct soc_mixer_control *)kcontrol->private_value; | |
3201 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); | |
3202 | unsigned int reg = mc->reg; | |
3203 | unsigned int shift = mc->shift; | |
3204 | unsigned int mask = 1 << shift; | |
3205 | unsigned int invert = mc->invert != 0; | |
3206 | unsigned int strobe = ucontrol->value.enumerated.item[0] != 0; | |
3207 | unsigned int val1 = (strobe ^ invert) ? mask : 0; | |
3208 | unsigned int val2 = (strobe ^ invert) ? 0 : mask; | |
3209 | int err; | |
3210 | ||
3211 | err = snd_soc_update_bits_locked(codec, reg, mask, val1); | |
3212 | if (err < 0) | |
3213 | return err; | |
3214 | ||
3215 | err = snd_soc_update_bits_locked(codec, reg, mask, val2); | |
3216 | return err; | |
3217 | } | |
3218 | EXPORT_SYMBOL_GPL(snd_soc_put_strobe); | |
3219 | ||
8c6529db LG |
3220 | /** |
3221 | * snd_soc_dai_set_sysclk - configure DAI system or master clock. | |
3222 | * @dai: DAI | |
3223 | * @clk_id: DAI specific clock ID | |
3224 | * @freq: new clock frequency in Hz | |
3225 | * @dir: new clock direction - input/output. | |
3226 | * | |
3227 | * Configures the DAI master (MCLK) or system (SYSCLK) clocking. | |
3228 | */ | |
3229 | int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, | |
3230 | unsigned int freq, int dir) | |
3231 | { | |
f0fba2ad LG |
3232 | if (dai->driver && dai->driver->ops->set_sysclk) |
3233 | return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); | |
ec4ee52a | 3234 | else if (dai->codec && dai->codec->driver->set_sysclk) |
da1c6ea6 | 3235 | return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, |
ec4ee52a | 3236 | freq, dir); |
8c6529db LG |
3237 | else |
3238 | return -EINVAL; | |
3239 | } | |
3240 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); | |
3241 | ||
ec4ee52a MB |
3242 | /** |
3243 | * snd_soc_codec_set_sysclk - configure CODEC system or master clock. | |
3244 | * @codec: CODEC | |
3245 | * @clk_id: DAI specific clock ID | |
da1c6ea6 | 3246 | * @source: Source for the clock |
ec4ee52a MB |
3247 | * @freq: new clock frequency in Hz |
3248 | * @dir: new clock direction - input/output. | |
3249 | * | |
3250 | * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. | |
3251 | */ | |
3252 | int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, | |
da1c6ea6 | 3253 | int source, unsigned int freq, int dir) |
ec4ee52a MB |
3254 | { |
3255 | if (codec->driver->set_sysclk) | |
da1c6ea6 MB |
3256 | return codec->driver->set_sysclk(codec, clk_id, source, |
3257 | freq, dir); | |
ec4ee52a MB |
3258 | else |
3259 | return -EINVAL; | |
3260 | } | |
3261 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); | |
3262 | ||
8c6529db LG |
3263 | /** |
3264 | * snd_soc_dai_set_clkdiv - configure DAI clock dividers. | |
3265 | * @dai: DAI | |
ac11a2b3 | 3266 | * @div_id: DAI specific clock divider ID |
8c6529db LG |
3267 | * @div: new clock divisor. |
3268 | * | |
3269 | * Configures the clock dividers. This is used to derive the best DAI bit and | |
3270 | * frame clocks from the system or master clock. It's best to set the DAI bit | |
3271 | * and frame clocks as low as possible to save system power. | |
3272 | */ | |
3273 | int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, | |
3274 | int div_id, int div) | |
3275 | { | |
f0fba2ad LG |
3276 | if (dai->driver && dai->driver->ops->set_clkdiv) |
3277 | return dai->driver->ops->set_clkdiv(dai, div_id, div); | |
8c6529db LG |
3278 | else |
3279 | return -EINVAL; | |
3280 | } | |
3281 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); | |
3282 | ||
3283 | /** | |
3284 | * snd_soc_dai_set_pll - configure DAI PLL. | |
3285 | * @dai: DAI | |
3286 | * @pll_id: DAI specific PLL ID | |
85488037 | 3287 | * @source: DAI specific source for the PLL |
8c6529db LG |
3288 | * @freq_in: PLL input clock frequency in Hz |
3289 | * @freq_out: requested PLL output clock frequency in Hz | |
3290 | * | |
3291 | * Configures and enables PLL to generate output clock based on input clock. | |
3292 | */ | |
85488037 MB |
3293 | int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, |
3294 | unsigned int freq_in, unsigned int freq_out) | |
8c6529db | 3295 | { |
f0fba2ad LG |
3296 | if (dai->driver && dai->driver->ops->set_pll) |
3297 | return dai->driver->ops->set_pll(dai, pll_id, source, | |
85488037 | 3298 | freq_in, freq_out); |
ec4ee52a MB |
3299 | else if (dai->codec && dai->codec->driver->set_pll) |
3300 | return dai->codec->driver->set_pll(dai->codec, pll_id, source, | |
3301 | freq_in, freq_out); | |
8c6529db LG |
3302 | else |
3303 | return -EINVAL; | |
3304 | } | |
3305 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); | |
3306 | ||
ec4ee52a MB |
3307 | /* |
3308 | * snd_soc_codec_set_pll - configure codec PLL. | |
3309 | * @codec: CODEC | |
3310 | * @pll_id: DAI specific PLL ID | |
3311 | * @source: DAI specific source for the PLL | |
3312 | * @freq_in: PLL input clock frequency in Hz | |
3313 | * @freq_out: requested PLL output clock frequency in Hz | |
3314 | * | |
3315 | * Configures and enables PLL to generate output clock based on input clock. | |
3316 | */ | |
3317 | int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, | |
3318 | unsigned int freq_in, unsigned int freq_out) | |
3319 | { | |
3320 | if (codec->driver->set_pll) | |
3321 | return codec->driver->set_pll(codec, pll_id, source, | |
3322 | freq_in, freq_out); | |
3323 | else | |
3324 | return -EINVAL; | |
3325 | } | |
3326 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); | |
3327 | ||
8c6529db LG |
3328 | /** |
3329 | * snd_soc_dai_set_fmt - configure DAI hardware audio format. | |
3330 | * @dai: DAI | |
8c6529db LG |
3331 | * @fmt: SND_SOC_DAIFMT_ format value. |
3332 | * | |
3333 | * Configures the DAI hardware format and clocking. | |
3334 | */ | |
3335 | int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
3336 | { | |
5e4ba569 | 3337 | if (dai->driver == NULL) |
8c6529db | 3338 | return -EINVAL; |
5e4ba569 SG |
3339 | if (dai->driver->ops->set_fmt == NULL) |
3340 | return -ENOTSUPP; | |
3341 | return dai->driver->ops->set_fmt(dai, fmt); | |
8c6529db LG |
3342 | } |
3343 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); | |
3344 | ||
3345 | /** | |
3346 | * snd_soc_dai_set_tdm_slot - configure DAI TDM. | |
3347 | * @dai: DAI | |
a5479e38 DR |
3348 | * @tx_mask: bitmask representing active TX slots. |
3349 | * @rx_mask: bitmask representing active RX slots. | |
8c6529db | 3350 | * @slots: Number of slots in use. |
a5479e38 | 3351 | * @slot_width: Width in bits for each slot. |
8c6529db LG |
3352 | * |
3353 | * Configures a DAI for TDM operation. Both mask and slots are codec and DAI | |
3354 | * specific. | |
3355 | */ | |
3356 | int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, | |
a5479e38 | 3357 | unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) |
8c6529db | 3358 | { |
f0fba2ad LG |
3359 | if (dai->driver && dai->driver->ops->set_tdm_slot) |
3360 | return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, | |
a5479e38 | 3361 | slots, slot_width); |
8c6529db LG |
3362 | else |
3363 | return -EINVAL; | |
3364 | } | |
3365 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); | |
3366 | ||
472df3cb BS |
3367 | /** |
3368 | * snd_soc_dai_set_channel_map - configure DAI audio channel map | |
3369 | * @dai: DAI | |
3370 | * @tx_num: how many TX channels | |
3371 | * @tx_slot: pointer to an array which imply the TX slot number channel | |
3372 | * 0~num-1 uses | |
3373 | * @rx_num: how many RX channels | |
3374 | * @rx_slot: pointer to an array which imply the RX slot number channel | |
3375 | * 0~num-1 uses | |
3376 | * | |
3377 | * configure the relationship between channel number and TDM slot number. | |
3378 | */ | |
3379 | int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, | |
3380 | unsigned int tx_num, unsigned int *tx_slot, | |
3381 | unsigned int rx_num, unsigned int *rx_slot) | |
3382 | { | |
f0fba2ad LG |
3383 | if (dai->driver && dai->driver->ops->set_channel_map) |
3384 | return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, | |
472df3cb BS |
3385 | rx_num, rx_slot); |
3386 | else | |
3387 | return -EINVAL; | |
3388 | } | |
3389 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); | |
3390 | ||
8c6529db LG |
3391 | /** |
3392 | * snd_soc_dai_set_tristate - configure DAI system or master clock. | |
3393 | * @dai: DAI | |
3394 | * @tristate: tristate enable | |
3395 | * | |
3396 | * Tristates the DAI so that others can use it. | |
3397 | */ | |
3398 | int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) | |
3399 | { | |
f0fba2ad LG |
3400 | if (dai->driver && dai->driver->ops->set_tristate) |
3401 | return dai->driver->ops->set_tristate(dai, tristate); | |
8c6529db LG |
3402 | else |
3403 | return -EINVAL; | |
3404 | } | |
3405 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); | |
3406 | ||
3407 | /** | |
3408 | * snd_soc_dai_digital_mute - configure DAI system or master clock. | |
3409 | * @dai: DAI | |
3410 | * @mute: mute enable | |
3411 | * | |
3412 | * Mutes the DAI DAC. | |
3413 | */ | |
3414 | int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute) | |
3415 | { | |
f0fba2ad LG |
3416 | if (dai->driver && dai->driver->ops->digital_mute) |
3417 | return dai->driver->ops->digital_mute(dai, mute); | |
8c6529db | 3418 | else |
04570c62 | 3419 | return -ENOTSUPP; |
8c6529db LG |
3420 | } |
3421 | EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); | |
3422 | ||
c5af3a2e MB |
3423 | /** |
3424 | * snd_soc_register_card - Register a card with the ASoC core | |
3425 | * | |
ac11a2b3 | 3426 | * @card: Card to register |
c5af3a2e | 3427 | * |
c5af3a2e | 3428 | */ |
70a7ca34 | 3429 | int snd_soc_register_card(struct snd_soc_card *card) |
c5af3a2e | 3430 | { |
b19e6e7b | 3431 | int i, ret; |
f0fba2ad | 3432 | |
c5af3a2e MB |
3433 | if (!card->name || !card->dev) |
3434 | return -EINVAL; | |
3435 | ||
5a504963 SW |
3436 | for (i = 0; i < card->num_links; i++) { |
3437 | struct snd_soc_dai_link *link = &card->dai_link[i]; | |
3438 | ||
3439 | /* | |
3440 | * Codec must be specified by 1 of name or OF node, | |
3441 | * not both or neither. | |
3442 | */ | |
3443 | if (!!link->codec_name == !!link->codec_of_node) { | |
3444 | dev_err(card->dev, | |
3b09bb82 LG |
3445 | "Neither/both codec name/of_node are set for %s\n", |
3446 | link->name); | |
5a504963 SW |
3447 | return -EINVAL; |
3448 | } | |
bc92657a SW |
3449 | /* Codec DAI name must be specified */ |
3450 | if (!link->codec_dai_name) { | |
3451 | dev_err(card->dev, "codec_dai_name not set for %s\n", | |
3452 | link->name); | |
3453 | return -EINVAL; | |
3454 | } | |
5a504963 SW |
3455 | |
3456 | /* | |
3457 | * Platform may be specified by either name or OF node, but | |
3458 | * can be left unspecified, and a dummy platform will be used. | |
3459 | */ | |
3460 | if (link->platform_name && link->platform_of_node) { | |
3461 | dev_err(card->dev, | |
3b09bb82 | 3462 | "Both platform name/of_node are set for %s\n", link->name); |
5a504963 SW |
3463 | return -EINVAL; |
3464 | } | |
3465 | ||
3466 | /* | |
bc92657a SW |
3467 | * CPU device may be specified by either name or OF node, but |
3468 | * can be left unspecified, and will be matched based on DAI | |
3469 | * name alone.. | |
3470 | */ | |
3471 | if (link->cpu_name && link->cpu_of_node) { | |
3472 | dev_err(card->dev, | |
3473 | "Neither/both cpu name/of_node are set for %s\n", | |
3474 | link->name); | |
3475 | return -EINVAL; | |
3476 | } | |
3477 | /* | |
3478 | * At least one of CPU DAI name or CPU device name/node must be | |
3479 | * specified | |
5a504963 | 3480 | */ |
bc92657a SW |
3481 | if (!link->cpu_dai_name && |
3482 | !(link->cpu_name || link->cpu_of_node)) { | |
5a504963 | 3483 | dev_err(card->dev, |
bc92657a | 3484 | "Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", |
3b09bb82 | 3485 | link->name); |
5a504963 SW |
3486 | return -EINVAL; |
3487 | } | |
3488 | } | |
3489 | ||
ed77cc12 MB |
3490 | dev_set_drvdata(card->dev, card); |
3491 | ||
111c6419 SW |
3492 | snd_soc_initialize_card_lists(card); |
3493 | ||
150dd2f8 VK |
3494 | soc_init_card_debugfs(card); |
3495 | ||
181a6892 MB |
3496 | card->rtd = devm_kzalloc(card->dev, |
3497 | sizeof(struct snd_soc_pcm_runtime) * | |
3498 | (card->num_links + card->num_aux_devs), | |
3499 | GFP_KERNEL); | |
f0fba2ad LG |
3500 | if (card->rtd == NULL) |
3501 | return -ENOMEM; | |
a7dbb603 | 3502 | card->num_rtd = 0; |
2eea392d | 3503 | card->rtd_aux = &card->rtd[card->num_links]; |
f0fba2ad LG |
3504 | |
3505 | for (i = 0; i < card->num_links; i++) | |
3506 | card->rtd[i].dai_link = &card->dai_link[i]; | |
3507 | ||
c5af3a2e | 3508 | INIT_LIST_HEAD(&card->list); |
db432b41 | 3509 | INIT_LIST_HEAD(&card->dapm_dirty); |
c5af3a2e | 3510 | card->instantiated = 0; |
f0fba2ad | 3511 | mutex_init(&card->mutex); |
a73fb2df | 3512 | mutex_init(&card->dapm_mutex); |
c5af3a2e | 3513 | |
b19e6e7b MB |
3514 | ret = snd_soc_instantiate_card(card); |
3515 | if (ret != 0) | |
3516 | soc_cleanup_card_debugfs(card); | |
c5af3a2e | 3517 | |
b19e6e7b | 3518 | return ret; |
c5af3a2e | 3519 | } |
70a7ca34 | 3520 | EXPORT_SYMBOL_GPL(snd_soc_register_card); |
c5af3a2e MB |
3521 | |
3522 | /** | |
3523 | * snd_soc_unregister_card - Unregister a card with the ASoC core | |
3524 | * | |
ac11a2b3 | 3525 | * @card: Card to unregister |
c5af3a2e | 3526 | * |
c5af3a2e | 3527 | */ |
70a7ca34 | 3528 | int snd_soc_unregister_card(struct snd_soc_card *card) |
c5af3a2e | 3529 | { |
b0e26485 VK |
3530 | if (card->instantiated) |
3531 | soc_cleanup_card_resources(card); | |
c5af3a2e MB |
3532 | dev_dbg(card->dev, "Unregistered card '%s'\n", card->name); |
3533 | ||
3534 | return 0; | |
3535 | } | |
70a7ca34 | 3536 | EXPORT_SYMBOL_GPL(snd_soc_unregister_card); |
c5af3a2e | 3537 | |
f0fba2ad LG |
3538 | /* |
3539 | * Simplify DAI link configuration by removing ".-1" from device names | |
3540 | * and sanitizing names. | |
3541 | */ | |
0b9a214a | 3542 | static char *fmt_single_name(struct device *dev, int *id) |
f0fba2ad LG |
3543 | { |
3544 | char *found, name[NAME_SIZE]; | |
3545 | int id1, id2; | |
3546 | ||
3547 | if (dev_name(dev) == NULL) | |
3548 | return NULL; | |
3549 | ||
58818a77 | 3550 | strlcpy(name, dev_name(dev), NAME_SIZE); |
f0fba2ad LG |
3551 | |
3552 | /* are we a "%s.%d" name (platform and SPI components) */ | |
3553 | found = strstr(name, dev->driver->name); | |
3554 | if (found) { | |
3555 | /* get ID */ | |
3556 | if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { | |
3557 | ||
3558 | /* discard ID from name if ID == -1 */ | |
3559 | if (*id == -1) | |
3560 | found[strlen(dev->driver->name)] = '\0'; | |
3561 | } | |
3562 | ||
3563 | } else { | |
3564 | /* I2C component devices are named "bus-addr" */ | |
3565 | if (sscanf(name, "%x-%x", &id1, &id2) == 2) { | |
3566 | char tmp[NAME_SIZE]; | |
3567 | ||
3568 | /* create unique ID number from I2C addr and bus */ | |
05899446 | 3569 | *id = ((id1 & 0xffff) << 16) + id2; |
f0fba2ad LG |
3570 | |
3571 | /* sanitize component name for DAI link creation */ | |
3572 | snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); | |
58818a77 | 3573 | strlcpy(name, tmp, NAME_SIZE); |
f0fba2ad LG |
3574 | } else |
3575 | *id = 0; | |
3576 | } | |
3577 | ||
3578 | return kstrdup(name, GFP_KERNEL); | |
3579 | } | |
3580 | ||
3581 | /* | |
3582 | * Simplify DAI link naming for single devices with multiple DAIs by removing | |
3583 | * any ".-1" and using the DAI name (instead of device name). | |
3584 | */ | |
3585 | static inline char *fmt_multiple_name(struct device *dev, | |
3586 | struct snd_soc_dai_driver *dai_drv) | |
3587 | { | |
3588 | if (dai_drv->name == NULL) { | |
0837fc62 | 3589 | pr_err("asoc: error - multiple DAI %s registered with no name\n", |
f0fba2ad LG |
3590 | dev_name(dev)); |
3591 | return NULL; | |
3592 | } | |
3593 | ||
3594 | return kstrdup(dai_drv->name, GFP_KERNEL); | |
3595 | } | |
3596 | ||
9115171a MB |
3597 | /** |
3598 | * snd_soc_register_dai - Register a DAI with the ASoC core | |
3599 | * | |
ac11a2b3 | 3600 | * @dai: DAI to register |
9115171a | 3601 | */ |
f0fba2ad LG |
3602 | int snd_soc_register_dai(struct device *dev, |
3603 | struct snd_soc_dai_driver *dai_drv) | |
9115171a | 3604 | { |
054880fe | 3605 | struct snd_soc_codec *codec; |
f0fba2ad LG |
3606 | struct snd_soc_dai *dai; |
3607 | ||
3608 | dev_dbg(dev, "dai register %s\n", dev_name(dev)); | |
9115171a | 3609 | |
f0fba2ad LG |
3610 | dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); |
3611 | if (dai == NULL) | |
a7393623 | 3612 | return -ENOMEM; |
9115171a | 3613 | |
f0fba2ad LG |
3614 | /* create DAI component name */ |
3615 | dai->name = fmt_single_name(dev, &dai->id); | |
3616 | if (dai->name == NULL) { | |
3617 | kfree(dai); | |
3618 | return -ENOMEM; | |
3619 | } | |
6335d055 | 3620 | |
f0fba2ad LG |
3621 | dai->dev = dev; |
3622 | dai->driver = dai_drv; | |
be09ad90 | 3623 | dai->dapm.dev = dev; |
f0fba2ad LG |
3624 | if (!dai->driver->ops) |
3625 | dai->driver->ops = &null_dai_ops; | |
9115171a MB |
3626 | |
3627 | mutex_lock(&client_mutex); | |
054880fe MB |
3628 | |
3629 | list_for_each_entry(codec, &codec_list, list) { | |
3630 | if (codec->dev == dev) { | |
3631 | dev_dbg(dev, "Mapped DAI %s to CODEC %s\n", | |
3632 | dai->name, codec->name); | |
3633 | dai->codec = codec; | |
3634 | break; | |
3635 | } | |
3636 | } | |
3637 | ||
9115171a | 3638 | list_add(&dai->list, &dai_list); |
054880fe | 3639 | |
9115171a MB |
3640 | mutex_unlock(&client_mutex); |
3641 | ||
3642 | pr_debug("Registered DAI '%s'\n", dai->name); | |
3643 | ||
3644 | return 0; | |
3645 | } | |
3646 | EXPORT_SYMBOL_GPL(snd_soc_register_dai); | |
3647 | ||
3648 | /** | |
3649 | * snd_soc_unregister_dai - Unregister a DAI from the ASoC core | |
3650 | * | |
ac11a2b3 | 3651 | * @dai: DAI to unregister |
9115171a | 3652 | */ |
f0fba2ad | 3653 | void snd_soc_unregister_dai(struct device *dev) |
9115171a | 3654 | { |
f0fba2ad LG |
3655 | struct snd_soc_dai *dai; |
3656 | ||
3657 | list_for_each_entry(dai, &dai_list, list) { | |
3658 | if (dev == dai->dev) | |
3659 | goto found; | |
3660 | } | |
3661 | return; | |
3662 | ||
3663 | found: | |
9115171a MB |
3664 | mutex_lock(&client_mutex); |
3665 | list_del(&dai->list); | |
3666 | mutex_unlock(&client_mutex); | |
3667 | ||
3668 | pr_debug("Unregistered DAI '%s'\n", dai->name); | |
f0fba2ad LG |
3669 | kfree(dai->name); |
3670 | kfree(dai); | |
9115171a MB |
3671 | } |
3672 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); | |
3673 | ||
3674 | /** | |
3675 | * snd_soc_register_dais - Register multiple DAIs with the ASoC core | |
3676 | * | |
ac11a2b3 MB |
3677 | * @dai: Array of DAIs to register |
3678 | * @count: Number of DAIs | |
9115171a | 3679 | */ |
f0fba2ad LG |
3680 | int snd_soc_register_dais(struct device *dev, |
3681 | struct snd_soc_dai_driver *dai_drv, size_t count) | |
9115171a | 3682 | { |
054880fe | 3683 | struct snd_soc_codec *codec; |
f0fba2ad LG |
3684 | struct snd_soc_dai *dai; |
3685 | int i, ret = 0; | |
3686 | ||
720ffa4c | 3687 | dev_dbg(dev, "dai register %s #%Zu\n", dev_name(dev), count); |
9115171a MB |
3688 | |
3689 | for (i = 0; i < count; i++) { | |
f0fba2ad LG |
3690 | |
3691 | dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); | |
c46e0079 AL |
3692 | if (dai == NULL) { |
3693 | ret = -ENOMEM; | |
3694 | goto err; | |
3695 | } | |
f0fba2ad LG |
3696 | |
3697 | /* create DAI component name */ | |
3698 | dai->name = fmt_multiple_name(dev, &dai_drv[i]); | |
3699 | if (dai->name == NULL) { | |
3700 | kfree(dai); | |
3701 | ret = -EINVAL; | |
9115171a | 3702 | goto err; |
f0fba2ad LG |
3703 | } |
3704 | ||
3705 | dai->dev = dev; | |
f0fba2ad | 3706 | dai->driver = &dai_drv[i]; |
0f9141c9 MB |
3707 | if (dai->driver->id) |
3708 | dai->id = dai->driver->id; | |
3709 | else | |
3710 | dai->id = i; | |
be09ad90 | 3711 | dai->dapm.dev = dev; |
f0fba2ad LG |
3712 | if (!dai->driver->ops) |
3713 | dai->driver->ops = &null_dai_ops; | |
3714 | ||
3715 | mutex_lock(&client_mutex); | |
054880fe MB |
3716 | |
3717 | list_for_each_entry(codec, &codec_list, list) { | |
3718 | if (codec->dev == dev) { | |
3719 | dev_dbg(dev, "Mapped DAI %s to CODEC %s\n", | |
3720 | dai->name, codec->name); | |
3721 | dai->codec = codec; | |
3722 | break; | |
3723 | } | |
3724 | } | |
3725 | ||
f0fba2ad | 3726 | list_add(&dai->list, &dai_list); |
054880fe | 3727 | |
f0fba2ad LG |
3728 | mutex_unlock(&client_mutex); |
3729 | ||
3730 | pr_debug("Registered DAI '%s'\n", dai->name); | |
9115171a MB |
3731 | } |
3732 | ||
3733 | return 0; | |
3734 | ||
3735 | err: | |
3736 | for (i--; i >= 0; i--) | |
f0fba2ad | 3737 | snd_soc_unregister_dai(dev); |
9115171a MB |
3738 | |
3739 | return ret; | |
3740 | } | |
3741 | EXPORT_SYMBOL_GPL(snd_soc_register_dais); | |
3742 | ||
3743 | /** | |
3744 | * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core | |
3745 | * | |
ac11a2b3 MB |
3746 | * @dai: Array of DAIs to unregister |
3747 | * @count: Number of DAIs | |
9115171a | 3748 | */ |
f0fba2ad | 3749 | void snd_soc_unregister_dais(struct device *dev, size_t count) |
9115171a MB |
3750 | { |
3751 | int i; | |
3752 | ||
3753 | for (i = 0; i < count; i++) | |
f0fba2ad | 3754 | snd_soc_unregister_dai(dev); |
9115171a MB |
3755 | } |
3756 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dais); | |
3757 | ||
12a48a8c MB |
3758 | /** |
3759 | * snd_soc_register_platform - Register a platform with the ASoC core | |
3760 | * | |
ac11a2b3 | 3761 | * @platform: platform to register |
12a48a8c | 3762 | */ |
f0fba2ad LG |
3763 | int snd_soc_register_platform(struct device *dev, |
3764 | struct snd_soc_platform_driver *platform_drv) | |
12a48a8c | 3765 | { |
f0fba2ad LG |
3766 | struct snd_soc_platform *platform; |
3767 | ||
3768 | dev_dbg(dev, "platform register %s\n", dev_name(dev)); | |
3769 | ||
3770 | platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); | |
3771 | if (platform == NULL) | |
a7393623 | 3772 | return -ENOMEM; |
f0fba2ad LG |
3773 | |
3774 | /* create platform component name */ | |
3775 | platform->name = fmt_single_name(dev, &platform->id); | |
3776 | if (platform->name == NULL) { | |
3777 | kfree(platform); | |
3778 | return -ENOMEM; | |
3779 | } | |
12a48a8c | 3780 | |
f0fba2ad LG |
3781 | platform->dev = dev; |
3782 | platform->driver = platform_drv; | |
b7950641 LG |
3783 | platform->dapm.dev = dev; |
3784 | platform->dapm.platform = platform; | |
64a648c2 | 3785 | platform->dapm.stream_event = platform_drv->stream_event; |
cc22d37e | 3786 | mutex_init(&platform->mutex); |
12a48a8c MB |
3787 | |
3788 | mutex_lock(&client_mutex); | |
3789 | list_add(&platform->list, &platform_list); | |
3790 | mutex_unlock(&client_mutex); | |
3791 | ||
3792 | pr_debug("Registered platform '%s'\n", platform->name); | |
3793 | ||
3794 | return 0; | |
3795 | } | |
3796 | EXPORT_SYMBOL_GPL(snd_soc_register_platform); | |
3797 | ||
3798 | /** | |
3799 | * snd_soc_unregister_platform - Unregister a platform from the ASoC core | |
3800 | * | |
ac11a2b3 | 3801 | * @platform: platform to unregister |
12a48a8c | 3802 | */ |
f0fba2ad | 3803 | void snd_soc_unregister_platform(struct device *dev) |
12a48a8c | 3804 | { |
f0fba2ad LG |
3805 | struct snd_soc_platform *platform; |
3806 | ||
3807 | list_for_each_entry(platform, &platform_list, list) { | |
3808 | if (dev == platform->dev) | |
3809 | goto found; | |
3810 | } | |
3811 | return; | |
3812 | ||
3813 | found: | |
12a48a8c MB |
3814 | mutex_lock(&client_mutex); |
3815 | list_del(&platform->list); | |
3816 | mutex_unlock(&client_mutex); | |
3817 | ||
3818 | pr_debug("Unregistered platform '%s'\n", platform->name); | |
f0fba2ad LG |
3819 | kfree(platform->name); |
3820 | kfree(platform); | |
12a48a8c MB |
3821 | } |
3822 | EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); | |
3823 | ||
151ab22c MB |
3824 | static u64 codec_format_map[] = { |
3825 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, | |
3826 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, | |
3827 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, | |
3828 | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, | |
3829 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, | |
3830 | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, | |
3831 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
3832 | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
3833 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, | |
3834 | SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, | |
3835 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, | |
3836 | SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, | |
3837 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, | |
3838 | SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, | |
3839 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | |
3840 | | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, | |
3841 | }; | |
3842 | ||
3843 | /* Fix up the DAI formats for endianness: codecs don't actually see | |
3844 | * the endianness of the data but we're using the CPU format | |
3845 | * definitions which do need to include endianness so we ensure that | |
3846 | * codec DAIs always have both big and little endian variants set. | |
3847 | */ | |
3848 | static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) | |
3849 | { | |
3850 | int i; | |
3851 | ||
3852 | for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) | |
3853 | if (stream->formats & codec_format_map[i]) | |
3854 | stream->formats |= codec_format_map[i]; | |
3855 | } | |
3856 | ||
0d0cf00a MB |
3857 | /** |
3858 | * snd_soc_register_codec - Register a codec with the ASoC core | |
3859 | * | |
ac11a2b3 | 3860 | * @codec: codec to register |
0d0cf00a | 3861 | */ |
f0fba2ad | 3862 | int snd_soc_register_codec(struct device *dev, |
001ae4c0 MB |
3863 | const struct snd_soc_codec_driver *codec_drv, |
3864 | struct snd_soc_dai_driver *dai_drv, | |
3865 | int num_dai) | |
0d0cf00a | 3866 | { |
3335ddca | 3867 | size_t reg_size; |
f0fba2ad LG |
3868 | struct snd_soc_codec *codec; |
3869 | int ret, i; | |
151ab22c | 3870 | |
f0fba2ad LG |
3871 | dev_dbg(dev, "codec register %s\n", dev_name(dev)); |
3872 | ||
3873 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); | |
3874 | if (codec == NULL) | |
3875 | return -ENOMEM; | |
0d0cf00a | 3876 | |
f0fba2ad LG |
3877 | /* create CODEC component name */ |
3878 | codec->name = fmt_single_name(dev, &codec->id); | |
3879 | if (codec->name == NULL) { | |
3880 | kfree(codec); | |
3881 | return -ENOMEM; | |
3882 | } | |
3883 | ||
23bbce34 DP |
3884 | if (codec_drv->compress_type) |
3885 | codec->compress_type = codec_drv->compress_type; | |
3886 | else | |
3887 | codec->compress_type = SND_SOC_FLAT_COMPRESSION; | |
3888 | ||
c3acec26 MB |
3889 | codec->write = codec_drv->write; |
3890 | codec->read = codec_drv->read; | |
1500b7b5 DP |
3891 | codec->volatile_register = codec_drv->volatile_register; |
3892 | codec->readable_register = codec_drv->readable_register; | |
8020454c | 3893 | codec->writable_register = codec_drv->writable_register; |
5124e69e | 3894 | codec->ignore_pmdown_time = codec_drv->ignore_pmdown_time; |
ce6120cc LG |
3895 | codec->dapm.bias_level = SND_SOC_BIAS_OFF; |
3896 | codec->dapm.dev = dev; | |
3897 | codec->dapm.codec = codec; | |
474b62d6 | 3898 | codec->dapm.seq_notifier = codec_drv->seq_notifier; |
64a648c2 | 3899 | codec->dapm.stream_event = codec_drv->stream_event; |
7a30a3db DP |
3900 | codec->dev = dev; |
3901 | codec->driver = codec_drv; | |
3902 | codec->num_dai = num_dai; | |
3903 | mutex_init(&codec->mutex); | |
ce6120cc | 3904 | |
f0fba2ad LG |
3905 | /* allocate CODEC register cache */ |
3906 | if (codec_drv->reg_cache_size && codec_drv->reg_word_size) { | |
3335ddca | 3907 | reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size; |
aea170a0 | 3908 | codec->reg_size = reg_size; |
3335ddca DP |
3909 | /* it is necessary to make a copy of the default register cache |
3910 | * because in the case of using a compression type that requires | |
3911 | * the default register cache to be marked as __devinitconst the | |
3912 | * kernel might have freed the array by the time we initialize | |
3913 | * the cache. | |
3914 | */ | |
2aa86323 DP |
3915 | if (codec_drv->reg_cache_default) { |
3916 | codec->reg_def_copy = kmemdup(codec_drv->reg_cache_default, | |
3917 | reg_size, GFP_KERNEL); | |
3918 | if (!codec->reg_def_copy) { | |
3919 | ret = -ENOMEM; | |
3920 | goto fail; | |
3921 | } | |
f0fba2ad | 3922 | } |
151ab22c MB |
3923 | } |
3924 | ||
1500b7b5 DP |
3925 | if (codec_drv->reg_access_size && codec_drv->reg_access_default) { |
3926 | if (!codec->volatile_register) | |
3927 | codec->volatile_register = snd_soc_default_volatile_register; | |
3928 | if (!codec->readable_register) | |
3929 | codec->readable_register = snd_soc_default_readable_register; | |
8020454c DP |
3930 | if (!codec->writable_register) |
3931 | codec->writable_register = snd_soc_default_writable_register; | |
1500b7b5 DP |
3932 | } |
3933 | ||
f0fba2ad LG |
3934 | for (i = 0; i < num_dai; i++) { |
3935 | fixup_codec_formats(&dai_drv[i].playback); | |
3936 | fixup_codec_formats(&dai_drv[i].capture); | |
3937 | } | |
3938 | ||
054880fe MB |
3939 | mutex_lock(&client_mutex); |
3940 | list_add(&codec->list, &codec_list); | |
3941 | mutex_unlock(&client_mutex); | |
3942 | ||
26b01ccd MB |
3943 | /* register any DAIs */ |
3944 | if (num_dai) { | |
3945 | ret = snd_soc_register_dais(dev, dai_drv, num_dai); | |
3946 | if (ret < 0) | |
054880fe MB |
3947 | dev_err(codec->dev, "Failed to regster DAIs: %d\n", |
3948 | ret); | |
26b01ccd | 3949 | } |
f0fba2ad | 3950 | |
0d0cf00a | 3951 | pr_debug("Registered codec '%s'\n", codec->name); |
0d0cf00a | 3952 | return 0; |
f0fba2ad | 3953 | |
fdf0f54d | 3954 | fail: |
3335ddca DP |
3955 | kfree(codec->reg_def_copy); |
3956 | codec->reg_def_copy = NULL; | |
f0fba2ad LG |
3957 | kfree(codec->name); |
3958 | kfree(codec); | |
3959 | return ret; | |
0d0cf00a MB |
3960 | } |
3961 | EXPORT_SYMBOL_GPL(snd_soc_register_codec); | |
3962 | ||
3963 | /** | |
3964 | * snd_soc_unregister_codec - Unregister a codec from the ASoC core | |
3965 | * | |
ac11a2b3 | 3966 | * @codec: codec to unregister |
0d0cf00a | 3967 | */ |
f0fba2ad | 3968 | void snd_soc_unregister_codec(struct device *dev) |
0d0cf00a | 3969 | { |
f0fba2ad LG |
3970 | struct snd_soc_codec *codec; |
3971 | int i; | |
3972 | ||
3973 | list_for_each_entry(codec, &codec_list, list) { | |
3974 | if (dev == codec->dev) | |
3975 | goto found; | |
3976 | } | |
3977 | return; | |
3978 | ||
3979 | found: | |
26b01ccd MB |
3980 | if (codec->num_dai) |
3981 | for (i = 0; i < codec->num_dai; i++) | |
3982 | snd_soc_unregister_dai(dev); | |
f0fba2ad | 3983 | |
0d0cf00a MB |
3984 | mutex_lock(&client_mutex); |
3985 | list_del(&codec->list); | |
3986 | mutex_unlock(&client_mutex); | |
3987 | ||
3988 | pr_debug("Unregistered codec '%s'\n", codec->name); | |
f0fba2ad | 3989 | |
7a30a3db | 3990 | snd_soc_cache_exit(codec); |
3335ddca | 3991 | kfree(codec->reg_def_copy); |
1aafcd4d | 3992 | kfree(codec->name); |
f0fba2ad | 3993 | kfree(codec); |
0d0cf00a MB |
3994 | } |
3995 | EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); | |
3996 | ||
bec4fa05 SW |
3997 | /* Retrieve a card's name from device tree */ |
3998 | int snd_soc_of_parse_card_name(struct snd_soc_card *card, | |
3999 | const char *propname) | |
4000 | { | |
4001 | struct device_node *np = card->dev->of_node; | |
4002 | int ret; | |
4003 | ||
4004 | ret = of_property_read_string_index(np, propname, 0, &card->name); | |
4005 | /* | |
4006 | * EINVAL means the property does not exist. This is fine providing | |
4007 | * card->name was previously set, which is checked later in | |
4008 | * snd_soc_register_card. | |
4009 | */ | |
4010 | if (ret < 0 && ret != -EINVAL) { | |
4011 | dev_err(card->dev, | |
4012 | "Property '%s' could not be read: %d\n", | |
4013 | propname, ret); | |
4014 | return ret; | |
4015 | } | |
4016 | ||
4017 | return 0; | |
4018 | } | |
4019 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); | |
4020 | ||
a4a54dd5 SW |
4021 | int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, |
4022 | const char *propname) | |
4023 | { | |
4024 | struct device_node *np = card->dev->of_node; | |
4025 | int num_routes; | |
4026 | struct snd_soc_dapm_route *routes; | |
4027 | int i, ret; | |
4028 | ||
4029 | num_routes = of_property_count_strings(np, propname); | |
c34ce320 | 4030 | if (num_routes < 0 || num_routes & 1) { |
a4a54dd5 | 4031 | dev_err(card->dev, |
c34ce320 RZ |
4032 | "Property '%s' does not exist or its length is not even\n", |
4033 | propname); | |
a4a54dd5 SW |
4034 | return -EINVAL; |
4035 | } | |
4036 | num_routes /= 2; | |
4037 | if (!num_routes) { | |
4038 | dev_err(card->dev, | |
4039 | "Property '%s's length is zero\n", | |
4040 | propname); | |
4041 | return -EINVAL; | |
4042 | } | |
4043 | ||
4044 | routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), | |
4045 | GFP_KERNEL); | |
4046 | if (!routes) { | |
4047 | dev_err(card->dev, | |
4048 | "Could not allocate DAPM route table\n"); | |
4049 | return -EINVAL; | |
4050 | } | |
4051 | ||
4052 | for (i = 0; i < num_routes; i++) { | |
4053 | ret = of_property_read_string_index(np, propname, | |
4054 | 2 * i, &routes[i].sink); | |
4055 | if (ret) { | |
4056 | dev_err(card->dev, | |
4057 | "Property '%s' index %d could not be read: %d\n", | |
4058 | propname, 2 * i, ret); | |
4059 | return -EINVAL; | |
4060 | } | |
4061 | ret = of_property_read_string_index(np, propname, | |
4062 | (2 * i) + 1, &routes[i].source); | |
4063 | if (ret) { | |
4064 | dev_err(card->dev, | |
4065 | "Property '%s' index %d could not be read: %d\n", | |
4066 | propname, (2 * i) + 1, ret); | |
4067 | return -EINVAL; | |
4068 | } | |
4069 | } | |
4070 | ||
4071 | card->num_dapm_routes = num_routes; | |
4072 | card->dapm_routes = routes; | |
4073 | ||
4074 | return 0; | |
4075 | } | |
4076 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); | |
4077 | ||
c9b3a40f | 4078 | static int __init snd_soc_init(void) |
db2a4165 | 4079 | { |
384c89e2 | 4080 | #ifdef CONFIG_DEBUG_FS |
8a9dab1a MB |
4081 | snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); |
4082 | if (IS_ERR(snd_soc_debugfs_root) || !snd_soc_debugfs_root) { | |
0837fc62 | 4083 | pr_warn("ASoC: Failed to create debugfs directory\n"); |
8a9dab1a | 4084 | snd_soc_debugfs_root = NULL; |
384c89e2 | 4085 | } |
c3c5a19a | 4086 | |
8a9dab1a | 4087 | if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, |
c3c5a19a MB |
4088 | &codec_list_fops)) |
4089 | pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); | |
4090 | ||
8a9dab1a | 4091 | if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, |
f3208780 MB |
4092 | &dai_list_fops)) |
4093 | pr_warn("ASoC: Failed to create DAI list debugfs file\n"); | |
19c7ac27 | 4094 | |
8a9dab1a | 4095 | if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, |
19c7ac27 MB |
4096 | &platform_list_fops)) |
4097 | pr_warn("ASoC: Failed to create platform list debugfs file\n"); | |
384c89e2 MB |
4098 | #endif |
4099 | ||
fb257897 MB |
4100 | snd_soc_util_init(); |
4101 | ||
db2a4165 FM |
4102 | return platform_driver_register(&soc_driver); |
4103 | } | |
4abe8e16 | 4104 | module_init(snd_soc_init); |
db2a4165 | 4105 | |
7d8c16a6 | 4106 | static void __exit snd_soc_exit(void) |
db2a4165 | 4107 | { |
fb257897 MB |
4108 | snd_soc_util_exit(); |
4109 | ||
384c89e2 | 4110 | #ifdef CONFIG_DEBUG_FS |
8a9dab1a | 4111 | debugfs_remove_recursive(snd_soc_debugfs_root); |
384c89e2 | 4112 | #endif |
3ff3f64b | 4113 | platform_driver_unregister(&soc_driver); |
db2a4165 | 4114 | } |
db2a4165 FM |
4115 | module_exit(snd_soc_exit); |
4116 | ||
4117 | /* Module information */ | |
d331124d | 4118 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
db2a4165 FM |
4119 | MODULE_DESCRIPTION("ALSA SoC Core"); |
4120 | MODULE_LICENSE("GPL"); | |
8b45a209 | 4121 | MODULE_ALIAS("platform:soc-audio"); |