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> |
741a509f | 33 | #include <linux/pinctrl/consumer.h> |
f0e8ed85 | 34 | #include <linux/ctype.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
bec4fa05 | 36 | #include <linux/of.h> |
a180e8b9 | 37 | #include <linux/of_graph.h> |
345233d7 | 38 | #include <linux/dmi.h> |
db2a4165 | 39 | #include <sound/core.h> |
3028eb8c | 40 | #include <sound/jack.h> |
db2a4165 FM |
41 | #include <sound/pcm.h> |
42 | #include <sound/pcm_params.h> | |
43 | #include <sound/soc.h> | |
01d7584c | 44 | #include <sound/soc-dpcm.h> |
8a978234 | 45 | #include <sound/soc-topology.h> |
db2a4165 FM |
46 | #include <sound/initval.h> |
47 | ||
a8b1d34f MB |
48 | #define CREATE_TRACE_POINTS |
49 | #include <trace/events/asoc.h> | |
50 | ||
f0fba2ad LG |
51 | #define NAME_SIZE 32 |
52 | ||
384c89e2 | 53 | #ifdef CONFIG_DEBUG_FS |
8a9dab1a MB |
54 | struct dentry *snd_soc_debugfs_root; |
55 | EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); | |
384c89e2 MB |
56 | #endif |
57 | ||
c5af3a2e | 58 | static DEFINE_MUTEX(client_mutex); |
12a48a8c | 59 | static LIST_HEAD(platform_list); |
0d0cf00a | 60 | static LIST_HEAD(codec_list); |
030e79f6 | 61 | static LIST_HEAD(component_list); |
c5af3a2e | 62 | |
db2a4165 FM |
63 | /* |
64 | * This is a timeout to do a DAPM powerdown after a stream is closed(). | |
65 | * It can be used to eliminate pops between different playback streams, e.g. | |
66 | * between two audio tracks. | |
67 | */ | |
68 | static int pmdown_time = 5000; | |
69 | module_param(pmdown_time, int, 0); | |
70 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); | |
71 | ||
98faf436 ML |
72 | /* If a DMI filed contain strings in this blacklist (e.g. |
73 | * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken | |
74 | * as invalid and dropped when setting the card long name from DMI info. | |
75 | */ | |
76 | static const char * const dmi_blacklist[] = { | |
77 | "To be filled by OEM", | |
78 | "TBD by OEM", | |
79 | "Default String", | |
80 | "Board Manufacturer", | |
81 | "Board Vendor Name", | |
82 | "Board Product Name", | |
83 | NULL, /* terminator */ | |
84 | }; | |
85 | ||
2bc9a81e DP |
86 | /* returns the minimum number of bytes needed to represent |
87 | * a particular given value */ | |
88 | static int min_bytes_needed(unsigned long val) | |
89 | { | |
90 | int c = 0; | |
91 | int i; | |
92 | ||
93 | for (i = (sizeof val * 8) - 1; i >= 0; --i, ++c) | |
94 | if (val & (1UL << i)) | |
95 | break; | |
96 | c = (sizeof val * 8) - c; | |
97 | if (!c || (c % 8)) | |
98 | c = (c + 8) / 8; | |
99 | else | |
100 | c /= 8; | |
101 | return c; | |
102 | } | |
103 | ||
13fd179f DP |
104 | /* fill buf which is 'len' bytes with a formatted |
105 | * string of the form 'reg: value\n' */ | |
106 | static int format_register_str(struct snd_soc_codec *codec, | |
107 | unsigned int reg, char *buf, size_t len) | |
108 | { | |
00b317a4 SW |
109 | int wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; |
110 | int regsize = codec->driver->reg_word_size * 2; | |
13fd179f | 111 | int ret; |
13fd179f DP |
112 | |
113 | /* +2 for ': ' and + 1 for '\n' */ | |
114 | if (wordsize + regsize + 2 + 1 != len) | |
115 | return -EINVAL; | |
116 | ||
d6b6c2ca TI |
117 | sprintf(buf, "%.*x: ", wordsize, reg); |
118 | buf += wordsize + 2; | |
13fd179f | 119 | |
d6b6c2ca TI |
120 | ret = snd_soc_read(codec, reg); |
121 | if (ret < 0) | |
122 | memset(buf, 'X', regsize); | |
123 | else | |
124 | sprintf(buf, "%.*x", regsize, ret); | |
125 | buf[regsize] = '\n'; | |
126 | /* no NUL-termination needed */ | |
13fd179f DP |
127 | return 0; |
128 | } | |
129 | ||
2624d5fa | 130 | /* codec register dump */ |
13fd179f DP |
131 | static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf, |
132 | size_t count, loff_t pos) | |
2624d5fa | 133 | { |
13fd179f | 134 | int i, step = 1; |
2bc9a81e | 135 | int wordsize, regsize; |
13fd179f DP |
136 | int len; |
137 | size_t total = 0; | |
138 | loff_t p = 0; | |
2bc9a81e | 139 | |
00b317a4 SW |
140 | wordsize = min_bytes_needed(codec->driver->reg_cache_size) * 2; |
141 | regsize = codec->driver->reg_word_size * 2; | |
2624d5fa | 142 | |
13fd179f DP |
143 | len = wordsize + regsize + 2 + 1; |
144 | ||
f0fba2ad | 145 | if (!codec->driver->reg_cache_size) |
2624d5fa MB |
146 | return 0; |
147 | ||
f0fba2ad LG |
148 | if (codec->driver->reg_cache_step) |
149 | step = codec->driver->reg_cache_step; | |
2624d5fa | 150 | |
f0fba2ad | 151 | for (i = 0; i < codec->driver->reg_cache_size; i += step) { |
20a0ec27 LPC |
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; | |
5164d74d | 159 | } |
20a0ec27 | 160 | p += len; |
2624d5fa MB |
161 | } |
162 | ||
13fd179f | 163 | total = min(total, count - 1); |
2624d5fa | 164 | |
13fd179f | 165 | return total; |
2624d5fa | 166 | } |
13fd179f | 167 | |
2624d5fa MB |
168 | static ssize_t codec_reg_show(struct device *dev, |
169 | struct device_attribute *attr, char *buf) | |
170 | { | |
36ae1a96 | 171 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
f0fba2ad | 172 | |
13fd179f | 173 | return soc_codec_reg_show(rtd->codec, buf, PAGE_SIZE, 0); |
2624d5fa MB |
174 | } |
175 | ||
176 | static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL); | |
177 | ||
dbe21408 MB |
178 | static ssize_t pmdown_time_show(struct device *dev, |
179 | struct device_attribute *attr, char *buf) | |
180 | { | |
36ae1a96 | 181 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
dbe21408 | 182 | |
f0fba2ad | 183 | return sprintf(buf, "%ld\n", rtd->pmdown_time); |
dbe21408 MB |
184 | } |
185 | ||
186 | static ssize_t pmdown_time_set(struct device *dev, | |
187 | struct device_attribute *attr, | |
188 | const char *buf, size_t count) | |
189 | { | |
36ae1a96 | 190 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
c593b520 | 191 | int ret; |
dbe21408 | 192 | |
b785a492 | 193 | ret = kstrtol(buf, 10, &rtd->pmdown_time); |
c593b520 MB |
194 | if (ret) |
195 | return ret; | |
dbe21408 MB |
196 | |
197 | return count; | |
198 | } | |
199 | ||
200 | static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); | |
201 | ||
d29697dc TI |
202 | static struct attribute *soc_dev_attrs[] = { |
203 | &dev_attr_codec_reg.attr, | |
204 | &dev_attr_pmdown_time.attr, | |
205 | NULL | |
206 | }; | |
207 | ||
208 | static umode_t soc_dev_attr_is_visible(struct kobject *kobj, | |
209 | struct attribute *attr, int idx) | |
210 | { | |
211 | struct device *dev = kobj_to_dev(kobj); | |
212 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); | |
213 | ||
214 | if (attr == &dev_attr_pmdown_time.attr) | |
215 | return attr->mode; /* always visible */ | |
216 | return rtd->codec ? attr->mode : 0; /* enabled only with codec */ | |
217 | } | |
218 | ||
219 | static const struct attribute_group soc_dapm_dev_group = { | |
220 | .attrs = soc_dapm_dev_attrs, | |
221 | .is_visible = soc_dev_attr_is_visible, | |
222 | }; | |
223 | ||
224 | static const struct attribute_group soc_dev_roup = { | |
225 | .attrs = soc_dev_attrs, | |
226 | .is_visible = soc_dev_attr_is_visible, | |
227 | }; | |
228 | ||
229 | static const struct attribute_group *soc_dev_attr_groups[] = { | |
230 | &soc_dapm_dev_group, | |
231 | &soc_dev_roup, | |
232 | NULL | |
233 | }; | |
234 | ||
2624d5fa | 235 | #ifdef CONFIG_DEBUG_FS |
2624d5fa | 236 | static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf, |
13fd179f | 237 | size_t count, loff_t *ppos) |
2624d5fa MB |
238 | { |
239 | ssize_t ret; | |
240 | struct snd_soc_codec *codec = file->private_data; | |
13fd179f DP |
241 | char *buf; |
242 | ||
243 | if (*ppos < 0 || !count) | |
244 | return -EINVAL; | |
245 | ||
246 | buf = kmalloc(count, GFP_KERNEL); | |
2624d5fa MB |
247 | if (!buf) |
248 | return -ENOMEM; | |
13fd179f DP |
249 | |
250 | ret = soc_codec_reg_show(codec, buf, count, *ppos); | |
251 | if (ret >= 0) { | |
252 | if (copy_to_user(user_buf, buf, ret)) { | |
253 | kfree(buf); | |
254 | return -EFAULT; | |
255 | } | |
256 | *ppos += ret; | |
257 | } | |
258 | ||
2624d5fa MB |
259 | kfree(buf); |
260 | return ret; | |
261 | } | |
262 | ||
263 | static ssize_t codec_reg_write_file(struct file *file, | |
264 | const char __user *user_buf, size_t count, loff_t *ppos) | |
265 | { | |
266 | char buf[32]; | |
34e268d8 | 267 | size_t buf_size; |
2624d5fa MB |
268 | char *start = buf; |
269 | unsigned long reg, value; | |
2624d5fa | 270 | struct snd_soc_codec *codec = file->private_data; |
b785a492 | 271 | int ret; |
2624d5fa MB |
272 | |
273 | buf_size = min(count, (sizeof(buf)-1)); | |
274 | if (copy_from_user(buf, user_buf, buf_size)) | |
275 | return -EFAULT; | |
276 | buf[buf_size] = 0; | |
2624d5fa MB |
277 | |
278 | while (*start == ' ') | |
279 | start++; | |
280 | reg = simple_strtoul(start, &start, 16); | |
2624d5fa MB |
281 | while (*start == ' ') |
282 | start++; | |
b785a492 JH |
283 | ret = kstrtoul(start, 16, &value); |
284 | if (ret) | |
285 | return ret; | |
0d51a9cb MB |
286 | |
287 | /* Userspace has been fiddling around behind the kernel's back */ | |
373d4d09 | 288 | add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); |
0d51a9cb | 289 | |
e4f078d8 | 290 | snd_soc_write(codec, reg, value); |
2624d5fa MB |
291 | return buf_size; |
292 | } | |
293 | ||
294 | static const struct file_operations codec_reg_fops = { | |
234e3405 | 295 | .open = simple_open, |
2624d5fa MB |
296 | .read = codec_reg_read_file, |
297 | .write = codec_reg_write_file, | |
6038f373 | 298 | .llseek = default_llseek, |
2624d5fa MB |
299 | }; |
300 | ||
81c7cfd1 | 301 | static void soc_init_component_debugfs(struct snd_soc_component *component) |
e73f3de5 | 302 | { |
6553bf06 LPC |
303 | if (!component->card->debugfs_card_root) |
304 | return; | |
305 | ||
81c7cfd1 LPC |
306 | if (component->debugfs_prefix) { |
307 | char *name; | |
e73f3de5 | 308 | |
81c7cfd1 LPC |
309 | name = kasprintf(GFP_KERNEL, "%s:%s", |
310 | component->debugfs_prefix, component->name); | |
311 | if (name) { | |
312 | component->debugfs_root = debugfs_create_dir(name, | |
313 | component->card->debugfs_card_root); | |
314 | kfree(name); | |
315 | } | |
316 | } else { | |
317 | component->debugfs_root = debugfs_create_dir(component->name, | |
318 | component->card->debugfs_card_root); | |
319 | } | |
e73f3de5 | 320 | |
81c7cfd1 LPC |
321 | if (!component->debugfs_root) { |
322 | dev_warn(component->dev, | |
323 | "ASoC: Failed to create component debugfs directory\n"); | |
324 | return; | |
325 | } | |
e73f3de5 | 326 | |
81c7cfd1 LPC |
327 | snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), |
328 | component->debugfs_root); | |
e73f3de5 | 329 | |
81c7cfd1 LPC |
330 | if (component->init_debugfs) |
331 | component->init_debugfs(component); | |
e73f3de5 RK |
332 | } |
333 | ||
81c7cfd1 | 334 | static void soc_cleanup_component_debugfs(struct snd_soc_component *component) |
2624d5fa | 335 | { |
81c7cfd1 LPC |
336 | debugfs_remove_recursive(component->debugfs_root); |
337 | } | |
d6ce4cf3 | 338 | |
81c7cfd1 LPC |
339 | static void soc_init_codec_debugfs(struct snd_soc_component *component) |
340 | { | |
341 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
353c64dd | 342 | struct dentry *debugfs_reg; |
2624d5fa | 343 | |
353c64dd FE |
344 | debugfs_reg = debugfs_create_file("codec_reg", 0644, |
345 | codec->component.debugfs_root, | |
346 | codec, &codec_reg_fops); | |
347 | if (!debugfs_reg) | |
10e8aa9a MM |
348 | dev_warn(codec->dev, |
349 | "ASoC: Failed to create codec register debugfs file\n"); | |
731f1ab2 SG |
350 | } |
351 | ||
c3c5a19a MB |
352 | static ssize_t codec_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; |
c3c5a19a MB |
357 | struct snd_soc_codec *codec; |
358 | ||
359 | if (!buf) | |
360 | return -ENOMEM; | |
361 | ||
34e81ab4 LPC |
362 | mutex_lock(&client_mutex); |
363 | ||
2b194f9d MB |
364 | list_for_each_entry(codec, &codec_list, list) { |
365 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", | |
f4333203 | 366 | codec->component.name); |
2b194f9d MB |
367 | if (len >= 0) |
368 | ret += len; | |
369 | if (ret > PAGE_SIZE) { | |
370 | ret = PAGE_SIZE; | |
371 | break; | |
372 | } | |
373 | } | |
c3c5a19a | 374 | |
34e81ab4 LPC |
375 | mutex_unlock(&client_mutex); |
376 | ||
c3c5a19a MB |
377 | if (ret >= 0) |
378 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
379 | ||
380 | kfree(buf); | |
381 | ||
382 | return ret; | |
383 | } | |
384 | ||
385 | static const struct file_operations codec_list_fops = { | |
386 | .read = codec_list_read_file, | |
387 | .llseek = default_llseek,/* read accesses f_pos */ | |
388 | }; | |
389 | ||
f3208780 MB |
390 | static ssize_t dai_list_read_file(struct file *file, char __user *user_buf, |
391 | size_t count, loff_t *ppos) | |
392 | { | |
393 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
2b194f9d | 394 | ssize_t len, ret = 0; |
1438c2f6 | 395 | struct snd_soc_component *component; |
f3208780 MB |
396 | struct snd_soc_dai *dai; |
397 | ||
398 | if (!buf) | |
399 | return -ENOMEM; | |
400 | ||
34e81ab4 LPC |
401 | mutex_lock(&client_mutex); |
402 | ||
1438c2f6 LPC |
403 | list_for_each_entry(component, &component_list, list) { |
404 | list_for_each_entry(dai, &component->dai_list, list) { | |
405 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", | |
406 | dai->name); | |
407 | if (len >= 0) | |
408 | ret += len; | |
409 | if (ret > PAGE_SIZE) { | |
410 | ret = PAGE_SIZE; | |
411 | break; | |
412 | } | |
2b194f9d MB |
413 | } |
414 | } | |
f3208780 | 415 | |
34e81ab4 LPC |
416 | mutex_unlock(&client_mutex); |
417 | ||
2b194f9d | 418 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
f3208780 MB |
419 | |
420 | kfree(buf); | |
421 | ||
422 | return ret; | |
423 | } | |
424 | ||
425 | static const struct file_operations dai_list_fops = { | |
426 | .read = dai_list_read_file, | |
427 | .llseek = default_llseek,/* read accesses f_pos */ | |
428 | }; | |
429 | ||
19c7ac27 MB |
430 | static ssize_t platform_list_read_file(struct file *file, |
431 | char __user *user_buf, | |
432 | size_t count, loff_t *ppos) | |
433 | { | |
434 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
2b194f9d | 435 | ssize_t len, ret = 0; |
19c7ac27 MB |
436 | struct snd_soc_platform *platform; |
437 | ||
438 | if (!buf) | |
439 | return -ENOMEM; | |
440 | ||
34e81ab4 LPC |
441 | mutex_lock(&client_mutex); |
442 | ||
2b194f9d MB |
443 | list_for_each_entry(platform, &platform_list, list) { |
444 | len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", | |
f4333203 | 445 | platform->component.name); |
2b194f9d MB |
446 | if (len >= 0) |
447 | ret += len; | |
448 | if (ret > PAGE_SIZE) { | |
449 | ret = PAGE_SIZE; | |
450 | break; | |
451 | } | |
452 | } | |
19c7ac27 | 453 | |
34e81ab4 LPC |
454 | mutex_unlock(&client_mutex); |
455 | ||
2b194f9d | 456 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
19c7ac27 MB |
457 | |
458 | kfree(buf); | |
459 | ||
460 | return ret; | |
461 | } | |
462 | ||
463 | static const struct file_operations platform_list_fops = { | |
464 | .read = platform_list_read_file, | |
465 | .llseek = default_llseek,/* read accesses f_pos */ | |
466 | }; | |
467 | ||
a6052154 JN |
468 | static void soc_init_card_debugfs(struct snd_soc_card *card) |
469 | { | |
6553bf06 LPC |
470 | if (!snd_soc_debugfs_root) |
471 | return; | |
472 | ||
a6052154 | 473 | card->debugfs_card_root = debugfs_create_dir(card->name, |
8a9dab1a | 474 | snd_soc_debugfs_root); |
3a45b867 | 475 | if (!card->debugfs_card_root) { |
a6052154 | 476 | dev_warn(card->dev, |
7c08be84 | 477 | "ASoC: Failed to create card debugfs directory\n"); |
3a45b867 JN |
478 | return; |
479 | } | |
480 | ||
481 | card->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0644, | |
482 | card->debugfs_card_root, | |
483 | &card->pop_time); | |
484 | if (!card->debugfs_pop_time) | |
485 | dev_warn(card->dev, | |
f110bfc7 | 486 | "ASoC: Failed to create pop time debugfs file\n"); |
a6052154 JN |
487 | } |
488 | ||
489 | static void soc_cleanup_card_debugfs(struct snd_soc_card *card) | |
490 | { | |
491 | debugfs_remove_recursive(card->debugfs_card_root); | |
492 | } | |
493 | ||
6553bf06 LPC |
494 | |
495 | static void snd_soc_debugfs_init(void) | |
496 | { | |
497 | snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); | |
d9a02c55 | 498 | if (IS_ERR_OR_NULL(snd_soc_debugfs_root)) { |
6553bf06 LPC |
499 | pr_warn("ASoC: Failed to create debugfs directory\n"); |
500 | snd_soc_debugfs_root = NULL; | |
501 | return; | |
502 | } | |
503 | ||
504 | if (!debugfs_create_file("codecs", 0444, snd_soc_debugfs_root, NULL, | |
505 | &codec_list_fops)) | |
506 | pr_warn("ASoC: Failed to create CODEC list debugfs file\n"); | |
507 | ||
508 | if (!debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, | |
509 | &dai_list_fops)) | |
510 | pr_warn("ASoC: Failed to create DAI list debugfs file\n"); | |
511 | ||
512 | if (!debugfs_create_file("platforms", 0444, snd_soc_debugfs_root, NULL, | |
513 | &platform_list_fops)) | |
514 | pr_warn("ASoC: Failed to create platform list debugfs file\n"); | |
515 | } | |
516 | ||
517 | static void snd_soc_debugfs_exit(void) | |
518 | { | |
519 | debugfs_remove_recursive(snd_soc_debugfs_root); | |
520 | } | |
521 | ||
2624d5fa MB |
522 | #else |
523 | ||
81c7cfd1 | 524 | #define soc_init_codec_debugfs NULL |
b95fccbc | 525 | |
81c7cfd1 LPC |
526 | static inline void soc_init_component_debugfs( |
527 | struct snd_soc_component *component) | |
731f1ab2 SG |
528 | { |
529 | } | |
530 | ||
81c7cfd1 LPC |
531 | static inline void soc_cleanup_component_debugfs( |
532 | struct snd_soc_component *component) | |
731f1ab2 SG |
533 | { |
534 | } | |
535 | ||
b95fccbc AL |
536 | static inline void soc_init_card_debugfs(struct snd_soc_card *card) |
537 | { | |
538 | } | |
539 | ||
540 | static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) | |
541 | { | |
542 | } | |
6553bf06 LPC |
543 | |
544 | static inline void snd_soc_debugfs_init(void) | |
545 | { | |
546 | } | |
547 | ||
548 | static inline void snd_soc_debugfs_exit(void) | |
549 | { | |
550 | } | |
551 | ||
2624d5fa MB |
552 | #endif |
553 | ||
a0ac4411 KM |
554 | static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd, |
555 | struct snd_soc_component *component) | |
556 | { | |
557 | struct snd_soc_rtdcom_list *rtdcom; | |
558 | struct snd_soc_rtdcom_list *new_rtdcom; | |
559 | ||
560 | for_each_rtdcom(rtd, rtdcom) { | |
561 | /* already connected */ | |
562 | if (rtdcom->component == component) | |
563 | return 0; | |
564 | } | |
565 | ||
566 | new_rtdcom = kmalloc(sizeof(*new_rtdcom), GFP_KERNEL); | |
567 | if (!new_rtdcom) | |
568 | return -ENOMEM; | |
569 | ||
570 | new_rtdcom->component = component; | |
571 | INIT_LIST_HEAD(&new_rtdcom->list); | |
572 | ||
573 | list_add_tail(&new_rtdcom->list, &rtd->component_list); | |
574 | ||
575 | return 0; | |
576 | } | |
577 | ||
578 | static void snd_soc_rtdcom_del_all(struct snd_soc_pcm_runtime *rtd) | |
579 | { | |
580 | struct snd_soc_rtdcom_list *rtdcom1, *rtdcom2; | |
581 | ||
582 | for_each_rtdcom_safe(rtd, rtdcom1, rtdcom2) | |
583 | kfree(rtdcom1); | |
584 | ||
585 | INIT_LIST_HEAD(&rtd->component_list); | |
586 | } | |
587 | ||
588 | struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, | |
589 | const char *driver_name) | |
590 | { | |
591 | struct snd_soc_rtdcom_list *rtdcom; | |
592 | ||
593 | for_each_rtdcom(rtd, rtdcom) { | |
594 | if ((rtdcom->component->driver->name == driver_name) || | |
595 | strcmp(rtdcom->component->driver->name, driver_name) == 0) | |
596 | return rtdcom->component; | |
597 | } | |
598 | ||
599 | return NULL; | |
600 | } | |
601 | ||
47c88fff LG |
602 | struct snd_pcm_substream *snd_soc_get_dai_substream(struct snd_soc_card *card, |
603 | const char *dai_link, int stream) | |
604 | { | |
1a497983 | 605 | struct snd_soc_pcm_runtime *rtd; |
47c88fff | 606 | |
1a497983 ML |
607 | list_for_each_entry(rtd, &card->rtd_list, list) { |
608 | if (rtd->dai_link->no_pcm && | |
609 | !strcmp(rtd->dai_link->name, dai_link)) | |
610 | return rtd->pcm->streams[stream].substream; | |
47c88fff | 611 | } |
f110bfc7 | 612 | dev_dbg(card->dev, "ASoC: failed to find dai link %s\n", dai_link); |
47c88fff LG |
613 | return NULL; |
614 | } | |
615 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_substream); | |
616 | ||
1a497983 ML |
617 | static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( |
618 | struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) | |
619 | { | |
620 | struct snd_soc_pcm_runtime *rtd; | |
621 | ||
622 | rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL); | |
623 | if (!rtd) | |
624 | return NULL; | |
625 | ||
a0ac4411 | 626 | INIT_LIST_HEAD(&rtd->component_list); |
1a497983 ML |
627 | rtd->card = card; |
628 | rtd->dai_link = dai_link; | |
629 | rtd->codec_dais = kzalloc(sizeof(struct snd_soc_dai *) * | |
630 | dai_link->num_codecs, | |
631 | GFP_KERNEL); | |
632 | if (!rtd->codec_dais) { | |
633 | kfree(rtd); | |
634 | return NULL; | |
635 | } | |
636 | ||
637 | return rtd; | |
638 | } | |
639 | ||
640 | static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) | |
641 | { | |
642 | if (rtd && rtd->codec_dais) | |
643 | kfree(rtd->codec_dais); | |
a0ac4411 | 644 | snd_soc_rtdcom_del_all(rtd); |
1a497983 ML |
645 | kfree(rtd); |
646 | } | |
647 | ||
648 | static void soc_add_pcm_runtime(struct snd_soc_card *card, | |
649 | struct snd_soc_pcm_runtime *rtd) | |
650 | { | |
651 | list_add_tail(&rtd->list, &card->rtd_list); | |
652 | rtd->num = card->num_rtd; | |
653 | card->num_rtd++; | |
654 | } | |
655 | ||
656 | static void soc_remove_pcm_runtimes(struct snd_soc_card *card) | |
657 | { | |
658 | struct snd_soc_pcm_runtime *rtd, *_rtd; | |
659 | ||
660 | list_for_each_entry_safe(rtd, _rtd, &card->rtd_list, list) { | |
661 | list_del(&rtd->list); | |
662 | soc_free_pcm_runtime(rtd); | |
663 | } | |
664 | ||
665 | card->num_rtd = 0; | |
666 | } | |
667 | ||
47c88fff LG |
668 | struct snd_soc_pcm_runtime *snd_soc_get_pcm_runtime(struct snd_soc_card *card, |
669 | const char *dai_link) | |
670 | { | |
1a497983 | 671 | struct snd_soc_pcm_runtime *rtd; |
47c88fff | 672 | |
1a497983 ML |
673 | list_for_each_entry(rtd, &card->rtd_list, list) { |
674 | if (!strcmp(rtd->dai_link->name, dai_link)) | |
675 | return rtd; | |
47c88fff | 676 | } |
f110bfc7 | 677 | dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link); |
47c88fff LG |
678 | return NULL; |
679 | } | |
680 | EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); | |
681 | ||
9d58a077 RF |
682 | static void codec2codec_close_delayed_work(struct work_struct *work) |
683 | { | |
684 | /* Currently nothing to do for c2c links | |
685 | * Since c2c links are internal nodes in the DAPM graph and | |
686 | * don't interface with the outside world or application layer | |
687 | * we don't have to do any special handling on close. | |
688 | */ | |
689 | } | |
690 | ||
6f8ab4ac | 691 | #ifdef CONFIG_PM_SLEEP |
db2a4165 | 692 | /* powers down audio subsystem for suspend */ |
6f8ab4ac | 693 | int snd_soc_suspend(struct device *dev) |
db2a4165 | 694 | { |
6f8ab4ac | 695 | struct snd_soc_card *card = dev_get_drvdata(dev); |
d9fc4063 | 696 | struct snd_soc_component *component; |
1a497983 ML |
697 | struct snd_soc_pcm_runtime *rtd; |
698 | int i; | |
db2a4165 | 699 | |
c5599b87 LPC |
700 | /* If the card is not initialized yet there is nothing to do */ |
701 | if (!card->instantiated) | |
e3509ff0 DM |
702 | return 0; |
703 | ||
6ed25978 AG |
704 | /* Due to the resume being scheduled into a workqueue we could |
705 | * suspend before that's finished - wait for it to complete. | |
706 | */ | |
f0fba2ad LG |
707 | snd_power_lock(card->snd_card); |
708 | snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); | |
709 | snd_power_unlock(card->snd_card); | |
6ed25978 AG |
710 | |
711 | /* we're going to block userspace touching us until resume completes */ | |
f0fba2ad | 712 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); |
6ed25978 | 713 | |
a00f90f9 | 714 | /* mute any active DACs */ |
1a497983 | 715 | list_for_each_entry(rtd, &card->rtd_list, list) { |
3efab7dc | 716 | |
1a497983 | 717 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
718 | continue; |
719 | ||
1a497983 ML |
720 | for (i = 0; i < rtd->num_codecs; i++) { |
721 | struct snd_soc_dai *dai = rtd->codec_dais[i]; | |
88bd870f BC |
722 | struct snd_soc_dai_driver *drv = dai->driver; |
723 | ||
724 | if (drv->ops->digital_mute && dai->playback_active) | |
725 | drv->ops->digital_mute(dai, 1); | |
726 | } | |
db2a4165 FM |
727 | } |
728 | ||
4ccab3e7 | 729 | /* suspend all pcms */ |
1a497983 ML |
730 | list_for_each_entry(rtd, &card->rtd_list, list) { |
731 | if (rtd->dai_link->ignore_suspend) | |
3efab7dc MB |
732 | continue; |
733 | ||
1a497983 | 734 | snd_pcm_suspend_all(rtd->pcm); |
3efab7dc | 735 | } |
4ccab3e7 | 736 | |
87506549 | 737 | if (card->suspend_pre) |
70b2ac12 | 738 | card->suspend_pre(card); |
db2a4165 | 739 | |
1a497983 ML |
740 | list_for_each_entry(rtd, &card->rtd_list, list) { |
741 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
3efab7dc | 742 | |
1a497983 | 743 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
744 | continue; |
745 | ||
bc263214 | 746 | if (cpu_dai->driver->suspend && !cpu_dai->driver->bus_control) |
f0fba2ad | 747 | cpu_dai->driver->suspend(cpu_dai); |
db2a4165 FM |
748 | } |
749 | ||
37660b6d | 750 | /* close any waiting streams */ |
1a497983 ML |
751 | list_for_each_entry(rtd, &card->rtd_list, list) |
752 | flush_delayed_work(&rtd->delayed_work); | |
db2a4165 | 753 | |
1a497983 | 754 | list_for_each_entry(rtd, &card->rtd_list, list) { |
3efab7dc | 755 | |
1a497983 | 756 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
757 | continue; |
758 | ||
1a497983 | 759 | snd_soc_dapm_stream_event(rtd, |
7bd3a6f3 | 760 | SNDRV_PCM_STREAM_PLAYBACK, |
7bd3a6f3 | 761 | SND_SOC_DAPM_STREAM_SUSPEND); |
f0fba2ad | 762 | |
1a497983 | 763 | snd_soc_dapm_stream_event(rtd, |
7bd3a6f3 | 764 | SNDRV_PCM_STREAM_CAPTURE, |
7bd3a6f3 | 765 | SND_SOC_DAPM_STREAM_SUSPEND); |
db2a4165 FM |
766 | } |
767 | ||
8be4da29 LPC |
768 | /* Recheck all endpoints too, their state is affected by suspend */ |
769 | dapm_mark_endpoints_dirty(card); | |
e2d32ff6 MB |
770 | snd_soc_dapm_sync(&card->dapm); |
771 | ||
9178feb4 | 772 | /* suspend all COMPONENTs */ |
d9fc4063 KM |
773 | list_for_each_entry(component, &card->component_dev_list, card_list) { |
774 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); | |
d9fc4063 | 775 | |
9178feb4 | 776 | /* If there are paths active then the COMPONENT will be held with |
f0fba2ad | 777 | * bias _ON and should not be suspended. */ |
9178feb4 | 778 | if (!component->suspended) { |
4890140f | 779 | switch (snd_soc_dapm_get_bias_level(dapm)) { |
f0fba2ad | 780 | case SND_SOC_BIAS_STANDBY: |
125a25da | 781 | /* |
9178feb4 | 782 | * If the COMPONENT is capable of idle |
125a25da MB |
783 | * bias off then being in STANDBY |
784 | * means it's doing something, | |
785 | * otherwise fall through. | |
786 | */ | |
4890140f | 787 | if (dapm->idle_bias_off) { |
9178feb4 | 788 | dev_dbg(component->dev, |
10e8aa9a | 789 | "ASoC: idle_bias_off CODEC on over suspend\n"); |
125a25da MB |
790 | break; |
791 | } | |
a8093297 | 792 | |
f0fba2ad | 793 | case SND_SOC_BIAS_OFF: |
9178feb4 KM |
794 | if (component->suspend) |
795 | component->suspend(component); | |
796 | component->suspended = 1; | |
797 | if (component->regmap) | |
798 | regcache_mark_dirty(component->regmap); | |
988e8cc4 | 799 | /* deactivate pins to sleep state */ |
9178feb4 | 800 | pinctrl_pm_select_sleep_state(component->dev); |
f0fba2ad LG |
801 | break; |
802 | default: | |
9178feb4 KM |
803 | dev_dbg(component->dev, |
804 | "ASoC: COMPONENT is on over suspend\n"); | |
f0fba2ad LG |
805 | break; |
806 | } | |
1547aba9 MB |
807 | } |
808 | } | |
db2a4165 | 809 | |
1a497983 ML |
810 | list_for_each_entry(rtd, &card->rtd_list, list) { |
811 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
3efab7dc | 812 | |
1a497983 | 813 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
814 | continue; |
815 | ||
bc263214 | 816 | if (cpu_dai->driver->suspend && cpu_dai->driver->bus_control) |
f0fba2ad | 817 | cpu_dai->driver->suspend(cpu_dai); |
988e8cc4 NC |
818 | |
819 | /* deactivate pins to sleep state */ | |
820 | pinctrl_pm_select_sleep_state(cpu_dai->dev); | |
db2a4165 FM |
821 | } |
822 | ||
87506549 | 823 | if (card->suspend_post) |
70b2ac12 | 824 | card->suspend_post(card); |
db2a4165 FM |
825 | |
826 | return 0; | |
827 | } | |
6f8ab4ac | 828 | EXPORT_SYMBOL_GPL(snd_soc_suspend); |
db2a4165 | 829 | |
6ed25978 AG |
830 | /* deferred resume work, so resume can complete before we finished |
831 | * setting our codec back up, which can be very slow on I2C | |
832 | */ | |
833 | static void soc_resume_deferred(struct work_struct *work) | |
db2a4165 | 834 | { |
f0fba2ad LG |
835 | struct snd_soc_card *card = |
836 | container_of(work, struct snd_soc_card, deferred_resume_work); | |
1a497983 | 837 | struct snd_soc_pcm_runtime *rtd; |
d9fc4063 | 838 | struct snd_soc_component *component; |
1a497983 | 839 | int i; |
db2a4165 | 840 | |
6ed25978 AG |
841 | /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
842 | * so userspace apps are blocked from touching us | |
843 | */ | |
844 | ||
f110bfc7 | 845 | dev_dbg(card->dev, "ASoC: starting resume work\n"); |
6ed25978 | 846 | |
9949788b | 847 | /* Bring us up into D2 so that DAPM starts enabling things */ |
f0fba2ad | 848 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); |
9949788b | 849 | |
87506549 | 850 | if (card->resume_pre) |
70b2ac12 | 851 | card->resume_pre(card); |
db2a4165 | 852 | |
bc263214 | 853 | /* resume control bus DAIs */ |
1a497983 ML |
854 | list_for_each_entry(rtd, &card->rtd_list, list) { |
855 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
3efab7dc | 856 | |
1a497983 | 857 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
858 | continue; |
859 | ||
bc263214 | 860 | if (cpu_dai->driver->resume && cpu_dai->driver->bus_control) |
f0fba2ad LG |
861 | cpu_dai->driver->resume(cpu_dai); |
862 | } | |
863 | ||
d9fc4063 | 864 | list_for_each_entry(component, &card->component_dev_list, card_list) { |
9178feb4 KM |
865 | if (component->suspended) { |
866 | if (component->resume) | |
867 | component->resume(component); | |
868 | component->suspended = 0; | |
1547aba9 MB |
869 | } |
870 | } | |
db2a4165 | 871 | |
1a497983 | 872 | list_for_each_entry(rtd, &card->rtd_list, list) { |
3efab7dc | 873 | |
1a497983 | 874 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
875 | continue; |
876 | ||
1a497983 | 877 | snd_soc_dapm_stream_event(rtd, |
d9b0951b | 878 | SNDRV_PCM_STREAM_PLAYBACK, |
7bd3a6f3 | 879 | SND_SOC_DAPM_STREAM_RESUME); |
f0fba2ad | 880 | |
1a497983 | 881 | snd_soc_dapm_stream_event(rtd, |
d9b0951b | 882 | SNDRV_PCM_STREAM_CAPTURE, |
7bd3a6f3 | 883 | SND_SOC_DAPM_STREAM_RESUME); |
db2a4165 FM |
884 | } |
885 | ||
3ff3f64b | 886 | /* unmute any active DACs */ |
1a497983 | 887 | list_for_each_entry(rtd, &card->rtd_list, list) { |
3efab7dc | 888 | |
1a497983 | 889 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
890 | continue; |
891 | ||
1a497983 ML |
892 | for (i = 0; i < rtd->num_codecs; i++) { |
893 | struct snd_soc_dai *dai = rtd->codec_dais[i]; | |
88bd870f BC |
894 | struct snd_soc_dai_driver *drv = dai->driver; |
895 | ||
896 | if (drv->ops->digital_mute && dai->playback_active) | |
897 | drv->ops->digital_mute(dai, 0); | |
898 | } | |
db2a4165 FM |
899 | } |
900 | ||
1a497983 ML |
901 | list_for_each_entry(rtd, &card->rtd_list, list) { |
902 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
3efab7dc | 903 | |
1a497983 | 904 | if (rtd->dai_link->ignore_suspend) |
3efab7dc MB |
905 | continue; |
906 | ||
bc263214 | 907 | if (cpu_dai->driver->resume && !cpu_dai->driver->bus_control) |
f0fba2ad | 908 | cpu_dai->driver->resume(cpu_dai); |
db2a4165 FM |
909 | } |
910 | ||
87506549 | 911 | if (card->resume_post) |
70b2ac12 | 912 | card->resume_post(card); |
db2a4165 | 913 | |
f110bfc7 | 914 | dev_dbg(card->dev, "ASoC: resume work completed\n"); |
6ed25978 | 915 | |
8be4da29 LPC |
916 | /* Recheck all endpoints too, their state is affected by suspend */ |
917 | dapm_mark_endpoints_dirty(card); | |
e2d32ff6 | 918 | snd_soc_dapm_sync(&card->dapm); |
1a7aaa58 JK |
919 | |
920 | /* userspace can access us now we are back as we were before */ | |
921 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); | |
6ed25978 AG |
922 | } |
923 | ||
924 | /* powers up audio subsystem after a suspend */ | |
6f8ab4ac | 925 | int snd_soc_resume(struct device *dev) |
6ed25978 | 926 | { |
6f8ab4ac | 927 | struct snd_soc_card *card = dev_get_drvdata(dev); |
bc263214 | 928 | bool bus_control = false; |
1a497983 | 929 | struct snd_soc_pcm_runtime *rtd; |
b9dd94a8 | 930 | |
c5599b87 LPC |
931 | /* If the card is not initialized yet there is nothing to do */ |
932 | if (!card->instantiated) | |
5ff1ddf2 EM |
933 | return 0; |
934 | ||
988e8cc4 | 935 | /* activate pins from sleep state */ |
1a497983 | 936 | list_for_each_entry(rtd, &card->rtd_list, list) { |
88bd870f BC |
937 | struct snd_soc_dai **codec_dais = rtd->codec_dais; |
938 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
939 | int j; | |
940 | ||
988e8cc4 NC |
941 | if (cpu_dai->active) |
942 | pinctrl_pm_select_default_state(cpu_dai->dev); | |
88bd870f BC |
943 | |
944 | for (j = 0; j < rtd->num_codecs; j++) { | |
945 | struct snd_soc_dai *codec_dai = codec_dais[j]; | |
946 | if (codec_dai->active) | |
947 | pinctrl_pm_select_default_state(codec_dai->dev); | |
948 | } | |
988e8cc4 NC |
949 | } |
950 | ||
bc263214 LPC |
951 | /* |
952 | * DAIs that also act as the control bus master might have other drivers | |
953 | * hanging off them so need to resume immediately. Other drivers don't | |
954 | * have that problem and may take a substantial amount of time to resume | |
64ab9baa MB |
955 | * due to I/O costs and anti-pop so handle them out of line. |
956 | */ | |
1a497983 ML |
957 | list_for_each_entry(rtd, &card->rtd_list, list) { |
958 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
bc263214 | 959 | bus_control |= cpu_dai->driver->bus_control; |
82e14e8b | 960 | } |
bc263214 LPC |
961 | if (bus_control) { |
962 | dev_dbg(dev, "ASoC: Resuming control bus master immediately\n"); | |
82e14e8b SW |
963 | soc_resume_deferred(&card->deferred_resume_work); |
964 | } else { | |
f110bfc7 | 965 | dev_dbg(dev, "ASoC: Scheduling resume work\n"); |
82e14e8b | 966 | if (!schedule_work(&card->deferred_resume_work)) |
f110bfc7 | 967 | dev_err(dev, "ASoC: resume work item may be lost\n"); |
64ab9baa | 968 | } |
6ed25978 | 969 | |
db2a4165 FM |
970 | return 0; |
971 | } | |
6f8ab4ac | 972 | EXPORT_SYMBOL_GPL(snd_soc_resume); |
db2a4165 | 973 | #else |
6f8ab4ac MB |
974 | #define snd_soc_suspend NULL |
975 | #define snd_soc_resume NULL | |
db2a4165 FM |
976 | #endif |
977 | ||
85e7652d | 978 | static const struct snd_soc_dai_ops null_dai_ops = { |
02a06d30 BS |
979 | }; |
980 | ||
65d9361f LPC |
981 | static struct snd_soc_component *soc_find_component( |
982 | const struct device_node *of_node, const char *name) | |
12023a9a | 983 | { |
65d9361f | 984 | struct snd_soc_component *component; |
12023a9a | 985 | |
34e81ab4 LPC |
986 | lockdep_assert_held(&client_mutex); |
987 | ||
65d9361f LPC |
988 | list_for_each_entry(component, &component_list, list) { |
989 | if (of_node) { | |
990 | if (component->dev->of_node == of_node) | |
991 | return component; | |
992 | } else if (strcmp(component->name, name) == 0) { | |
993 | return component; | |
12023a9a | 994 | } |
12023a9a MLC |
995 | } |
996 | ||
997 | return NULL; | |
998 | } | |
999 | ||
fbb88b5c ML |
1000 | /** |
1001 | * snd_soc_find_dai - Find a registered DAI | |
1002 | * | |
1003 | * @dlc: name of the DAI and optional component info to match | |
1004 | * | |
ad61dd30 | 1005 | * This function will search all registered components and their DAIs to |
fbb88b5c ML |
1006 | * find the DAI of the same name. The component's of_node and name |
1007 | * should also match if being specified. | |
1008 | * | |
1009 | * Return: pointer of DAI, or NULL if not found. | |
1010 | */ | |
305e9020 | 1011 | struct snd_soc_dai *snd_soc_find_dai( |
14621c7e | 1012 | const struct snd_soc_dai_link_component *dlc) |
12023a9a | 1013 | { |
14621c7e LPC |
1014 | struct snd_soc_component *component; |
1015 | struct snd_soc_dai *dai; | |
3e0aa8d8 | 1016 | struct device_node *component_of_node; |
12023a9a | 1017 | |
34e81ab4 LPC |
1018 | lockdep_assert_held(&client_mutex); |
1019 | ||
14621c7e LPC |
1020 | /* Find CPU DAI from registered DAIs*/ |
1021 | list_for_each_entry(component, &component_list, list) { | |
3e0aa8d8 JS |
1022 | component_of_node = component->dev->of_node; |
1023 | if (!component_of_node && component->dev->parent) | |
1024 | component_of_node = component->dev->parent->of_node; | |
1025 | ||
1026 | if (dlc->of_node && component_of_node != dlc->of_node) | |
14621c7e | 1027 | continue; |
1ffae361 | 1028 | if (dlc->name && strcmp(component->name, dlc->name)) |
14621c7e LPC |
1029 | continue; |
1030 | list_for_each_entry(dai, &component->dai_list, list) { | |
1031 | if (dlc->dai_name && strcmp(dai->name, dlc->dai_name)) | |
12023a9a | 1032 | continue; |
12023a9a | 1033 | |
14621c7e | 1034 | return dai; |
12023a9a MLC |
1035 | } |
1036 | } | |
1037 | ||
1038 | return NULL; | |
1039 | } | |
305e9020 | 1040 | EXPORT_SYMBOL_GPL(snd_soc_find_dai); |
12023a9a | 1041 | |
17fb1755 ML |
1042 | |
1043 | /** | |
1044 | * snd_soc_find_dai_link - Find a DAI link | |
1045 | * | |
1046 | * @card: soc card | |
1047 | * @id: DAI link ID to match | |
1048 | * @name: DAI link name to match, optional | |
8abab35f | 1049 | * @stream_name: DAI link stream name to match, optional |
17fb1755 ML |
1050 | * |
1051 | * This function will search all existing DAI links of the soc card to | |
1052 | * find the link of the same ID. Since DAI links may not have their | |
1053 | * unique ID, so name and stream name should also match if being | |
1054 | * specified. | |
1055 | * | |
1056 | * Return: pointer of DAI link, or NULL if not found. | |
1057 | */ | |
1058 | struct snd_soc_dai_link *snd_soc_find_dai_link(struct snd_soc_card *card, | |
1059 | int id, const char *name, | |
1060 | const char *stream_name) | |
1061 | { | |
1062 | struct snd_soc_dai_link *link, *_link; | |
1063 | ||
1064 | lockdep_assert_held(&client_mutex); | |
1065 | ||
1066 | list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { | |
1067 | if (link->id != id) | |
1068 | continue; | |
1069 | ||
1070 | if (name && (!link->name || strcmp(name, link->name))) | |
1071 | continue; | |
1072 | ||
1073 | if (stream_name && (!link->stream_name | |
1074 | || strcmp(stream_name, link->stream_name))) | |
1075 | continue; | |
1076 | ||
1077 | return link; | |
1078 | } | |
1079 | ||
1080 | return NULL; | |
1081 | } | |
1082 | EXPORT_SYMBOL_GPL(snd_soc_find_dai_link); | |
1083 | ||
49a5ba1c ML |
1084 | static bool soc_is_dai_link_bound(struct snd_soc_card *card, |
1085 | struct snd_soc_dai_link *dai_link) | |
1086 | { | |
1087 | struct snd_soc_pcm_runtime *rtd; | |
1088 | ||
1089 | list_for_each_entry(rtd, &card->rtd_list, list) { | |
1090 | if (rtd->dai_link == dai_link) | |
1091 | return true; | |
1092 | } | |
1093 | ||
1094 | return false; | |
1095 | } | |
1096 | ||
6f2f1ff0 ML |
1097 | static int soc_bind_dai_link(struct snd_soc_card *card, |
1098 | struct snd_soc_dai_link *dai_link) | |
db2a4165 | 1099 | { |
1a497983 | 1100 | struct snd_soc_pcm_runtime *rtd; |
88bd870f | 1101 | struct snd_soc_dai_link_component *codecs = dai_link->codecs; |
14621c7e | 1102 | struct snd_soc_dai_link_component cpu_dai_component; |
90be711e | 1103 | struct snd_soc_component *component; |
1a497983 | 1104 | struct snd_soc_dai **codec_dais; |
435c5e25 | 1105 | struct snd_soc_platform *platform; |
06859fca | 1106 | struct device_node *platform_of_node; |
848dd8be | 1107 | const char *platform_name; |
88bd870f | 1108 | int i; |
435c5e25 | 1109 | |
6f2f1ff0 | 1110 | dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); |
435c5e25 | 1111 | |
49a5ba1c ML |
1112 | if (soc_is_dai_link_bound(card, dai_link)) { |
1113 | dev_dbg(card->dev, "ASoC: dai link %s already bound\n", | |
1114 | dai_link->name); | |
1115 | return 0; | |
1116 | } | |
435c5e25 | 1117 | |
513cb311 SM |
1118 | rtd = soc_new_pcm_runtime(card, dai_link); |
1119 | if (!rtd) | |
1120 | return -ENOMEM; | |
1121 | ||
14621c7e LPC |
1122 | cpu_dai_component.name = dai_link->cpu_name; |
1123 | cpu_dai_component.of_node = dai_link->cpu_of_node; | |
1124 | cpu_dai_component.dai_name = dai_link->cpu_dai_name; | |
1125 | rtd->cpu_dai = snd_soc_find_dai(&cpu_dai_component); | |
b19e6e7b | 1126 | if (!rtd->cpu_dai) { |
f110bfc7 | 1127 | dev_err(card->dev, "ASoC: CPU DAI %s not registered\n", |
b19e6e7b | 1128 | dai_link->cpu_dai_name); |
1a497983 | 1129 | goto _err_defer; |
f0fba2ad | 1130 | } |
90be711e | 1131 | snd_soc_rtdcom_add(rtd, rtd->cpu_dai->component); |
f0fba2ad | 1132 | |
88bd870f | 1133 | rtd->num_codecs = dai_link->num_codecs; |
848dd8be | 1134 | |
88bd870f | 1135 | /* Find CODEC from registered CODECs */ |
1a497983 | 1136 | codec_dais = rtd->codec_dais; |
88bd870f | 1137 | for (i = 0; i < rtd->num_codecs; i++) { |
14621c7e | 1138 | codec_dais[i] = snd_soc_find_dai(&codecs[i]); |
88bd870f BC |
1139 | if (!codec_dais[i]) { |
1140 | dev_err(card->dev, "ASoC: CODEC DAI %s not registered\n", | |
1141 | codecs[i].dai_name); | |
1a497983 | 1142 | goto _err_defer; |
88bd870f | 1143 | } |
90be711e | 1144 | snd_soc_rtdcom_add(rtd, codec_dais[i]->component); |
12023a9a MLC |
1145 | } |
1146 | ||
88bd870f BC |
1147 | /* Single codec links expect codec and codec_dai in runtime data */ |
1148 | rtd->codec_dai = codec_dais[0]; | |
1149 | rtd->codec = rtd->codec_dai->codec; | |
1150 | ||
848dd8be MB |
1151 | /* if there's no platform we match on the empty platform */ |
1152 | platform_name = dai_link->platform_name; | |
5a504963 | 1153 | if (!platform_name && !dai_link->platform_of_node) |
848dd8be MB |
1154 | platform_name = "snd-soc-dummy"; |
1155 | ||
90be711e KM |
1156 | /* find one from the set of registered platforms */ |
1157 | list_for_each_entry(component, &component_list, list) { | |
1158 | platform_of_node = component->dev->of_node; | |
1159 | if (!platform_of_node && component->dev->parent->of_node) | |
1160 | platform_of_node = component->dev->parent->of_node; | |
1161 | ||
1162 | if (dai_link->platform_of_node) { | |
1163 | if (platform_of_node != dai_link->platform_of_node) | |
1164 | continue; | |
1165 | } else { | |
1166 | if (strcmp(component->name, platform_name)) | |
1167 | continue; | |
1168 | } | |
1169 | ||
1170 | snd_soc_rtdcom_add(rtd, component); | |
1171 | } | |
1172 | ||
b19e6e7b | 1173 | /* find one from the set of registered platforms */ |
f0fba2ad | 1174 | list_for_each_entry(platform, &platform_list, list) { |
06859fca CK |
1175 | platform_of_node = platform->dev->of_node; |
1176 | if (!platform_of_node && platform->dev->parent->of_node) | |
1177 | platform_of_node = platform->dev->parent->of_node; | |
1178 | ||
5a504963 | 1179 | if (dai_link->platform_of_node) { |
06859fca | 1180 | if (platform_of_node != dai_link->platform_of_node) |
5a504963 SW |
1181 | continue; |
1182 | } else { | |
f4333203 | 1183 | if (strcmp(platform->component.name, platform_name)) |
5a504963 SW |
1184 | continue; |
1185 | } | |
2610ab77 SW |
1186 | |
1187 | rtd->platform = platform; | |
02a06d30 | 1188 | } |
b19e6e7b | 1189 | if (!rtd->platform) { |
f110bfc7 | 1190 | dev_err(card->dev, "ASoC: platform %s not registered\n", |
f0fba2ad | 1191 | dai_link->platform_name); |
70fcad49 | 1192 | goto _err_defer; |
f0fba2ad | 1193 | } |
b19e6e7b | 1194 | |
1a497983 | 1195 | soc_add_pcm_runtime(card, rtd); |
b19e6e7b | 1196 | return 0; |
1a497983 ML |
1197 | |
1198 | _err_defer: | |
1199 | soc_free_pcm_runtime(rtd); | |
1200 | return -EPROBE_DEFER; | |
f0fba2ad LG |
1201 | } |
1202 | ||
f1d45cc3 | 1203 | static void soc_remove_component(struct snd_soc_component *component) |
d12cd198 | 1204 | { |
abd31b32 | 1205 | if (!component->card) |
70090bbb | 1206 | return; |
d12cd198 | 1207 | |
d9fc4063 | 1208 | list_del(&component->card_list); |
589c3563 | 1209 | |
f1d45cc3 LPC |
1210 | if (component->remove) |
1211 | component->remove(component); | |
589c3563 | 1212 | |
f1d45cc3 | 1213 | snd_soc_dapm_free(snd_soc_component_get_dapm(component)); |
589c3563 | 1214 | |
f1d45cc3 | 1215 | soc_cleanup_component_debugfs(component); |
abd31b32 | 1216 | component->card = NULL; |
f1d45cc3 | 1217 | module_put(component->dev->driver->owner); |
589c3563 JN |
1218 | } |
1219 | ||
e60cd14f | 1220 | static void soc_remove_dai(struct snd_soc_dai *dai, int order) |
f0fba2ad | 1221 | { |
f0fba2ad LG |
1222 | int err; |
1223 | ||
e60cd14f LPC |
1224 | if (dai && dai->probed && |
1225 | dai->driver->remove_order == order) { | |
1226 | if (dai->driver->remove) { | |
1227 | err = dai->driver->remove(dai); | |
f0fba2ad | 1228 | if (err < 0) |
e60cd14f | 1229 | dev_err(dai->dev, |
f110bfc7 | 1230 | "ASoC: failed to remove %s: %d\n", |
e60cd14f | 1231 | dai->name, err); |
6b05eda6 | 1232 | } |
e60cd14f | 1233 | dai->probed = 0; |
f0fba2ad | 1234 | } |
b0aa88af MLC |
1235 | } |
1236 | ||
1a497983 ML |
1237 | static void soc_remove_link_dais(struct snd_soc_card *card, |
1238 | struct snd_soc_pcm_runtime *rtd, int order) | |
b0aa88af | 1239 | { |
e60cd14f | 1240 | int i; |
b0aa88af MLC |
1241 | |
1242 | /* unregister the rtd device */ | |
1243 | if (rtd->dev_registered) { | |
b0aa88af MLC |
1244 | device_unregister(rtd->dev); |
1245 | rtd->dev_registered = 0; | |
1246 | } | |
1247 | ||
1248 | /* remove the CODEC DAI */ | |
88bd870f | 1249 | for (i = 0; i < rtd->num_codecs; i++) |
e60cd14f | 1250 | soc_remove_dai(rtd->codec_dais[i], order); |
6b05eda6 | 1251 | |
e60cd14f | 1252 | soc_remove_dai(rtd->cpu_dai, order); |
f0fba2ad | 1253 | } |
db2a4165 | 1254 | |
1a497983 ML |
1255 | static void soc_remove_link_components(struct snd_soc_card *card, |
1256 | struct snd_soc_pcm_runtime *rtd, int order) | |
62ae68fa | 1257 | { |
61aca564 | 1258 | struct snd_soc_component *component; |
90be711e | 1259 | struct snd_soc_rtdcom_list *rtdcom; |
62ae68fa | 1260 | |
90be711e KM |
1261 | for_each_rtdcom(rtd, rtdcom) { |
1262 | component = rtdcom->component; | |
62ae68fa | 1263 | |
70090bbb | 1264 | if (component->driver->remove_order == order) |
61aca564 | 1265 | soc_remove_component(component); |
62ae68fa | 1266 | } |
62ae68fa SW |
1267 | } |
1268 | ||
0671fd8e KM |
1269 | static void soc_remove_dai_links(struct snd_soc_card *card) |
1270 | { | |
1a497983 ML |
1271 | int order; |
1272 | struct snd_soc_pcm_runtime *rtd; | |
f8f80361 | 1273 | struct snd_soc_dai_link *link, *_link; |
0671fd8e | 1274 | |
0168bf0d LG |
1275 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; |
1276 | order++) { | |
1a497983 ML |
1277 | list_for_each_entry(rtd, &card->rtd_list, list) |
1278 | soc_remove_link_dais(card, rtd, order); | |
62ae68fa SW |
1279 | } |
1280 | ||
1281 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; | |
1282 | order++) { | |
1a497983 ML |
1283 | list_for_each_entry(rtd, &card->rtd_list, list) |
1284 | soc_remove_link_components(card, rtd, order); | |
0168bf0d | 1285 | } |
62ae68fa | 1286 | |
f8f80361 ML |
1287 | list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { |
1288 | if (link->dobj.type == SND_SOC_DOBJ_DAI_LINK) | |
1289 | dev_warn(card->dev, "Topology forgot to remove link %s?\n", | |
1290 | link->name); | |
1291 | ||
1292 | list_del(&link->list); | |
1293 | card->num_dai_links--; | |
1294 | } | |
0671fd8e KM |
1295 | } |
1296 | ||
923c5e61 ML |
1297 | static int snd_soc_init_multicodec(struct snd_soc_card *card, |
1298 | struct snd_soc_dai_link *dai_link) | |
1299 | { | |
1300 | /* Legacy codec/codec_dai link is a single entry in multicodec */ | |
1301 | if (dai_link->codec_name || dai_link->codec_of_node || | |
1302 | dai_link->codec_dai_name) { | |
1303 | dai_link->num_codecs = 1; | |
1304 | ||
1305 | dai_link->codecs = devm_kzalloc(card->dev, | |
1306 | sizeof(struct snd_soc_dai_link_component), | |
1307 | GFP_KERNEL); | |
1308 | if (!dai_link->codecs) | |
1309 | return -ENOMEM; | |
1310 | ||
1311 | dai_link->codecs[0].name = dai_link->codec_name; | |
1312 | dai_link->codecs[0].of_node = dai_link->codec_of_node; | |
1313 | dai_link->codecs[0].dai_name = dai_link->codec_dai_name; | |
1314 | } | |
1315 | ||
1316 | if (!dai_link->codecs) { | |
1317 | dev_err(card->dev, "ASoC: DAI link has no CODECs\n"); | |
1318 | return -EINVAL; | |
1319 | } | |
1320 | ||
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | static int soc_init_dai_link(struct snd_soc_card *card, | |
1325 | struct snd_soc_dai_link *link) | |
1326 | { | |
1327 | int i, ret; | |
1328 | ||
1329 | ret = snd_soc_init_multicodec(card, link); | |
1330 | if (ret) { | |
1331 | dev_err(card->dev, "ASoC: failed to init multicodec\n"); | |
1332 | return ret; | |
1333 | } | |
1334 | ||
1335 | for (i = 0; i < link->num_codecs; i++) { | |
1336 | /* | |
1337 | * Codec must be specified by 1 of name or OF node, | |
1338 | * not both or neither. | |
1339 | */ | |
1340 | if (!!link->codecs[i].name == | |
1341 | !!link->codecs[i].of_node) { | |
1342 | dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", | |
1343 | link->name); | |
1344 | return -EINVAL; | |
1345 | } | |
1346 | /* Codec DAI name must be specified */ | |
1347 | if (!link->codecs[i].dai_name) { | |
1348 | dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", | |
1349 | link->name); | |
1350 | return -EINVAL; | |
1351 | } | |
1352 | } | |
1353 | ||
1354 | /* | |
1355 | * Platform may be specified by either name or OF node, but | |
1356 | * can be left unspecified, and a dummy platform will be used. | |
1357 | */ | |
1358 | if (link->platform_name && link->platform_of_node) { | |
1359 | dev_err(card->dev, | |
1360 | "ASoC: Both platform name/of_node are set for %s\n", | |
1361 | link->name); | |
1362 | return -EINVAL; | |
1363 | } | |
1364 | ||
1365 | /* | |
1366 | * CPU device may be specified by either name or OF node, but | |
1367 | * can be left unspecified, and will be matched based on DAI | |
1368 | * name alone.. | |
1369 | */ | |
1370 | if (link->cpu_name && link->cpu_of_node) { | |
1371 | dev_err(card->dev, | |
1372 | "ASoC: Neither/both cpu name/of_node are set for %s\n", | |
1373 | link->name); | |
1374 | return -EINVAL; | |
1375 | } | |
1376 | /* | |
1377 | * At least one of CPU DAI name or CPU device name/node must be | |
1378 | * specified | |
1379 | */ | |
1380 | if (!link->cpu_dai_name && | |
1381 | !(link->cpu_name || link->cpu_of_node)) { | |
1382 | dev_err(card->dev, | |
1383 | "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", | |
1384 | link->name); | |
1385 | return -EINVAL; | |
1386 | } | |
1387 | ||
1388 | return 0; | |
0671fd8e KM |
1389 | } |
1390 | ||
f8f80361 ML |
1391 | /** |
1392 | * snd_soc_add_dai_link - Add a DAI link dynamically | |
1393 | * @card: The ASoC card to which the DAI link is added | |
1394 | * @dai_link: The new DAI link to add | |
1395 | * | |
1396 | * This function adds a DAI link to the ASoC card's link list. | |
1397 | * | |
1398 | * Note: Topology can use this API to add DAI links when probing the | |
1399 | * topology component. And machine drivers can still define static | |
1400 | * DAI links in dai_link array. | |
1401 | */ | |
1402 | int snd_soc_add_dai_link(struct snd_soc_card *card, | |
1403 | struct snd_soc_dai_link *dai_link) | |
1404 | { | |
1405 | if (dai_link->dobj.type | |
1406 | && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) { | |
1407 | dev_err(card->dev, "Invalid dai link type %d\n", | |
1408 | dai_link->dobj.type); | |
1409 | return -EINVAL; | |
1410 | } | |
1411 | ||
1412 | lockdep_assert_held(&client_mutex); | |
d6f220ea ML |
1413 | /* Notify the machine driver for extra initialization |
1414 | * on the link created by topology. | |
1415 | */ | |
1416 | if (dai_link->dobj.type && card->add_dai_link) | |
1417 | card->add_dai_link(card, dai_link); | |
1418 | ||
f8f80361 ML |
1419 | list_add_tail(&dai_link->list, &card->dai_link_list); |
1420 | card->num_dai_links++; | |
1421 | ||
1422 | return 0; | |
1423 | } | |
1424 | EXPORT_SYMBOL_GPL(snd_soc_add_dai_link); | |
1425 | ||
1426 | /** | |
1427 | * snd_soc_remove_dai_link - Remove a DAI link from the list | |
1428 | * @card: The ASoC card that owns the link | |
1429 | * @dai_link: The DAI link to remove | |
1430 | * | |
1431 | * This function removes a DAI link from the ASoC card's link list. | |
1432 | * | |
1433 | * For DAI links previously added by topology, topology should | |
1434 | * remove them by using the dobj embedded in the link. | |
1435 | */ | |
1436 | void snd_soc_remove_dai_link(struct snd_soc_card *card, | |
1437 | struct snd_soc_dai_link *dai_link) | |
1438 | { | |
1439 | struct snd_soc_dai_link *link, *_link; | |
1440 | ||
1441 | if (dai_link->dobj.type | |
1442 | && dai_link->dobj.type != SND_SOC_DOBJ_DAI_LINK) { | |
1443 | dev_err(card->dev, "Invalid dai link type %d\n", | |
1444 | dai_link->dobj.type); | |
1445 | return; | |
1446 | } | |
1447 | ||
1448 | lockdep_assert_held(&client_mutex); | |
d6f220ea ML |
1449 | /* Notify the machine driver for extra destruction |
1450 | * on the link created by topology. | |
1451 | */ | |
1452 | if (dai_link->dobj.type && card->remove_dai_link) | |
1453 | card->remove_dai_link(card, dai_link); | |
1454 | ||
f8f80361 ML |
1455 | list_for_each_entry_safe(link, _link, &card->dai_link_list, list) { |
1456 | if (link == dai_link) { | |
1457 | list_del(&link->list); | |
1458 | card->num_dai_links--; | |
1459 | return; | |
1460 | } | |
1461 | } | |
1462 | } | |
1463 | EXPORT_SYMBOL_GPL(snd_soc_remove_dai_link); | |
1464 | ||
ead9b919 | 1465 | static void soc_set_name_prefix(struct snd_soc_card *card, |
94f99c87 | 1466 | struct snd_soc_component *component) |
ead9b919 JN |
1467 | { |
1468 | int i; | |
1469 | ||
ff819b83 | 1470 | if (card->codec_conf == NULL) |
ead9b919 JN |
1471 | return; |
1472 | ||
ff819b83 DP |
1473 | for (i = 0; i < card->num_configs; i++) { |
1474 | struct snd_soc_codec_conf *map = &card->codec_conf[i]; | |
94f99c87 | 1475 | if (map->of_node && component->dev->of_node != map->of_node) |
3ca041ed | 1476 | continue; |
94f99c87 | 1477 | if (map->dev_name && strcmp(component->name, map->dev_name)) |
3ca041ed | 1478 | continue; |
94f99c87 | 1479 | component->name_prefix = map->name_prefix; |
3ca041ed | 1480 | break; |
ead9b919 JN |
1481 | } |
1482 | } | |
1483 | ||
f1d45cc3 LPC |
1484 | static int soc_probe_component(struct snd_soc_card *card, |
1485 | struct snd_soc_component *component) | |
589c3563 | 1486 | { |
f1d45cc3 | 1487 | struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); |
888df395 | 1488 | struct snd_soc_dai *dai; |
f1d45cc3 | 1489 | int ret; |
589c3563 | 1490 | |
1b7c1231 | 1491 | if (!strcmp(component->name, "snd-soc-dummy")) |
70090bbb | 1492 | return 0; |
589c3563 | 1493 | |
abd31b32 | 1494 | if (component->card) { |
1b7c1231 LPC |
1495 | if (component->card != card) { |
1496 | dev_err(component->dev, | |
1497 | "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", | |
1498 | card->name, component->card->name); | |
1499 | return -ENODEV; | |
1500 | } | |
1501 | return 0; | |
1502 | } | |
589c3563 | 1503 | |
f1d45cc3 | 1504 | if (!try_module_get(component->dev->driver->owner)) |
70d29331 JN |
1505 | return -ENODEV; |
1506 | ||
f1d45cc3 LPC |
1507 | component->card = card; |
1508 | dapm->card = card; | |
1509 | soc_set_name_prefix(card, component); | |
589c3563 | 1510 | |
f1d45cc3 | 1511 | soc_init_component_debugfs(component); |
d5d1e0be | 1512 | |
f1d45cc3 LPC |
1513 | if (component->dapm_widgets) { |
1514 | ret = snd_soc_dapm_new_controls(dapm, component->dapm_widgets, | |
1515 | component->num_dapm_widgets); | |
b318ad50 NP |
1516 | |
1517 | if (ret != 0) { | |
f1d45cc3 | 1518 | dev_err(component->dev, |
b318ad50 NP |
1519 | "Failed to create new controls %d\n", ret); |
1520 | goto err_probe; | |
1521 | } | |
1522 | } | |
77530150 | 1523 | |
0634814f LPC |
1524 | list_for_each_entry(dai, &component->dai_list, list) { |
1525 | ret = snd_soc_dapm_new_dai_widgets(dapm, dai); | |
261edc70 | 1526 | if (ret != 0) { |
0634814f | 1527 | dev_err(component->dev, |
261edc70 NP |
1528 | "Failed to create DAI widgets %d\n", ret); |
1529 | goto err_probe; | |
1530 | } | |
1531 | } | |
888df395 | 1532 | |
f1d45cc3 LPC |
1533 | if (component->probe) { |
1534 | ret = component->probe(component); | |
589c3563 | 1535 | if (ret < 0) { |
f1d45cc3 LPC |
1536 | dev_err(component->dev, |
1537 | "ASoC: failed to probe component %d\n", ret); | |
70d29331 | 1538 | goto err_probe; |
589c3563 | 1539 | } |
cb2cf612 | 1540 | |
f1d45cc3 LPC |
1541 | WARN(dapm->idle_bias_off && |
1542 | dapm->bias_level != SND_SOC_BIAS_OFF, | |
1543 | "codec %s can not start from non-off bias with idle_bias_off==1\n", | |
1544 | component->name); | |
be09ad90 LG |
1545 | } |
1546 | ||
f2ed6b07 ML |
1547 | /* machine specific init */ |
1548 | if (component->init) { | |
1549 | ret = component->init(component); | |
1550 | if (ret < 0) { | |
1551 | dev_err(component->dev, | |
1552 | "Failed to do machine specific init %d\n", ret); | |
1553 | goto err_probe; | |
1554 | } | |
1555 | } | |
1556 | ||
f1d45cc3 LPC |
1557 | if (component->controls) |
1558 | snd_soc_add_component_controls(component, component->controls, | |
1559 | component->num_controls); | |
1560 | if (component->dapm_routes) | |
1561 | snd_soc_dapm_add_routes(dapm, component->dapm_routes, | |
1562 | component->num_dapm_routes); | |
3fec6b6d | 1563 | |
f1d45cc3 | 1564 | list_add(&dapm->list, &card->dapm_list); |
d9fc4063 | 1565 | list_add(&component->card_list, &card->component_dev_list); |
956245e9 LG |
1566 | |
1567 | return 0; | |
1568 | ||
1569 | err_probe: | |
f1d45cc3 | 1570 | soc_cleanup_component_debugfs(component); |
abd31b32 | 1571 | component->card = NULL; |
f1d45cc3 | 1572 | module_put(component->dev->driver->owner); |
956245e9 LG |
1573 | |
1574 | return ret; | |
1575 | } | |
1576 | ||
36ae1a96 MB |
1577 | static void rtd_release(struct device *dev) |
1578 | { | |
1579 | kfree(dev); | |
1580 | } | |
f0fba2ad | 1581 | |
5f3484ac LPC |
1582 | static int soc_post_component_init(struct snd_soc_pcm_runtime *rtd, |
1583 | const char *name) | |
503ae5e0 | 1584 | { |
589c3563 JN |
1585 | int ret = 0; |
1586 | ||
589c3563 | 1587 | /* register the rtd device */ |
36ae1a96 MB |
1588 | rtd->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
1589 | if (!rtd->dev) | |
1590 | return -ENOMEM; | |
1591 | device_initialize(rtd->dev); | |
5f3484ac | 1592 | rtd->dev->parent = rtd->card->dev; |
36ae1a96 | 1593 | rtd->dev->release = rtd_release; |
d29697dc | 1594 | rtd->dev->groups = soc_dev_attr_groups; |
f294afed | 1595 | dev_set_name(rtd->dev, "%s", name); |
36ae1a96 | 1596 | dev_set_drvdata(rtd->dev, rtd); |
b8c0dab9 | 1597 | mutex_init(&rtd->pcm_mutex); |
01d7584c LG |
1598 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); |
1599 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); | |
1600 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); | |
1601 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); | |
36ae1a96 | 1602 | ret = device_add(rtd->dev); |
589c3563 | 1603 | if (ret < 0) { |
865df9cb CL |
1604 | /* calling put_device() here to free the rtd->dev */ |
1605 | put_device(rtd->dev); | |
5f3484ac | 1606 | dev_err(rtd->card->dev, |
f110bfc7 | 1607 | "ASoC: failed to register runtime device: %d\n", ret); |
589c3563 JN |
1608 | return ret; |
1609 | } | |
1610 | rtd->dev_registered = 1; | |
589c3563 JN |
1611 | return 0; |
1612 | } | |
1613 | ||
1a497983 ML |
1614 | static int soc_probe_link_components(struct snd_soc_card *card, |
1615 | struct snd_soc_pcm_runtime *rtd, | |
62ae68fa SW |
1616 | int order) |
1617 | { | |
f1d45cc3 | 1618 | struct snd_soc_component *component; |
90be711e KM |
1619 | struct snd_soc_rtdcom_list *rtdcom; |
1620 | int ret; | |
62ae68fa | 1621 | |
90be711e KM |
1622 | for_each_rtdcom(rtd, rtdcom) { |
1623 | component = rtdcom->component; | |
62ae68fa | 1624 | |
70090bbb | 1625 | if (component->driver->probe_order == order) { |
f1d45cc3 | 1626 | ret = soc_probe_component(card, component); |
88bd870f BC |
1627 | if (ret < 0) |
1628 | return ret; | |
1629 | } | |
62ae68fa SW |
1630 | } |
1631 | ||
62ae68fa SW |
1632 | return 0; |
1633 | } | |
1634 | ||
8e2be562 | 1635 | static int soc_probe_dai(struct snd_soc_dai *dai, int order) |
b0aa88af MLC |
1636 | { |
1637 | int ret; | |
1638 | ||
8e2be562 LPC |
1639 | if (!dai->probed && dai->driver->probe_order == order) { |
1640 | if (dai->driver->probe) { | |
1641 | ret = dai->driver->probe(dai); | |
b0aa88af | 1642 | if (ret < 0) { |
8e2be562 LPC |
1643 | dev_err(dai->dev, |
1644 | "ASoC: failed to probe DAI %s: %d\n", | |
1645 | dai->name, ret); | |
b0aa88af MLC |
1646 | return ret; |
1647 | } | |
1648 | } | |
1649 | ||
8e2be562 | 1650 | dai->probed = 1; |
b0aa88af MLC |
1651 | } |
1652 | ||
1653 | return 0; | |
1654 | } | |
1655 | ||
25f7b701 AP |
1656 | static int soc_link_dai_pcm_new(struct snd_soc_dai **dais, int num_dais, |
1657 | struct snd_soc_pcm_runtime *rtd) | |
1658 | { | |
1659 | int i, ret = 0; | |
1660 | ||
1661 | for (i = 0; i < num_dais; ++i) { | |
1662 | struct snd_soc_dai_driver *drv = dais[i]->driver; | |
1663 | ||
1664 | if (!rtd->dai_link->no_pcm && drv->pcm_new) | |
1665 | ret = drv->pcm_new(rtd, dais[i]); | |
1666 | if (ret < 0) { | |
1667 | dev_err(dais[i]->dev, | |
1668 | "ASoC: Failed to bind %s with pcm device\n", | |
1669 | dais[i]->name); | |
1670 | return ret; | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | return 0; | |
1675 | } | |
1676 | ||
2436a723 MLC |
1677 | static int soc_link_dai_widgets(struct snd_soc_card *card, |
1678 | struct snd_soc_dai_link *dai_link, | |
3f901a02 | 1679 | struct snd_soc_pcm_runtime *rtd) |
2436a723 | 1680 | { |
3f901a02 BC |
1681 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
1682 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
3de7c420 | 1683 | struct snd_soc_dapm_widget *sink, *source; |
2436a723 MLC |
1684 | int ret; |
1685 | ||
88bd870f BC |
1686 | if (rtd->num_codecs > 1) |
1687 | dev_warn(card->dev, "ASoC: Multiple codecs not supported yet\n"); | |
1688 | ||
2436a723 | 1689 | /* link the DAI widgets */ |
3de7c420 VK |
1690 | sink = codec_dai->playback_widget; |
1691 | source = cpu_dai->capture_widget; | |
1692 | if (sink && source) { | |
2436a723 | 1693 | ret = snd_soc_dapm_new_pcm(card, dai_link->params, |
3de7c420 VK |
1694 | dai_link->num_params, |
1695 | source, sink); | |
2436a723 MLC |
1696 | if (ret != 0) { |
1697 | dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", | |
3de7c420 | 1698 | sink->name, source->name, ret); |
2436a723 MLC |
1699 | return ret; |
1700 | } | |
1701 | } | |
1702 | ||
3de7c420 VK |
1703 | sink = cpu_dai->playback_widget; |
1704 | source = codec_dai->capture_widget; | |
1705 | if (sink && source) { | |
2436a723 | 1706 | ret = snd_soc_dapm_new_pcm(card, dai_link->params, |
3de7c420 VK |
1707 | dai_link->num_params, |
1708 | source, sink); | |
2436a723 MLC |
1709 | if (ret != 0) { |
1710 | dev_err(card->dev, "ASoC: Can't link %s to %s: %d\n", | |
3de7c420 | 1711 | sink->name, source->name, ret); |
2436a723 MLC |
1712 | return ret; |
1713 | } | |
1714 | } | |
1715 | ||
1716 | return 0; | |
1717 | } | |
1718 | ||
1a497983 ML |
1719 | static int soc_probe_link_dais(struct snd_soc_card *card, |
1720 | struct snd_soc_pcm_runtime *rtd, int order) | |
f0fba2ad | 1721 | { |
1a497983 | 1722 | struct snd_soc_dai_link *dai_link = rtd->dai_link; |
c74184ed | 1723 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
88bd870f | 1724 | int i, ret; |
f0fba2ad | 1725 | |
f110bfc7 | 1726 | dev_dbg(card->dev, "ASoC: probe %s dai link %d late %d\n", |
1a497983 | 1727 | card->name, rtd->num, order); |
f0fba2ad | 1728 | |
f0fba2ad LG |
1729 | /* set default power off timeout */ |
1730 | rtd->pmdown_time = pmdown_time; | |
1731 | ||
8e2be562 LPC |
1732 | ret = soc_probe_dai(cpu_dai, order); |
1733 | if (ret) | |
1734 | return ret; | |
db2a4165 | 1735 | |
f0fba2ad | 1736 | /* probe the CODEC DAI */ |
88bd870f | 1737 | for (i = 0; i < rtd->num_codecs; i++) { |
8e2be562 | 1738 | ret = soc_probe_dai(rtd->codec_dais[i], order); |
88bd870f BC |
1739 | if (ret) |
1740 | return ret; | |
1741 | } | |
fe3e78e0 | 1742 | |
0168bf0d LG |
1743 | /* complete DAI probe during last probe */ |
1744 | if (order != SND_SOC_COMP_ORDER_LAST) | |
1745 | return 0; | |
1746 | ||
5f3484ac LPC |
1747 | /* do machine specific initialization */ |
1748 | if (dai_link->init) { | |
1749 | ret = dai_link->init(rtd); | |
1750 | if (ret < 0) { | |
1751 | dev_err(card->dev, "ASoC: failed to init %s: %d\n", | |
1752 | dai_link->name, ret); | |
1753 | return ret; | |
1754 | } | |
1755 | } | |
1756 | ||
a5053a8e KM |
1757 | if (dai_link->dai_fmt) |
1758 | snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); | |
1759 | ||
5f3484ac | 1760 | ret = soc_post_component_init(rtd, dai_link->name); |
589c3563 | 1761 | if (ret) |
f0fba2ad | 1762 | return ret; |
fe3e78e0 | 1763 | |
5f3484ac LPC |
1764 | #ifdef CONFIG_DEBUG_FS |
1765 | /* add DPCM sysfs entries */ | |
2e55b90a LPC |
1766 | if (dai_link->dynamic) |
1767 | soc_dpcm_debugfs_add(rtd); | |
5f3484ac LPC |
1768 | #endif |
1769 | ||
6f0c4226 | 1770 | if (cpu_dai->driver->compress_new) { |
1245b700 | 1771 | /*create compress_device"*/ |
1a497983 | 1772 | ret = cpu_dai->driver->compress_new(rtd, rtd->num); |
c74184ed | 1773 | if (ret < 0) { |
f110bfc7 | 1774 | dev_err(card->dev, "ASoC: can't create compress %s\n", |
1245b700 | 1775 | dai_link->stream_name); |
c74184ed MB |
1776 | return ret; |
1777 | } | |
1778 | } else { | |
1245b700 NK |
1779 | |
1780 | if (!dai_link->params) { | |
1781 | /* create the pcm */ | |
1a497983 | 1782 | ret = soc_new_pcm(rtd, rtd->num); |
1245b700 | 1783 | if (ret < 0) { |
f110bfc7 | 1784 | dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", |
1245b700 | 1785 | dai_link->stream_name, ret); |
c74184ed MB |
1786 | return ret; |
1787 | } | |
25f7b701 AP |
1788 | ret = soc_link_dai_pcm_new(&cpu_dai, 1, rtd); |
1789 | if (ret < 0) | |
1790 | return ret; | |
1791 | ret = soc_link_dai_pcm_new(rtd->codec_dais, | |
1792 | rtd->num_codecs, rtd); | |
1793 | if (ret < 0) | |
1794 | return ret; | |
1245b700 | 1795 | } else { |
9d58a077 RF |
1796 | INIT_DELAYED_WORK(&rtd->delayed_work, |
1797 | codec2codec_close_delayed_work); | |
1798 | ||
1245b700 | 1799 | /* link the DAI widgets */ |
3f901a02 | 1800 | ret = soc_link_dai_widgets(card, dai_link, rtd); |
2436a723 MLC |
1801 | if (ret) |
1802 | return ret; | |
c74184ed | 1803 | } |
f0fba2ad LG |
1804 | } |
1805 | ||
f0fba2ad LG |
1806 | return 0; |
1807 | } | |
1808 | ||
44c69bb1 | 1809 | static int soc_bind_aux_dev(struct snd_soc_card *card, int num) |
3ca041ed SR |
1810 | { |
1811 | struct snd_soc_aux_dev *aux_dev = &card->aux_dev[num]; | |
f2ed6b07 ML |
1812 | struct snd_soc_component *component; |
1813 | const char *name; | |
1814 | struct device_node *codec_of_node; | |
1815 | ||
1816 | if (aux_dev->codec_of_node || aux_dev->codec_name) { | |
1817 | /* codecs, usually analog devices */ | |
1818 | name = aux_dev->codec_name; | |
1819 | codec_of_node = aux_dev->codec_of_node; | |
1820 | component = soc_find_component(codec_of_node, name); | |
1821 | if (!component) { | |
1822 | if (codec_of_node) | |
1823 | name = of_node_full_name(codec_of_node); | |
1824 | goto err_defer; | |
1825 | } | |
1826 | } else if (aux_dev->name) { | |
1827 | /* generic components */ | |
1828 | name = aux_dev->name; | |
1829 | component = soc_find_component(NULL, name); | |
1830 | if (!component) | |
1831 | goto err_defer; | |
1832 | } else { | |
1833 | dev_err(card->dev, "ASoC: Invalid auxiliary device\n"); | |
1834 | return -EINVAL; | |
3ca041ed | 1835 | } |
fb099cb7 | 1836 | |
f2ed6b07 | 1837 | component->init = aux_dev->init; |
d2e3a135 | 1838 | list_add(&component->card_aux_list, &card->aux_comp_list); |
1a653aa4 | 1839 | |
44c69bb1 | 1840 | return 0; |
f2ed6b07 ML |
1841 | |
1842 | err_defer: | |
1843 | dev_err(card->dev, "ASoC: %s not registered\n", name); | |
1844 | return -EPROBE_DEFER; | |
b19e6e7b MB |
1845 | } |
1846 | ||
f2ed6b07 | 1847 | static int soc_probe_aux_devices(struct snd_soc_card *card) |
2eea392d | 1848 | { |
991454e1 | 1849 | struct snd_soc_component *comp; |
f2ed6b07 | 1850 | int order; |
44c69bb1 | 1851 | int ret; |
3ca041ed | 1852 | |
f2ed6b07 ML |
1853 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; |
1854 | order++) { | |
991454e1 | 1855 | list_for_each_entry(comp, &card->aux_comp_list, card_aux_list) { |
f2ed6b07 ML |
1856 | if (comp->driver->probe_order == order) { |
1857 | ret = soc_probe_component(card, comp); | |
1858 | if (ret < 0) { | |
1859 | dev_err(card->dev, | |
1860 | "ASoC: failed to probe aux component %s %d\n", | |
1861 | comp->name, ret); | |
1862 | return ret; | |
1863 | } | |
1864 | } | |
5f3484ac LPC |
1865 | } |
1866 | } | |
2eea392d | 1867 | |
f2ed6b07 | 1868 | return 0; |
2eea392d JN |
1869 | } |
1870 | ||
f2ed6b07 | 1871 | static void soc_remove_aux_devices(struct snd_soc_card *card) |
2eea392d | 1872 | { |
f2ed6b07 ML |
1873 | struct snd_soc_component *comp, *_comp; |
1874 | int order; | |
2eea392d | 1875 | |
f2ed6b07 ML |
1876 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; |
1877 | order++) { | |
1878 | list_for_each_entry_safe(comp, _comp, | |
991454e1 | 1879 | &card->aux_comp_list, card_aux_list) { |
1a653aa4 | 1880 | |
f2ed6b07 ML |
1881 | if (comp->driver->remove_order == order) { |
1882 | soc_remove_component(comp); | |
991454e1 KM |
1883 | /* remove it from the card's aux_comp_list */ |
1884 | list_del(&comp->card_aux_list); | |
f2ed6b07 ML |
1885 | } |
1886 | } | |
2eea392d | 1887 | } |
2eea392d JN |
1888 | } |
1889 | ||
f90fb3f7 | 1890 | static int snd_soc_init_codec_cache(struct snd_soc_codec *codec) |
fdf0f54d DP |
1891 | { |
1892 | int ret; | |
1893 | ||
1894 | if (codec->cache_init) | |
1895 | return 0; | |
1896 | ||
fdf0f54d DP |
1897 | ret = snd_soc_cache_init(codec); |
1898 | if (ret < 0) { | |
10e8aa9a MM |
1899 | dev_err(codec->dev, |
1900 | "ASoC: Failed to set cache compression type: %d\n", | |
1901 | ret); | |
fdf0f54d DP |
1902 | return ret; |
1903 | } | |
1904 | codec->cache_init = 1; | |
1905 | return 0; | |
1906 | } | |
1907 | ||
ce64c8b9 LPC |
1908 | /** |
1909 | * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime | |
1910 | * @rtd: The runtime for which the DAI link format should be changed | |
1911 | * @dai_fmt: The new DAI link format | |
1912 | * | |
1913 | * This function updates the DAI link format for all DAIs connected to the DAI | |
1914 | * link for the specified runtime. | |
1915 | * | |
1916 | * Note: For setups with a static format set the dai_fmt field in the | |
1917 | * corresponding snd_dai_link struct instead of using this function. | |
1918 | * | |
1919 | * Returns 0 on success, otherwise a negative error code. | |
1920 | */ | |
1921 | int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, | |
1922 | unsigned int dai_fmt) | |
1923 | { | |
1924 | struct snd_soc_dai **codec_dais = rtd->codec_dais; | |
1925 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; | |
1926 | unsigned int i; | |
1927 | int ret; | |
1928 | ||
1929 | for (i = 0; i < rtd->num_codecs; i++) { | |
1930 | struct snd_soc_dai *codec_dai = codec_dais[i]; | |
1931 | ||
1932 | ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); | |
1933 | if (ret != 0 && ret != -ENOTSUPP) { | |
1934 | dev_warn(codec_dai->dev, | |
1935 | "ASoC: Failed to set DAI format: %d\n", ret); | |
1936 | return ret; | |
1937 | } | |
1938 | } | |
1939 | ||
1940 | /* Flip the polarity for the "CPU" end of a CODEC<->CODEC link */ | |
1941 | if (cpu_dai->codec) { | |
1942 | unsigned int inv_dai_fmt; | |
1943 | ||
1944 | inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; | |
1945 | switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
1946 | case SND_SOC_DAIFMT_CBM_CFM: | |
1947 | inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; | |
1948 | break; | |
1949 | case SND_SOC_DAIFMT_CBM_CFS: | |
1950 | inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; | |
1951 | break; | |
1952 | case SND_SOC_DAIFMT_CBS_CFM: | |
1953 | inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; | |
1954 | break; | |
1955 | case SND_SOC_DAIFMT_CBS_CFS: | |
1956 | inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; | |
1957 | break; | |
1958 | } | |
1959 | ||
1960 | dai_fmt = inv_dai_fmt; | |
1961 | } | |
1962 | ||
1963 | ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt); | |
1964 | if (ret != 0 && ret != -ENOTSUPP) { | |
1965 | dev_warn(cpu_dai->dev, | |
1966 | "ASoC: Failed to set DAI format: %d\n", ret); | |
1967 | return ret; | |
1968 | } | |
1969 | ||
1970 | return 0; | |
1971 | } | |
ddaca25a | 1972 | EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); |
ce64c8b9 | 1973 | |
345233d7 | 1974 | |
1f5a4535 | 1975 | #ifdef CONFIG_DMI |
345233d7 LG |
1976 | /* Trim special characters, and replace '-' with '_' since '-' is used to |
1977 | * separate different DMI fields in the card long name. Only number and | |
1978 | * alphabet characters and a few separator characters are kept. | |
1979 | */ | |
1980 | static void cleanup_dmi_name(char *name) | |
1981 | { | |
1982 | int i, j = 0; | |
1983 | ||
1984 | for (i = 0; name[i]; i++) { | |
1985 | if (isalnum(name[i]) || (name[i] == '.') | |
1986 | || (name[i] == '_')) | |
1987 | name[j++] = name[i]; | |
1988 | else if (name[i] == '-') | |
1989 | name[j++] = '_'; | |
1990 | } | |
1991 | ||
1992 | name[j] = '\0'; | |
1993 | } | |
1994 | ||
98faf436 ML |
1995 | /* Check if a DMI field is valid, i.e. not containing any string |
1996 | * in the black list. | |
1997 | */ | |
1998 | static int is_dmi_valid(const char *field) | |
1999 | { | |
2000 | int i = 0; | |
2001 | ||
2002 | while (dmi_blacklist[i]) { | |
2003 | if (strstr(field, dmi_blacklist[i])) | |
2004 | return 0; | |
2005 | i++; | |
46b5a4d2 | 2006 | } |
98faf436 ML |
2007 | |
2008 | return 1; | |
2009 | } | |
2010 | ||
345233d7 LG |
2011 | /** |
2012 | * snd_soc_set_dmi_name() - Register DMI names to card | |
2013 | * @card: The card to register DMI names | |
2014 | * @flavour: The flavour "differentiator" for the card amongst its peers. | |
2015 | * | |
2016 | * An Intel machine driver may be used by many different devices but are | |
2017 | * difficult for userspace to differentiate, since machine drivers ususally | |
2018 | * use their own name as the card short name and leave the card long name | |
2019 | * blank. To differentiate such devices and fix bugs due to lack of | |
2020 | * device-specific configurations, this function allows DMI info to be used | |
2021 | * as the sound card long name, in the format of | |
2022 | * "vendor-product-version-board" | |
2023 | * (Character '-' is used to separate different DMI fields here). | |
2024 | * This will help the user space to load the device-specific Use Case Manager | |
2025 | * (UCM) configurations for the card. | |
2026 | * | |
2027 | * Possible card long names may be: | |
2028 | * DellInc.-XPS139343-01-0310JH | |
2029 | * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA | |
2030 | * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX | |
2031 | * | |
2032 | * This function also supports flavoring the card longname to provide | |
2033 | * the extra differentiation, like "vendor-product-version-board-flavor". | |
2034 | * | |
2035 | * We only keep number and alphabet characters and a few separator characters | |
2036 | * in the card long name since UCM in the user space uses the card long names | |
2037 | * as card configuration directory names and AudoConf cannot support special | |
2038 | * charactors like SPACE. | |
2039 | * | |
2040 | * Returns 0 on success, otherwise a negative error code. | |
2041 | */ | |
2042 | int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) | |
2043 | { | |
2044 | const char *vendor, *product, *product_version, *board; | |
2045 | size_t longname_buf_size = sizeof(card->snd_card->longname); | |
2046 | size_t len; | |
2047 | ||
2048 | if (card->long_name) | |
2049 | return 0; /* long name already set by driver or from DMI */ | |
2050 | ||
2051 | /* make up dmi long name as: vendor.product.version.board */ | |
2052 | vendor = dmi_get_system_info(DMI_BOARD_VENDOR); | |
98faf436 | 2053 | if (!vendor || !is_dmi_valid(vendor)) { |
345233d7 LG |
2054 | dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); |
2055 | return 0; | |
2056 | } | |
2057 | ||
98faf436 | 2058 | |
345233d7 LG |
2059 | snprintf(card->dmi_longname, sizeof(card->snd_card->longname), |
2060 | "%s", vendor); | |
2061 | cleanup_dmi_name(card->dmi_longname); | |
2062 | ||
2063 | product = dmi_get_system_info(DMI_PRODUCT_NAME); | |
98faf436 | 2064 | if (product && is_dmi_valid(product)) { |
345233d7 LG |
2065 | len = strlen(card->dmi_longname); |
2066 | snprintf(card->dmi_longname + len, | |
2067 | longname_buf_size - len, | |
2068 | "-%s", product); | |
2069 | ||
2070 | len++; /* skip the separator "-" */ | |
2071 | if (len < longname_buf_size) | |
2072 | cleanup_dmi_name(card->dmi_longname + len); | |
2073 | ||
2074 | /* some vendors like Lenovo may only put a self-explanatory | |
2075 | * name in the product version field | |
2076 | */ | |
2077 | product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); | |
98faf436 | 2078 | if (product_version && is_dmi_valid(product_version)) { |
345233d7 LG |
2079 | len = strlen(card->dmi_longname); |
2080 | snprintf(card->dmi_longname + len, | |
2081 | longname_buf_size - len, | |
2082 | "-%s", product_version); | |
2083 | ||
2084 | len++; | |
2085 | if (len < longname_buf_size) | |
2086 | cleanup_dmi_name(card->dmi_longname + len); | |
2087 | } | |
2088 | } | |
2089 | ||
2090 | board = dmi_get_system_info(DMI_BOARD_NAME); | |
98faf436 | 2091 | if (board && is_dmi_valid(board)) { |
345233d7 LG |
2092 | len = strlen(card->dmi_longname); |
2093 | snprintf(card->dmi_longname + len, | |
2094 | longname_buf_size - len, | |
2095 | "-%s", board); | |
2096 | ||
2097 | len++; | |
2098 | if (len < longname_buf_size) | |
2099 | cleanup_dmi_name(card->dmi_longname + len); | |
2100 | } else if (!product) { | |
2101 | /* fall back to using legacy name */ | |
2102 | dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); | |
2103 | return 0; | |
2104 | } | |
2105 | ||
2106 | /* Add flavour to dmi long name */ | |
2107 | if (flavour) { | |
2108 | len = strlen(card->dmi_longname); | |
2109 | snprintf(card->dmi_longname + len, | |
2110 | longname_buf_size - len, | |
2111 | "-%s", flavour); | |
2112 | ||
2113 | len++; | |
2114 | if (len < longname_buf_size) | |
2115 | cleanup_dmi_name(card->dmi_longname + len); | |
2116 | } | |
2117 | ||
2118 | /* set the card long name */ | |
2119 | card->long_name = card->dmi_longname; | |
2120 | ||
2121 | return 0; | |
2122 | } | |
2123 | EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); | |
1f5a4535 | 2124 | #endif /* CONFIG_DMI */ |
345233d7 | 2125 | |
b19e6e7b | 2126 | static int snd_soc_instantiate_card(struct snd_soc_card *card) |
f0fba2ad | 2127 | { |
fdf0f54d | 2128 | struct snd_soc_codec *codec; |
1a497983 | 2129 | struct snd_soc_pcm_runtime *rtd; |
61b0088b | 2130 | struct snd_soc_dai_link *dai_link; |
ce64c8b9 | 2131 | int ret, i, order; |
fe3e78e0 | 2132 | |
34e81ab4 | 2133 | mutex_lock(&client_mutex); |
01b9d99a | 2134 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); |
dbe21408 | 2135 | |
f0fba2ad | 2136 | /* bind DAIs */ |
b19e6e7b | 2137 | for (i = 0; i < card->num_links; i++) { |
6f2f1ff0 | 2138 | ret = soc_bind_dai_link(card, &card->dai_link[i]); |
b19e6e7b MB |
2139 | if (ret != 0) |
2140 | goto base_error; | |
2141 | } | |
fe3e78e0 | 2142 | |
44c69bb1 | 2143 | /* bind aux_devs too */ |
b19e6e7b | 2144 | for (i = 0; i < card->num_aux_devs; i++) { |
44c69bb1 | 2145 | ret = soc_bind_aux_dev(card, i); |
b19e6e7b MB |
2146 | if (ret != 0) |
2147 | goto base_error; | |
f0fba2ad | 2148 | } |
435c5e25 | 2149 | |
f8f80361 ML |
2150 | /* add predefined DAI links to the list */ |
2151 | for (i = 0; i < card->num_links; i++) | |
2152 | snd_soc_add_dai_link(card, card->dai_link+i); | |
2153 | ||
fdf0f54d DP |
2154 | /* initialize the register cache for each available codec */ |
2155 | list_for_each_entry(codec, &codec_list, list) { | |
2156 | if (codec->cache_init) | |
2157 | continue; | |
f90fb3f7 | 2158 | ret = snd_soc_init_codec_cache(codec); |
b19e6e7b MB |
2159 | if (ret < 0) |
2160 | goto base_error; | |
fdf0f54d DP |
2161 | } |
2162 | ||
f0fba2ad | 2163 | /* card bind complete so register a sound card */ |
102b5a8d | 2164 | ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
f0fba2ad LG |
2165 | card->owner, 0, &card->snd_card); |
2166 | if (ret < 0) { | |
10e8aa9a MM |
2167 | dev_err(card->dev, |
2168 | "ASoC: can't create sound card for card %s: %d\n", | |
2169 | card->name, ret); | |
b19e6e7b | 2170 | goto base_error; |
f0fba2ad | 2171 | } |
f0fba2ad | 2172 | |
0757d834 LPC |
2173 | soc_init_card_debugfs(card); |
2174 | ||
e37a4970 MB |
2175 | card->dapm.bias_level = SND_SOC_BIAS_OFF; |
2176 | card->dapm.dev = card->dev; | |
2177 | card->dapm.card = card; | |
2178 | list_add(&card->dapm.list, &card->dapm_list); | |
2179 | ||
d5d1e0be LPC |
2180 | #ifdef CONFIG_DEBUG_FS |
2181 | snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); | |
2182 | #endif | |
2183 | ||
88ee1c61 | 2184 | #ifdef CONFIG_PM_SLEEP |
f0fba2ad LG |
2185 | /* deferred resume work */ |
2186 | INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); | |
2187 | #endif | |
db2a4165 | 2188 | |
9a841ebb MB |
2189 | if (card->dapm_widgets) |
2190 | snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, | |
2191 | card->num_dapm_widgets); | |
2192 | ||
f23e860e NC |
2193 | if (card->of_dapm_widgets) |
2194 | snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, | |
2195 | card->num_of_dapm_widgets); | |
2196 | ||
f0fba2ad LG |
2197 | /* initialise the sound card only once */ |
2198 | if (card->probe) { | |
e7361ec4 | 2199 | ret = card->probe(card); |
f0fba2ad LG |
2200 | if (ret < 0) |
2201 | goto card_probe_error; | |
2202 | } | |
fe3e78e0 | 2203 | |
62ae68fa | 2204 | /* probe all components used by DAI links on this card */ |
0168bf0d LG |
2205 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; |
2206 | order++) { | |
1a497983 ML |
2207 | list_for_each_entry(rtd, &card->rtd_list, list) { |
2208 | ret = soc_probe_link_components(card, rtd, order); | |
0168bf0d | 2209 | if (ret < 0) { |
f110bfc7 LG |
2210 | dev_err(card->dev, |
2211 | "ASoC: failed to instantiate card %d\n", | |
2212 | ret); | |
62ae68fa SW |
2213 | goto probe_dai_err; |
2214 | } | |
2215 | } | |
2216 | } | |
2217 | ||
f2ed6b07 ML |
2218 | /* probe auxiliary components */ |
2219 | ret = soc_probe_aux_devices(card); | |
2220 | if (ret < 0) | |
2221 | goto probe_dai_err; | |
2222 | ||
61b0088b ML |
2223 | /* Find new DAI links added during probing components and bind them. |
2224 | * Components with topology may bring new DAIs and DAI links. | |
2225 | */ | |
2226 | list_for_each_entry(dai_link, &card->dai_link_list, list) { | |
2227 | if (soc_is_dai_link_bound(card, dai_link)) | |
2228 | continue; | |
2229 | ||
2230 | ret = soc_init_dai_link(card, dai_link); | |
2231 | if (ret) | |
2232 | goto probe_dai_err; | |
2233 | ret = soc_bind_dai_link(card, dai_link); | |
2234 | if (ret) | |
2235 | goto probe_dai_err; | |
2236 | } | |
2237 | ||
62ae68fa SW |
2238 | /* probe all DAI links on this card */ |
2239 | for (order = SND_SOC_COMP_ORDER_FIRST; order <= SND_SOC_COMP_ORDER_LAST; | |
2240 | order++) { | |
1a497983 ML |
2241 | list_for_each_entry(rtd, &card->rtd_list, list) { |
2242 | ret = soc_probe_link_dais(card, rtd, order); | |
62ae68fa | 2243 | if (ret < 0) { |
f110bfc7 LG |
2244 | dev_err(card->dev, |
2245 | "ASoC: failed to instantiate card %d\n", | |
2246 | ret); | |
0168bf0d LG |
2247 | goto probe_dai_err; |
2248 | } | |
f0fba2ad LG |
2249 | } |
2250 | } | |
db2a4165 | 2251 | |
888df395 | 2252 | snd_soc_dapm_link_dai_widgets(card); |
b893ea5f | 2253 | snd_soc_dapm_connect_dai_link_widgets(card); |
888df395 | 2254 | |
b7af1daf | 2255 | if (card->controls) |
022658be | 2256 | snd_soc_add_card_controls(card, card->controls, card->num_controls); |
b7af1daf | 2257 | |
b8ad29de MB |
2258 | if (card->dapm_routes) |
2259 | snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, | |
2260 | card->num_dapm_routes); | |
2261 | ||
f23e860e NC |
2262 | if (card->of_dapm_routes) |
2263 | snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, | |
2264 | card->num_of_dapm_routes); | |
75d9ac46 | 2265 | |
861886d3 TI |
2266 | /* try to set some sane longname if DMI is available */ |
2267 | snd_soc_set_dmi_name(card, NULL); | |
2268 | ||
f0fba2ad | 2269 | snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname), |
f0fba2ad | 2270 | "%s", card->name); |
22de71ba LG |
2271 | snprintf(card->snd_card->longname, sizeof(card->snd_card->longname), |
2272 | "%s", card->long_name ? card->long_name : card->name); | |
f0e8ed85 MB |
2273 | snprintf(card->snd_card->driver, sizeof(card->snd_card->driver), |
2274 | "%s", card->driver_name ? card->driver_name : card->name); | |
2275 | for (i = 0; i < ARRAY_SIZE(card->snd_card->driver); i++) { | |
2276 | switch (card->snd_card->driver[i]) { | |
2277 | case '_': | |
2278 | case '-': | |
2279 | case '\0': | |
2280 | break; | |
2281 | default: | |
2282 | if (!isalnum(card->snd_card->driver[i])) | |
2283 | card->snd_card->driver[i] = '_'; | |
2284 | break; | |
2285 | } | |
2286 | } | |
f0fba2ad | 2287 | |
28e9ad92 MB |
2288 | if (card->late_probe) { |
2289 | ret = card->late_probe(card); | |
2290 | if (ret < 0) { | |
f110bfc7 | 2291 | dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", |
28e9ad92 MB |
2292 | card->name, ret); |
2293 | goto probe_aux_dev_err; | |
2294 | } | |
2295 | } | |
2296 | ||
824ef826 | 2297 | snd_soc_dapm_new_widgets(card); |
8c193b8d | 2298 | |
f0fba2ad LG |
2299 | ret = snd_card_register(card->snd_card); |
2300 | if (ret < 0) { | |
f110bfc7 LG |
2301 | dev_err(card->dev, "ASoC: failed to register soundcard %d\n", |
2302 | ret); | |
6b3ed785 | 2303 | goto probe_aux_dev_err; |
db2a4165 FM |
2304 | } |
2305 | ||
f0fba2ad | 2306 | card->instantiated = 1; |
4f4c0072 | 2307 | snd_soc_dapm_sync(&card->dapm); |
f0fba2ad | 2308 | mutex_unlock(&card->mutex); |
34e81ab4 | 2309 | mutex_unlock(&client_mutex); |
b19e6e7b MB |
2310 | |
2311 | return 0; | |
f0fba2ad | 2312 | |
2eea392d | 2313 | probe_aux_dev_err: |
f2ed6b07 | 2314 | soc_remove_aux_devices(card); |
2eea392d | 2315 | |
f0fba2ad | 2316 | probe_dai_err: |
0671fd8e | 2317 | soc_remove_dai_links(card); |
f0fba2ad LG |
2318 | |
2319 | card_probe_error: | |
87506549 | 2320 | if (card->remove) |
e7361ec4 | 2321 | card->remove(card); |
f0fba2ad | 2322 | |
2210438b | 2323 | snd_soc_dapm_free(&card->dapm); |
0757d834 | 2324 | soc_cleanup_card_debugfs(card); |
f0fba2ad LG |
2325 | snd_card_free(card->snd_card); |
2326 | ||
b19e6e7b | 2327 | base_error: |
1a497983 | 2328 | soc_remove_pcm_runtimes(card); |
f0fba2ad | 2329 | mutex_unlock(&card->mutex); |
34e81ab4 | 2330 | mutex_unlock(&client_mutex); |
db2a4165 | 2331 | |
b19e6e7b | 2332 | return ret; |
435c5e25 MB |
2333 | } |
2334 | ||
2335 | /* probes a new socdev */ | |
2336 | static int soc_probe(struct platform_device *pdev) | |
2337 | { | |
f0fba2ad | 2338 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
435c5e25 | 2339 | |
70a7ca34 VK |
2340 | /* |
2341 | * no card, so machine driver should be registering card | |
2342 | * we should not be here in that case so ret error | |
2343 | */ | |
2344 | if (!card) | |
2345 | return -EINVAL; | |
2346 | ||
fe4085e8 | 2347 | dev_warn(&pdev->dev, |
f110bfc7 | 2348 | "ASoC: machine %s should use snd_soc_register_card()\n", |
fe4085e8 MB |
2349 | card->name); |
2350 | ||
435c5e25 MB |
2351 | /* Bodge while we unpick instantiation */ |
2352 | card->dev = &pdev->dev; | |
f0fba2ad | 2353 | |
28d528c8 | 2354 | return snd_soc_register_card(card); |
db2a4165 FM |
2355 | } |
2356 | ||
b0e26485 | 2357 | static int soc_cleanup_card_resources(struct snd_soc_card *card) |
db2a4165 | 2358 | { |
1a497983 | 2359 | struct snd_soc_pcm_runtime *rtd; |
db2a4165 | 2360 | |
b0e26485 | 2361 | /* make sure any delayed work runs */ |
1a497983 | 2362 | list_for_each_entry(rtd, &card->rtd_list, list) |
43829731 | 2363 | flush_delayed_work(&rtd->delayed_work); |
914dc182 | 2364 | |
4efda5f2 TI |
2365 | /* free the ALSA card at first; this syncs with pending operations */ |
2366 | snd_card_free(card->snd_card); | |
2367 | ||
b0e26485 | 2368 | /* remove and free each DAI */ |
0671fd8e | 2369 | soc_remove_dai_links(card); |
1a497983 | 2370 | soc_remove_pcm_runtimes(card); |
2eea392d | 2371 | |
f2ed6b07 ML |
2372 | /* remove auxiliary devices */ |
2373 | soc_remove_aux_devices(card); | |
2374 | ||
d1e81428 | 2375 | snd_soc_dapm_free(&card->dapm); |
b0e26485 | 2376 | soc_cleanup_card_debugfs(card); |
f0fba2ad | 2377 | |
b0e26485 VK |
2378 | /* remove the card */ |
2379 | if (card->remove) | |
e7361ec4 | 2380 | card->remove(card); |
a6052154 | 2381 | |
b0e26485 | 2382 | return 0; |
b0e26485 VK |
2383 | } |
2384 | ||
2385 | /* removes a socdev */ | |
2386 | static int soc_remove(struct platform_device *pdev) | |
2387 | { | |
2388 | struct snd_soc_card *card = platform_get_drvdata(pdev); | |
db2a4165 | 2389 | |
c5af3a2e | 2390 | snd_soc_unregister_card(card); |
db2a4165 FM |
2391 | return 0; |
2392 | } | |
2393 | ||
6f8ab4ac | 2394 | int snd_soc_poweroff(struct device *dev) |
51737470 | 2395 | { |
6f8ab4ac | 2396 | struct snd_soc_card *card = dev_get_drvdata(dev); |
1a497983 | 2397 | struct snd_soc_pcm_runtime *rtd; |
51737470 MB |
2398 | |
2399 | if (!card->instantiated) | |
416356fc | 2400 | return 0; |
51737470 MB |
2401 | |
2402 | /* Flush out pmdown_time work - we actually do want to run it | |
2403 | * now, we're shutting down so no imminent restart. */ | |
1a497983 | 2404 | list_for_each_entry(rtd, &card->rtd_list, list) |
43829731 | 2405 | flush_delayed_work(&rtd->delayed_work); |
51737470 | 2406 | |
f0fba2ad | 2407 | snd_soc_dapm_shutdown(card); |
416356fc | 2408 | |
988e8cc4 | 2409 | /* deactivate pins to sleep state */ |
1a497983 | 2410 | list_for_each_entry(rtd, &card->rtd_list, list) { |
88bd870f | 2411 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
1a497983 | 2412 | int i; |
88bd870f | 2413 | |
988e8cc4 | 2414 | pinctrl_pm_select_sleep_state(cpu_dai->dev); |
1a497983 ML |
2415 | for (i = 0; i < rtd->num_codecs; i++) { |
2416 | struct snd_soc_dai *codec_dai = rtd->codec_dais[i]; | |
88bd870f BC |
2417 | pinctrl_pm_select_sleep_state(codec_dai->dev); |
2418 | } | |
988e8cc4 NC |
2419 | } |
2420 | ||
416356fc | 2421 | return 0; |
51737470 | 2422 | } |
6f8ab4ac | 2423 | EXPORT_SYMBOL_GPL(snd_soc_poweroff); |
51737470 | 2424 | |
6f8ab4ac | 2425 | const struct dev_pm_ops snd_soc_pm_ops = { |
b1dd5897 VK |
2426 | .suspend = snd_soc_suspend, |
2427 | .resume = snd_soc_resume, | |
2428 | .freeze = snd_soc_suspend, | |
2429 | .thaw = snd_soc_resume, | |
6f8ab4ac | 2430 | .poweroff = snd_soc_poweroff, |
b1dd5897 | 2431 | .restore = snd_soc_resume, |
416356fc | 2432 | }; |
deb2607e | 2433 | EXPORT_SYMBOL_GPL(snd_soc_pm_ops); |
416356fc | 2434 | |
db2a4165 FM |
2435 | /* ASoC platform driver */ |
2436 | static struct platform_driver soc_driver = { | |
2437 | .driver = { | |
2438 | .name = "soc-audio", | |
6f8ab4ac | 2439 | .pm = &snd_soc_pm_ops, |
db2a4165 FM |
2440 | }, |
2441 | .probe = soc_probe, | |
2442 | .remove = soc_remove, | |
db2a4165 FM |
2443 | }; |
2444 | ||
db2a4165 FM |
2445 | /** |
2446 | * snd_soc_cnew - create new control | |
2447 | * @_template: control template | |
2448 | * @data: control private data | |
ac11a2b3 | 2449 | * @long_name: control long name |
efb7ac3f | 2450 | * @prefix: control name prefix |
db2a4165 FM |
2451 | * |
2452 | * Create a new mixer control from a template control. | |
2453 | * | |
2454 | * Returns 0 for success, else error. | |
2455 | */ | |
2456 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, | |
3056557f | 2457 | void *data, const char *long_name, |
efb7ac3f | 2458 | const char *prefix) |
db2a4165 FM |
2459 | { |
2460 | struct snd_kcontrol_new template; | |
efb7ac3f MB |
2461 | struct snd_kcontrol *kcontrol; |
2462 | char *name = NULL; | |
db2a4165 FM |
2463 | |
2464 | memcpy(&template, _template, sizeof(template)); | |
db2a4165 FM |
2465 | template.index = 0; |
2466 | ||
efb7ac3f MB |
2467 | if (!long_name) |
2468 | long_name = template.name; | |
2469 | ||
2470 | if (prefix) { | |
2b581074 | 2471 | name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); |
efb7ac3f MB |
2472 | if (!name) |
2473 | return NULL; | |
2474 | ||
efb7ac3f MB |
2475 | template.name = name; |
2476 | } else { | |
2477 | template.name = long_name; | |
2478 | } | |
2479 | ||
2480 | kcontrol = snd_ctl_new1(&template, data); | |
2481 | ||
2482 | kfree(name); | |
2483 | ||
2484 | return kcontrol; | |
db2a4165 FM |
2485 | } |
2486 | EXPORT_SYMBOL_GPL(snd_soc_cnew); | |
2487 | ||
022658be LG |
2488 | static int snd_soc_add_controls(struct snd_card *card, struct device *dev, |
2489 | const struct snd_kcontrol_new *controls, int num_controls, | |
2490 | const char *prefix, void *data) | |
2491 | { | |
2492 | int err, i; | |
2493 | ||
2494 | for (i = 0; i < num_controls; i++) { | |
2495 | const struct snd_kcontrol_new *control = &controls[i]; | |
2496 | err = snd_ctl_add(card, snd_soc_cnew(control, data, | |
2497 | control->name, prefix)); | |
2498 | if (err < 0) { | |
f110bfc7 LG |
2499 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
2500 | control->name, err); | |
022658be LG |
2501 | return err; |
2502 | } | |
2503 | } | |
2504 | ||
2505 | return 0; | |
2506 | } | |
2507 | ||
4fefd698 DP |
2508 | struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, |
2509 | const char *name) | |
2510 | { | |
2511 | struct snd_card *card = soc_card->snd_card; | |
2512 | struct snd_kcontrol *kctl; | |
2513 | ||
2514 | if (unlikely(!name)) | |
2515 | return NULL; | |
2516 | ||
2517 | list_for_each_entry(kctl, &card->controls, list) | |
2518 | if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) | |
2519 | return kctl; | |
2520 | return NULL; | |
2521 | } | |
2522 | EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); | |
2523 | ||
0f2780ad LPC |
2524 | /** |
2525 | * snd_soc_add_component_controls - Add an array of controls to a component. | |
2526 | * | |
2527 | * @component: Component to add controls to | |
2528 | * @controls: Array of controls to add | |
2529 | * @num_controls: Number of elements in the array | |
2530 | * | |
2531 | * Return: 0 for success, else error. | |
2532 | */ | |
2533 | int snd_soc_add_component_controls(struct snd_soc_component *component, | |
2534 | const struct snd_kcontrol_new *controls, unsigned int num_controls) | |
2535 | { | |
2536 | struct snd_card *card = component->card->snd_card; | |
2537 | ||
2538 | return snd_soc_add_controls(card, component->dev, controls, | |
2539 | num_controls, component->name_prefix, component); | |
2540 | } | |
2541 | EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); | |
2542 | ||
3e8e1952 | 2543 | /** |
022658be LG |
2544 | * snd_soc_add_codec_controls - add an array of controls to a codec. |
2545 | * Convenience function to add a list of controls. Many codecs were | |
3e8e1952 IM |
2546 | * duplicating this code. |
2547 | * | |
2548 | * @codec: codec to add controls to | |
2549 | * @controls: array of controls to add | |
2550 | * @num_controls: number of elements in the array | |
2551 | * | |
2552 | * Return 0 for success, else error. | |
2553 | */ | |
022658be | 2554 | int snd_soc_add_codec_controls(struct snd_soc_codec *codec, |
0f2780ad | 2555 | const struct snd_kcontrol_new *controls, unsigned int num_controls) |
3e8e1952 | 2556 | { |
0f2780ad LPC |
2557 | return snd_soc_add_component_controls(&codec->component, controls, |
2558 | num_controls); | |
3e8e1952 | 2559 | } |
022658be | 2560 | EXPORT_SYMBOL_GPL(snd_soc_add_codec_controls); |
3e8e1952 | 2561 | |
a491a5c8 LG |
2562 | /** |
2563 | * snd_soc_add_platform_controls - add an array of controls to a platform. | |
022658be | 2564 | * Convenience function to add a list of controls. |
a491a5c8 LG |
2565 | * |
2566 | * @platform: platform to add controls to | |
2567 | * @controls: array of controls to add | |
2568 | * @num_controls: number of elements in the array | |
2569 | * | |
2570 | * Return 0 for success, else error. | |
2571 | */ | |
2572 | int snd_soc_add_platform_controls(struct snd_soc_platform *platform, | |
0f2780ad | 2573 | const struct snd_kcontrol_new *controls, unsigned int num_controls) |
a491a5c8 | 2574 | { |
0f2780ad LPC |
2575 | return snd_soc_add_component_controls(&platform->component, controls, |
2576 | num_controls); | |
a491a5c8 LG |
2577 | } |
2578 | EXPORT_SYMBOL_GPL(snd_soc_add_platform_controls); | |
2579 | ||
022658be LG |
2580 | /** |
2581 | * snd_soc_add_card_controls - add an array of controls to a SoC card. | |
2582 | * Convenience function to add a list of controls. | |
2583 | * | |
2584 | * @soc_card: SoC card to add controls to | |
2585 | * @controls: array of controls to add | |
2586 | * @num_controls: number of elements in the array | |
2587 | * | |
2588 | * Return 0 for success, else error. | |
2589 | */ | |
2590 | int snd_soc_add_card_controls(struct snd_soc_card *soc_card, | |
2591 | const struct snd_kcontrol_new *controls, int num_controls) | |
2592 | { | |
2593 | struct snd_card *card = soc_card->snd_card; | |
2594 | ||
2595 | return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, | |
2596 | NULL, soc_card); | |
2597 | } | |
2598 | EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); | |
2599 | ||
2600 | /** | |
2601 | * snd_soc_add_dai_controls - add an array of controls to a DAI. | |
2602 | * Convienience function to add a list of controls. | |
2603 | * | |
2604 | * @dai: DAI to add controls to | |
2605 | * @controls: array of controls to add | |
2606 | * @num_controls: number of elements in the array | |
2607 | * | |
2608 | * Return 0 for success, else error. | |
2609 | */ | |
2610 | int snd_soc_add_dai_controls(struct snd_soc_dai *dai, | |
2611 | const struct snd_kcontrol_new *controls, int num_controls) | |
2612 | { | |
313665b9 | 2613 | struct snd_card *card = dai->component->card->snd_card; |
022658be LG |
2614 | |
2615 | return snd_soc_add_controls(card, dai->dev, controls, num_controls, | |
2616 | NULL, dai); | |
2617 | } | |
2618 | EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); | |
2619 | ||
8c6529db LG |
2620 | /** |
2621 | * snd_soc_dai_set_sysclk - configure DAI system or master clock. | |
2622 | * @dai: DAI | |
2623 | * @clk_id: DAI specific clock ID | |
2624 | * @freq: new clock frequency in Hz | |
2625 | * @dir: new clock direction - input/output. | |
2626 | * | |
2627 | * Configures the DAI master (MCLK) or system (SYSCLK) clocking. | |
2628 | */ | |
2629 | int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, | |
2630 | unsigned int freq, int dir) | |
2631 | { | |
f0fba2ad LG |
2632 | if (dai->driver && dai->driver->ops->set_sysclk) |
2633 | return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir); | |
ec4ee52a | 2634 | else if (dai->codec && dai->codec->driver->set_sysclk) |
da1c6ea6 | 2635 | return dai->codec->driver->set_sysclk(dai->codec, clk_id, 0, |
ec4ee52a | 2636 | freq, dir); |
8c6529db | 2637 | else |
1104a9c8 | 2638 | return -ENOTSUPP; |
8c6529db LG |
2639 | } |
2640 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk); | |
2641 | ||
ec4ee52a MB |
2642 | /** |
2643 | * snd_soc_codec_set_sysclk - configure CODEC system or master clock. | |
2644 | * @codec: CODEC | |
2645 | * @clk_id: DAI specific clock ID | |
da1c6ea6 | 2646 | * @source: Source for the clock |
ec4ee52a MB |
2647 | * @freq: new clock frequency in Hz |
2648 | * @dir: new clock direction - input/output. | |
2649 | * | |
2650 | * Configures the CODEC master (MCLK) or system (SYSCLK) clocking. | |
2651 | */ | |
2652 | int snd_soc_codec_set_sysclk(struct snd_soc_codec *codec, int clk_id, | |
da1c6ea6 | 2653 | int source, unsigned int freq, int dir) |
ec4ee52a MB |
2654 | { |
2655 | if (codec->driver->set_sysclk) | |
da1c6ea6 MB |
2656 | return codec->driver->set_sysclk(codec, clk_id, source, |
2657 | freq, dir); | |
ec4ee52a | 2658 | else |
1104a9c8 | 2659 | return -ENOTSUPP; |
ec4ee52a MB |
2660 | } |
2661 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_sysclk); | |
2662 | ||
8c6529db LG |
2663 | /** |
2664 | * snd_soc_dai_set_clkdiv - configure DAI clock dividers. | |
2665 | * @dai: DAI | |
ac11a2b3 | 2666 | * @div_id: DAI specific clock divider ID |
8c6529db LG |
2667 | * @div: new clock divisor. |
2668 | * | |
2669 | * Configures the clock dividers. This is used to derive the best DAI bit and | |
2670 | * frame clocks from the system or master clock. It's best to set the DAI bit | |
2671 | * and frame clocks as low as possible to save system power. | |
2672 | */ | |
2673 | int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai, | |
2674 | int div_id, int div) | |
2675 | { | |
f0fba2ad LG |
2676 | if (dai->driver && dai->driver->ops->set_clkdiv) |
2677 | return dai->driver->ops->set_clkdiv(dai, div_id, div); | |
8c6529db LG |
2678 | else |
2679 | return -EINVAL; | |
2680 | } | |
2681 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv); | |
2682 | ||
2683 | /** | |
2684 | * snd_soc_dai_set_pll - configure DAI PLL. | |
2685 | * @dai: DAI | |
2686 | * @pll_id: DAI specific PLL ID | |
85488037 | 2687 | * @source: DAI specific source for the PLL |
8c6529db LG |
2688 | * @freq_in: PLL input clock frequency in Hz |
2689 | * @freq_out: requested PLL output clock frequency in Hz | |
2690 | * | |
2691 | * Configures and enables PLL to generate output clock based on input clock. | |
2692 | */ | |
85488037 MB |
2693 | int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source, |
2694 | unsigned int freq_in, unsigned int freq_out) | |
8c6529db | 2695 | { |
f0fba2ad LG |
2696 | if (dai->driver && dai->driver->ops->set_pll) |
2697 | return dai->driver->ops->set_pll(dai, pll_id, source, | |
85488037 | 2698 | freq_in, freq_out); |
ec4ee52a MB |
2699 | else if (dai->codec && dai->codec->driver->set_pll) |
2700 | return dai->codec->driver->set_pll(dai->codec, pll_id, source, | |
2701 | freq_in, freq_out); | |
8c6529db LG |
2702 | else |
2703 | return -EINVAL; | |
2704 | } | |
2705 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll); | |
2706 | ||
ec4ee52a MB |
2707 | /* |
2708 | * snd_soc_codec_set_pll - configure codec PLL. | |
2709 | * @codec: CODEC | |
2710 | * @pll_id: DAI specific PLL ID | |
2711 | * @source: DAI specific source for the PLL | |
2712 | * @freq_in: PLL input clock frequency in Hz | |
2713 | * @freq_out: requested PLL output clock frequency in Hz | |
2714 | * | |
2715 | * Configures and enables PLL to generate output clock based on input clock. | |
2716 | */ | |
2717 | int snd_soc_codec_set_pll(struct snd_soc_codec *codec, int pll_id, int source, | |
2718 | unsigned int freq_in, unsigned int freq_out) | |
2719 | { | |
2720 | if (codec->driver->set_pll) | |
2721 | return codec->driver->set_pll(codec, pll_id, source, | |
2722 | freq_in, freq_out); | |
2723 | else | |
2724 | return -EINVAL; | |
2725 | } | |
2726 | EXPORT_SYMBOL_GPL(snd_soc_codec_set_pll); | |
2727 | ||
e54cf76b LG |
2728 | /** |
2729 | * snd_soc_dai_set_bclk_ratio - configure BCLK to sample rate ratio. | |
2730 | * @dai: DAI | |
231b86b1 | 2731 | * @ratio: Ratio of BCLK to Sample rate. |
e54cf76b LG |
2732 | * |
2733 | * Configures the DAI for a preset BCLK to sample rate ratio. | |
2734 | */ | |
2735 | int snd_soc_dai_set_bclk_ratio(struct snd_soc_dai *dai, unsigned int ratio) | |
2736 | { | |
2737 | if (dai->driver && dai->driver->ops->set_bclk_ratio) | |
2738 | return dai->driver->ops->set_bclk_ratio(dai, ratio); | |
2739 | else | |
2740 | return -EINVAL; | |
2741 | } | |
2742 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_bclk_ratio); | |
2743 | ||
8c6529db LG |
2744 | /** |
2745 | * snd_soc_dai_set_fmt - configure DAI hardware audio format. | |
2746 | * @dai: DAI | |
8c6529db LG |
2747 | * @fmt: SND_SOC_DAIFMT_ format value. |
2748 | * | |
2749 | * Configures the DAI hardware format and clocking. | |
2750 | */ | |
2751 | int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
2752 | { | |
5e4ba569 | 2753 | if (dai->driver == NULL) |
8c6529db | 2754 | return -EINVAL; |
5e4ba569 SG |
2755 | if (dai->driver->ops->set_fmt == NULL) |
2756 | return -ENOTSUPP; | |
2757 | return dai->driver->ops->set_fmt(dai, fmt); | |
8c6529db LG |
2758 | } |
2759 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt); | |
2760 | ||
89c67857 | 2761 | /** |
e5c21514 | 2762 | * snd_soc_xlate_tdm_slot - generate tx/rx slot mask. |
89c67857 XL |
2763 | * @slots: Number of slots in use. |
2764 | * @tx_mask: bitmask representing active TX slots. | |
2765 | * @rx_mask: bitmask representing active RX slots. | |
2766 | * | |
2767 | * Generates the TDM tx and rx slot default masks for DAI. | |
2768 | */ | |
e5c21514 | 2769 | static int snd_soc_xlate_tdm_slot_mask(unsigned int slots, |
89c67857 XL |
2770 | unsigned int *tx_mask, |
2771 | unsigned int *rx_mask) | |
2772 | { | |
2773 | if (*tx_mask || *rx_mask) | |
2774 | return 0; | |
2775 | ||
2776 | if (!slots) | |
2777 | return -EINVAL; | |
2778 | ||
2779 | *tx_mask = (1 << slots) - 1; | |
2780 | *rx_mask = (1 << slots) - 1; | |
2781 | ||
2782 | return 0; | |
2783 | } | |
2784 | ||
8c6529db | 2785 | /** |
e46c9366 LPC |
2786 | * snd_soc_dai_set_tdm_slot() - Configures a DAI for TDM operation |
2787 | * @dai: The DAI to configure | |
a5479e38 DR |
2788 | * @tx_mask: bitmask representing active TX slots. |
2789 | * @rx_mask: bitmask representing active RX slots. | |
8c6529db | 2790 | * @slots: Number of slots in use. |
a5479e38 | 2791 | * @slot_width: Width in bits for each slot. |
8c6529db | 2792 | * |
e46c9366 LPC |
2793 | * This function configures the specified DAI for TDM operation. @slot contains |
2794 | * the total number of slots of the TDM stream and @slot_with the width of each | |
2795 | * slot in bit clock cycles. @tx_mask and @rx_mask are bitmasks specifying the | |
2796 | * active slots of the TDM stream for the specified DAI, i.e. which slots the | |
2797 | * DAI should write to or read from. If a bit is set the corresponding slot is | |
2798 | * active, if a bit is cleared the corresponding slot is inactive. Bit 0 maps to | |
2799 | * the first slot, bit 1 to the second slot and so on. The first active slot | |
2800 | * maps to the first channel of the DAI, the second active slot to the second | |
2801 | * channel and so on. | |
2802 | * | |
2803 | * TDM mode can be disabled by passing 0 for @slots. In this case @tx_mask, | |
2804 | * @rx_mask and @slot_width will be ignored. | |
2805 | * | |
2806 | * Returns 0 on success, a negative error code otherwise. | |
8c6529db LG |
2807 | */ |
2808 | int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai, | |
a5479e38 | 2809 | unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width) |
8c6529db | 2810 | { |
e5c21514 XL |
2811 | if (dai->driver && dai->driver->ops->xlate_tdm_slot_mask) |
2812 | dai->driver->ops->xlate_tdm_slot_mask(slots, | |
89c67857 XL |
2813 | &tx_mask, &rx_mask); |
2814 | else | |
e5c21514 | 2815 | snd_soc_xlate_tdm_slot_mask(slots, &tx_mask, &rx_mask); |
89c67857 | 2816 | |
88bd870f BC |
2817 | dai->tx_mask = tx_mask; |
2818 | dai->rx_mask = rx_mask; | |
2819 | ||
f0fba2ad LG |
2820 | if (dai->driver && dai->driver->ops->set_tdm_slot) |
2821 | return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask, | |
a5479e38 | 2822 | slots, slot_width); |
8c6529db | 2823 | else |
b2cbb6e1 | 2824 | return -ENOTSUPP; |
8c6529db LG |
2825 | } |
2826 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot); | |
2827 | ||
472df3cb BS |
2828 | /** |
2829 | * snd_soc_dai_set_channel_map - configure DAI audio channel map | |
2830 | * @dai: DAI | |
2831 | * @tx_num: how many TX channels | |
2832 | * @tx_slot: pointer to an array which imply the TX slot number channel | |
2833 | * 0~num-1 uses | |
2834 | * @rx_num: how many RX channels | |
2835 | * @rx_slot: pointer to an array which imply the RX slot number channel | |
2836 | * 0~num-1 uses | |
2837 | * | |
2838 | * configure the relationship between channel number and TDM slot number. | |
2839 | */ | |
2840 | int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai, | |
2841 | unsigned int tx_num, unsigned int *tx_slot, | |
2842 | unsigned int rx_num, unsigned int *rx_slot) | |
2843 | { | |
f0fba2ad LG |
2844 | if (dai->driver && dai->driver->ops->set_channel_map) |
2845 | return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot, | |
472df3cb BS |
2846 | rx_num, rx_slot); |
2847 | else | |
2848 | return -EINVAL; | |
2849 | } | |
2850 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map); | |
2851 | ||
8c6529db LG |
2852 | /** |
2853 | * snd_soc_dai_set_tristate - configure DAI system or master clock. | |
2854 | * @dai: DAI | |
2855 | * @tristate: tristate enable | |
2856 | * | |
2857 | * Tristates the DAI so that others can use it. | |
2858 | */ | |
2859 | int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate) | |
2860 | { | |
f0fba2ad LG |
2861 | if (dai->driver && dai->driver->ops->set_tristate) |
2862 | return dai->driver->ops->set_tristate(dai, tristate); | |
8c6529db LG |
2863 | else |
2864 | return -EINVAL; | |
2865 | } | |
2866 | EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate); | |
2867 | ||
2868 | /** | |
2869 | * snd_soc_dai_digital_mute - configure DAI system or master clock. | |
2870 | * @dai: DAI | |
2871 | * @mute: mute enable | |
da18396f | 2872 | * @direction: stream to mute |
8c6529db LG |
2873 | * |
2874 | * Mutes the DAI DAC. | |
2875 | */ | |
da18396f MB |
2876 | int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute, |
2877 | int direction) | |
8c6529db | 2878 | { |
da18396f MB |
2879 | if (!dai->driver) |
2880 | return -ENOTSUPP; | |
2881 | ||
2882 | if (dai->driver->ops->mute_stream) | |
2883 | return dai->driver->ops->mute_stream(dai, mute, direction); | |
2884 | else if (direction == SNDRV_PCM_STREAM_PLAYBACK && | |
2885 | dai->driver->ops->digital_mute) | |
f0fba2ad | 2886 | return dai->driver->ops->digital_mute(dai, mute); |
8c6529db | 2887 | else |
04570c62 | 2888 | return -ENOTSUPP; |
8c6529db LG |
2889 | } |
2890 | EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute); | |
2891 | ||
c5af3a2e MB |
2892 | /** |
2893 | * snd_soc_register_card - Register a card with the ASoC core | |
2894 | * | |
ac11a2b3 | 2895 | * @card: Card to register |
c5af3a2e | 2896 | * |
c5af3a2e | 2897 | */ |
70a7ca34 | 2898 | int snd_soc_register_card(struct snd_soc_card *card) |
c5af3a2e | 2899 | { |
923c5e61 | 2900 | int i, ret; |
1a497983 | 2901 | struct snd_soc_pcm_runtime *rtd; |
f0fba2ad | 2902 | |
c5af3a2e MB |
2903 | if (!card->name || !card->dev) |
2904 | return -EINVAL; | |
2905 | ||
5a504963 SW |
2906 | for (i = 0; i < card->num_links; i++) { |
2907 | struct snd_soc_dai_link *link = &card->dai_link[i]; | |
2908 | ||
923c5e61 | 2909 | ret = soc_init_dai_link(card, link); |
88bd870f | 2910 | if (ret) { |
923c5e61 | 2911 | dev_err(card->dev, "ASoC: failed to init link %s\n", |
10e8aa9a | 2912 | link->name); |
923c5e61 | 2913 | return ret; |
5a504963 SW |
2914 | } |
2915 | } | |
2916 | ||
ed77cc12 MB |
2917 | dev_set_drvdata(card->dev, card); |
2918 | ||
111c6419 SW |
2919 | snd_soc_initialize_card_lists(card); |
2920 | ||
f8f80361 ML |
2921 | INIT_LIST_HEAD(&card->dai_link_list); |
2922 | card->num_dai_links = 0; | |
2923 | ||
1a497983 ML |
2924 | INIT_LIST_HEAD(&card->rtd_list); |
2925 | card->num_rtd = 0; | |
2926 | ||
db432b41 | 2927 | INIT_LIST_HEAD(&card->dapm_dirty); |
8a978234 | 2928 | INIT_LIST_HEAD(&card->dobj_list); |
c5af3a2e | 2929 | card->instantiated = 0; |
f0fba2ad | 2930 | mutex_init(&card->mutex); |
a73fb2df | 2931 | mutex_init(&card->dapm_mutex); |
c5af3a2e | 2932 | |
b19e6e7b MB |
2933 | ret = snd_soc_instantiate_card(card); |
2934 | if (ret != 0) | |
4e2576bd | 2935 | return ret; |
c5af3a2e | 2936 | |
988e8cc4 | 2937 | /* deactivate pins to sleep state */ |
1a497983 | 2938 | list_for_each_entry(rtd, &card->rtd_list, list) { |
88bd870f BC |
2939 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
2940 | int j; | |
2941 | ||
2942 | for (j = 0; j < rtd->num_codecs; j++) { | |
2943 | struct snd_soc_dai *codec_dai = rtd->codec_dais[j]; | |
2944 | if (!codec_dai->active) | |
2945 | pinctrl_pm_select_sleep_state(codec_dai->dev); | |
2946 | } | |
2947 | ||
988e8cc4 NC |
2948 | if (!cpu_dai->active) |
2949 | pinctrl_pm_select_sleep_state(cpu_dai->dev); | |
2950 | } | |
2951 | ||
b19e6e7b | 2952 | return ret; |
c5af3a2e | 2953 | } |
70a7ca34 | 2954 | EXPORT_SYMBOL_GPL(snd_soc_register_card); |
c5af3a2e MB |
2955 | |
2956 | /** | |
2957 | * snd_soc_unregister_card - Unregister a card with the ASoC core | |
2958 | * | |
ac11a2b3 | 2959 | * @card: Card to unregister |
c5af3a2e | 2960 | * |
c5af3a2e | 2961 | */ |
70a7ca34 | 2962 | int snd_soc_unregister_card(struct snd_soc_card *card) |
c5af3a2e | 2963 | { |
01e0df66 LPC |
2964 | if (card->instantiated) { |
2965 | card->instantiated = false; | |
1c325f77 | 2966 | snd_soc_dapm_shutdown(card); |
b0e26485 | 2967 | soc_cleanup_card_resources(card); |
8f6f9b29 | 2968 | dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); |
01e0df66 | 2969 | } |
c5af3a2e MB |
2970 | |
2971 | return 0; | |
2972 | } | |
70a7ca34 | 2973 | EXPORT_SYMBOL_GPL(snd_soc_unregister_card); |
c5af3a2e | 2974 | |
f0fba2ad LG |
2975 | /* |
2976 | * Simplify DAI link configuration by removing ".-1" from device names | |
2977 | * and sanitizing names. | |
2978 | */ | |
0b9a214a | 2979 | static char *fmt_single_name(struct device *dev, int *id) |
f0fba2ad LG |
2980 | { |
2981 | char *found, name[NAME_SIZE]; | |
2982 | int id1, id2; | |
2983 | ||
2984 | if (dev_name(dev) == NULL) | |
2985 | return NULL; | |
2986 | ||
58818a77 | 2987 | strlcpy(name, dev_name(dev), NAME_SIZE); |
f0fba2ad LG |
2988 | |
2989 | /* are we a "%s.%d" name (platform and SPI components) */ | |
2990 | found = strstr(name, dev->driver->name); | |
2991 | if (found) { | |
2992 | /* get ID */ | |
2993 | if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { | |
2994 | ||
2995 | /* discard ID from name if ID == -1 */ | |
2996 | if (*id == -1) | |
2997 | found[strlen(dev->driver->name)] = '\0'; | |
2998 | } | |
2999 | ||
3000 | } else { | |
3001 | /* I2C component devices are named "bus-addr" */ | |
3002 | if (sscanf(name, "%x-%x", &id1, &id2) == 2) { | |
3003 | char tmp[NAME_SIZE]; | |
3004 | ||
3005 | /* create unique ID number from I2C addr and bus */ | |
05899446 | 3006 | *id = ((id1 & 0xffff) << 16) + id2; |
f0fba2ad LG |
3007 | |
3008 | /* sanitize component name for DAI link creation */ | |
3009 | snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name); | |
58818a77 | 3010 | strlcpy(name, tmp, NAME_SIZE); |
f0fba2ad LG |
3011 | } else |
3012 | *id = 0; | |
3013 | } | |
3014 | ||
3015 | return kstrdup(name, GFP_KERNEL); | |
3016 | } | |
3017 | ||
3018 | /* | |
3019 | * Simplify DAI link naming for single devices with multiple DAIs by removing | |
3020 | * any ".-1" and using the DAI name (instead of device name). | |
3021 | */ | |
3022 | static inline char *fmt_multiple_name(struct device *dev, | |
3023 | struct snd_soc_dai_driver *dai_drv) | |
3024 | { | |
3025 | if (dai_drv->name == NULL) { | |
10e8aa9a MM |
3026 | dev_err(dev, |
3027 | "ASoC: error - multiple DAI %s registered with no name\n", | |
3028 | dev_name(dev)); | |
f0fba2ad LG |
3029 | return NULL; |
3030 | } | |
3031 | ||
3032 | return kstrdup(dai_drv->name, GFP_KERNEL); | |
3033 | } | |
3034 | ||
9115171a | 3035 | /** |
32c9ba54 | 3036 | * snd_soc_unregister_dai - Unregister DAIs from the ASoC core |
9115171a | 3037 | * |
32c9ba54 | 3038 | * @component: The component for which the DAIs should be unregistered |
9115171a | 3039 | */ |
32c9ba54 | 3040 | static void snd_soc_unregister_dais(struct snd_soc_component *component) |
9115171a | 3041 | { |
5c1d5f09 | 3042 | struct snd_soc_dai *dai, *_dai; |
9115171a | 3043 | |
5c1d5f09 | 3044 | list_for_each_entry_safe(dai, _dai, &component->dai_list, list) { |
32c9ba54 LPC |
3045 | dev_dbg(component->dev, "ASoC: Unregistered DAI '%s'\n", |
3046 | dai->name); | |
5c1d5f09 | 3047 | list_del(&dai->list); |
32c9ba54 | 3048 | kfree(dai->name); |
f0fba2ad | 3049 | kfree(dai); |
f0fba2ad | 3050 | } |
9115171a | 3051 | } |
9115171a | 3052 | |
5e4fb372 ML |
3053 | /* Create a DAI and add it to the component's DAI list */ |
3054 | static struct snd_soc_dai *soc_add_dai(struct snd_soc_component *component, | |
3055 | struct snd_soc_dai_driver *dai_drv, | |
3056 | bool legacy_dai_naming) | |
3057 | { | |
3058 | struct device *dev = component->dev; | |
3059 | struct snd_soc_dai *dai; | |
3060 | ||
3061 | dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); | |
3062 | ||
3063 | dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL); | |
3064 | if (dai == NULL) | |
3065 | return NULL; | |
3066 | ||
3067 | /* | |
3068 | * Back in the old days when we still had component-less DAIs, | |
3069 | * instead of having a static name, component-less DAIs would | |
3070 | * inherit the name of the parent device so it is possible to | |
3071 | * register multiple instances of the DAI. We still need to keep | |
3072 | * the same naming style even though those DAIs are not | |
3073 | * component-less anymore. | |
3074 | */ | |
3075 | if (legacy_dai_naming && | |
3076 | (dai_drv->id == 0 || dai_drv->name == NULL)) { | |
3077 | dai->name = fmt_single_name(dev, &dai->id); | |
3078 | } else { | |
3079 | dai->name = fmt_multiple_name(dev, dai_drv); | |
3080 | if (dai_drv->id) | |
3081 | dai->id = dai_drv->id; | |
3082 | else | |
3083 | dai->id = component->num_dai; | |
3084 | } | |
3085 | if (dai->name == NULL) { | |
3086 | kfree(dai); | |
3087 | return NULL; | |
3088 | } | |
3089 | ||
3090 | dai->component = component; | |
3091 | dai->dev = dev; | |
3092 | dai->driver = dai_drv; | |
3093 | if (!dai->driver->ops) | |
3094 | dai->driver->ops = &null_dai_ops; | |
3095 | ||
3096 | list_add(&dai->list, &component->dai_list); | |
3097 | component->num_dai++; | |
3098 | ||
3099 | dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); | |
3100 | return dai; | |
3101 | } | |
3102 | ||
9115171a | 3103 | /** |
32c9ba54 | 3104 | * snd_soc_register_dais - Register a DAI with the ASoC core |
9115171a | 3105 | * |
6106d129 LPC |
3106 | * @component: The component the DAIs are registered for |
3107 | * @dai_drv: DAI driver to use for the DAIs | |
ac11a2b3 | 3108 | * @count: Number of DAIs |
32c9ba54 LPC |
3109 | * @legacy_dai_naming: Use the legacy naming scheme and let the DAI inherit the |
3110 | * parent's name. | |
9115171a | 3111 | */ |
6106d129 | 3112 | static int snd_soc_register_dais(struct snd_soc_component *component, |
bb13109d LPC |
3113 | struct snd_soc_dai_driver *dai_drv, size_t count, |
3114 | bool legacy_dai_naming) | |
9115171a | 3115 | { |
6106d129 | 3116 | struct device *dev = component->dev; |
f0fba2ad | 3117 | struct snd_soc_dai *dai; |
32c9ba54 LPC |
3118 | unsigned int i; |
3119 | int ret; | |
f0fba2ad | 3120 | |
5b5e0928 | 3121 | dev_dbg(dev, "ASoC: dai register %s #%zu\n", dev_name(dev), count); |
9115171a | 3122 | |
bb13109d | 3123 | component->dai_drv = dai_drv; |
bb13109d | 3124 | |
9115171a | 3125 | for (i = 0; i < count; i++) { |
f0fba2ad | 3126 | |
5e4fb372 ML |
3127 | dai = soc_add_dai(component, dai_drv + i, |
3128 | count == 1 && legacy_dai_naming); | |
c46e0079 AL |
3129 | if (dai == NULL) { |
3130 | ret = -ENOMEM; | |
3131 | goto err; | |
3132 | } | |
9115171a | 3133 | } |
f0fba2ad | 3134 | |
9115171a | 3135 | return 0; |
f0fba2ad | 3136 | |
9115171a | 3137 | err: |
32c9ba54 | 3138 | snd_soc_unregister_dais(component); |
f0fba2ad | 3139 | |
9115171a MB |
3140 | return ret; |
3141 | } | |
9115171a | 3142 | |
68003e6c ML |
3143 | /** |
3144 | * snd_soc_register_dai - Register a DAI dynamically & create its widgets | |
3145 | * | |
3146 | * @component: The component the DAIs are registered for | |
3147 | * @dai_drv: DAI driver to use for the DAI | |
3148 | * | |
3149 | * Topology can use this API to register DAIs when probing a component. | |
3150 | * These DAIs's widgets will be freed in the card cleanup and the DAIs | |
3151 | * will be freed in the component cleanup. | |
3152 | */ | |
3153 | int snd_soc_register_dai(struct snd_soc_component *component, | |
3154 | struct snd_soc_dai_driver *dai_drv) | |
3155 | { | |
3156 | struct snd_soc_dapm_context *dapm = | |
3157 | snd_soc_component_get_dapm(component); | |
3158 | struct snd_soc_dai *dai; | |
3159 | int ret; | |
054880fe | 3160 | |
68003e6c ML |
3161 | if (dai_drv->dobj.type != SND_SOC_DOBJ_PCM) { |
3162 | dev_err(component->dev, "Invalid dai type %d\n", | |
3163 | dai_drv->dobj.type); | |
3164 | return -EINVAL; | |
9115171a MB |
3165 | } |
3166 | ||
68003e6c ML |
3167 | lockdep_assert_held(&client_mutex); |
3168 | dai = soc_add_dai(component, dai_drv, false); | |
3169 | if (!dai) | |
3170 | return -ENOMEM; | |
9115171a | 3171 | |
68003e6c ML |
3172 | /* Create the DAI widgets here. After adding DAIs, topology may |
3173 | * also add routes that need these widgets as source or sink. | |
3174 | */ | |
3175 | ret = snd_soc_dapm_new_dai_widgets(dapm, dai); | |
3176 | if (ret != 0) { | |
3177 | dev_err(component->dev, | |
3178 | "Failed to create DAI widgets %d\n", ret); | |
3179 | } | |
9115171a MB |
3180 | |
3181 | return ret; | |
3182 | } | |
68003e6c | 3183 | EXPORT_SYMBOL_GPL(snd_soc_register_dai); |
9115171a | 3184 | |
14e8bdeb LPC |
3185 | static void snd_soc_component_seq_notifier(struct snd_soc_dapm_context *dapm, |
3186 | enum snd_soc_dapm_type type, int subseq) | |
d191bd8d | 3187 | { |
14e8bdeb | 3188 | struct snd_soc_component *component = dapm->component; |
d191bd8d | 3189 | |
14e8bdeb LPC |
3190 | component->driver->seq_notifier(component, type, subseq); |
3191 | } | |
d191bd8d | 3192 | |
14e8bdeb LPC |
3193 | static int snd_soc_component_stream_event(struct snd_soc_dapm_context *dapm, |
3194 | int event) | |
3195 | { | |
3196 | struct snd_soc_component *component = dapm->component; | |
d191bd8d | 3197 | |
14e8bdeb LPC |
3198 | return component->driver->stream_event(component, event); |
3199 | } | |
e2c330b9 | 3200 | |
bb13109d LPC |
3201 | static int snd_soc_component_initialize(struct snd_soc_component *component, |
3202 | const struct snd_soc_component_driver *driver, struct device *dev) | |
d191bd8d | 3203 | { |
ce0fc93a LPC |
3204 | struct snd_soc_dapm_context *dapm; |
3205 | ||
bb13109d LPC |
3206 | component->name = fmt_single_name(dev, &component->id); |
3207 | if (!component->name) { | |
3208 | dev_err(dev, "ASoC: Failed to allocate name\n"); | |
d191bd8d KM |
3209 | return -ENOMEM; |
3210 | } | |
3211 | ||
bb13109d LPC |
3212 | component->dev = dev; |
3213 | component->driver = driver; | |
61aca564 LPC |
3214 | component->probe = component->driver->probe; |
3215 | component->remove = component->driver->remove; | |
9178feb4 KM |
3216 | component->suspend = component->driver->suspend; |
3217 | component->resume = component->driver->resume; | |
99b04f4c | 3218 | component->pcm_new = component->driver->pcm_new; |
ec5a82d6 | 3219 | component->pcm_free = component->driver->pcm_free; |
d191bd8d | 3220 | |
4890140f | 3221 | dapm = &component->dapm; |
ce0fc93a LPC |
3222 | dapm->dev = dev; |
3223 | dapm->component = component; | |
3224 | dapm->bias_level = SND_SOC_BIAS_OFF; | |
5819c2fa | 3225 | dapm->idle_bias_off = true; |
14e8bdeb LPC |
3226 | if (driver->seq_notifier) |
3227 | dapm->seq_notifier = snd_soc_component_seq_notifier; | |
3228 | if (driver->stream_event) | |
3229 | dapm->stream_event = snd_soc_component_stream_event; | |
d191bd8d | 3230 | |
61aca564 LPC |
3231 | component->controls = driver->controls; |
3232 | component->num_controls = driver->num_controls; | |
3233 | component->dapm_widgets = driver->dapm_widgets; | |
3234 | component->num_dapm_widgets = driver->num_dapm_widgets; | |
3235 | component->dapm_routes = driver->dapm_routes; | |
3236 | component->num_dapm_routes = driver->num_dapm_routes; | |
3237 | ||
bb13109d LPC |
3238 | INIT_LIST_HEAD(&component->dai_list); |
3239 | mutex_init(&component->io_mutex); | |
d191bd8d | 3240 | |
bb13109d LPC |
3241 | return 0; |
3242 | } | |
d191bd8d | 3243 | |
20feb881 | 3244 | static void snd_soc_component_setup_regmap(struct snd_soc_component *component) |
886f5692 | 3245 | { |
20feb881 LPC |
3246 | int val_bytes = regmap_get_val_bytes(component->regmap); |
3247 | ||
3248 | /* Errors are legitimate for non-integer byte multiples */ | |
3249 | if (val_bytes > 0) | |
3250 | component->val_bytes = val_bytes; | |
3251 | } | |
3252 | ||
e874bf5f LPC |
3253 | #ifdef CONFIG_REGMAP |
3254 | ||
20feb881 LPC |
3255 | /** |
3256 | * snd_soc_component_init_regmap() - Initialize regmap instance for the component | |
3257 | * @component: The component for which to initialize the regmap instance | |
3258 | * @regmap: The regmap instance that should be used by the component | |
3259 | * | |
3260 | * This function allows deferred assignment of the regmap instance that is | |
3261 | * associated with the component. Only use this if the regmap instance is not | |
3262 | * yet ready when the component is registered. The function must also be called | |
3263 | * before the first IO attempt of the component. | |
3264 | */ | |
3265 | void snd_soc_component_init_regmap(struct snd_soc_component *component, | |
3266 | struct regmap *regmap) | |
3267 | { | |
3268 | component->regmap = regmap; | |
3269 | snd_soc_component_setup_regmap(component); | |
886f5692 | 3270 | } |
20feb881 LPC |
3271 | EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); |
3272 | ||
3273 | /** | |
3274 | * snd_soc_component_exit_regmap() - De-initialize regmap instance for the component | |
3275 | * @component: The component for which to de-initialize the regmap instance | |
3276 | * | |
3277 | * Calls regmap_exit() on the regmap instance associated to the component and | |
3278 | * removes the regmap instance from the component. | |
3279 | * | |
3280 | * This function should only be used if snd_soc_component_init_regmap() was used | |
3281 | * to initialize the regmap instance. | |
3282 | */ | |
3283 | void snd_soc_component_exit_regmap(struct snd_soc_component *component) | |
3284 | { | |
3285 | regmap_exit(component->regmap); | |
3286 | component->regmap = NULL; | |
3287 | } | |
3288 | EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); | |
886f5692 | 3289 | |
e874bf5f | 3290 | #endif |
886f5692 | 3291 | |
bb13109d LPC |
3292 | static void snd_soc_component_add_unlocked(struct snd_soc_component *component) |
3293 | { | |
20feb881 LPC |
3294 | if (!component->write && !component->read) { |
3295 | if (!component->regmap) | |
3296 | component->regmap = dev_get_regmap(component->dev, NULL); | |
3297 | if (component->regmap) | |
3298 | snd_soc_component_setup_regmap(component); | |
3299 | } | |
886f5692 | 3300 | |
bb13109d | 3301 | list_add(&component->list, &component_list); |
8a978234 | 3302 | INIT_LIST_HEAD(&component->dobj_list); |
bb13109d | 3303 | } |
d191bd8d | 3304 | |
bb13109d LPC |
3305 | static void snd_soc_component_add(struct snd_soc_component *component) |
3306 | { | |
d191bd8d | 3307 | mutex_lock(&client_mutex); |
bb13109d | 3308 | snd_soc_component_add_unlocked(component); |
d191bd8d | 3309 | mutex_unlock(&client_mutex); |
bb13109d | 3310 | } |
d191bd8d | 3311 | |
bb13109d LPC |
3312 | static void snd_soc_component_cleanup(struct snd_soc_component *component) |
3313 | { | |
3314 | snd_soc_unregister_dais(component); | |
3315 | kfree(component->name); | |
3316 | } | |
d191bd8d | 3317 | |
bb13109d LPC |
3318 | static void snd_soc_component_del_unlocked(struct snd_soc_component *component) |
3319 | { | |
c12c1aad KM |
3320 | struct snd_soc_card *card = component->card; |
3321 | ||
3322 | if (card) | |
3323 | snd_soc_unregister_card(card); | |
3324 | ||
bb13109d LPC |
3325 | list_del(&component->list); |
3326 | } | |
d191bd8d | 3327 | |
d191bd8d | 3328 | int snd_soc_register_component(struct device *dev, |
cf9e829e | 3329 | const struct snd_soc_component_driver *component_driver, |
d191bd8d KM |
3330 | struct snd_soc_dai_driver *dai_drv, |
3331 | int num_dai) | |
3332 | { | |
cf9e829e | 3333 | struct snd_soc_component *component; |
bb13109d | 3334 | int ret; |
d191bd8d | 3335 | |
cf9e829e KM |
3336 | component = kzalloc(sizeof(*component), GFP_KERNEL); |
3337 | if (!component) { | |
d191bd8d KM |
3338 | dev_err(dev, "ASoC: Failed to allocate memory\n"); |
3339 | return -ENOMEM; | |
3340 | } | |
3341 | ||
cf9e829e | 3342 | ret = snd_soc_component_initialize(component, component_driver, dev); |
bb13109d LPC |
3343 | if (ret) |
3344 | goto err_free; | |
3345 | ||
cf9e829e KM |
3346 | component->ignore_pmdown_time = true; |
3347 | component->registered_as_component = true; | |
3d59400f | 3348 | |
cf9e829e | 3349 | ret = snd_soc_register_dais(component, dai_drv, num_dai, true); |
bb13109d | 3350 | if (ret < 0) { |
f42cf8d6 | 3351 | dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); |
bb13109d LPC |
3352 | goto err_cleanup; |
3353 | } | |
d191bd8d | 3354 | |
cf9e829e | 3355 | snd_soc_component_add(component); |
4da53393 | 3356 | |
bb13109d | 3357 | return 0; |
4da53393 | 3358 | |
bb13109d | 3359 | err_cleanup: |
cf9e829e | 3360 | snd_soc_component_cleanup(component); |
bb13109d | 3361 | err_free: |
cf9e829e | 3362 | kfree(component); |
bb13109d | 3363 | return ret; |
4da53393 | 3364 | } |
bb13109d | 3365 | EXPORT_SYMBOL_GPL(snd_soc_register_component); |
4da53393 | 3366 | |
d191bd8d | 3367 | /** |
2eccea8c KM |
3368 | * snd_soc_unregister_component - Unregister all related component |
3369 | * from the ASoC core | |
d191bd8d | 3370 | * |
628536ea | 3371 | * @dev: The device to unregister |
d191bd8d | 3372 | */ |
2eccea8c | 3373 | static int __snd_soc_unregister_component(struct device *dev) |
d191bd8d | 3374 | { |
cf9e829e | 3375 | struct snd_soc_component *component; |
21a03528 | 3376 | int found = 0; |
d191bd8d | 3377 | |
34e81ab4 | 3378 | mutex_lock(&client_mutex); |
cf9e829e | 3379 | list_for_each_entry(component, &component_list, list) { |
21a03528 KM |
3380 | if (dev != component->dev || |
3381 | !component->registered_as_component) | |
3382 | continue; | |
3383 | ||
3384 | snd_soc_tplg_component_remove(component, SND_SOC_TPLG_INDEX_ALL); | |
3385 | snd_soc_component_del_unlocked(component); | |
3386 | found = 1; | |
3387 | break; | |
d191bd8d | 3388 | } |
34e81ab4 | 3389 | mutex_unlock(&client_mutex); |
d191bd8d | 3390 | |
21a03528 KM |
3391 | if (found) { |
3392 | snd_soc_component_cleanup(component); | |
3393 | kfree(component); | |
3394 | } | |
2eccea8c KM |
3395 | |
3396 | return found; | |
3397 | } | |
3398 | ||
3399 | void snd_soc_unregister_component(struct device *dev) | |
3400 | { | |
3401 | while (__snd_soc_unregister_component(dev)); | |
d191bd8d KM |
3402 | } |
3403 | EXPORT_SYMBOL_GPL(snd_soc_unregister_component); | |
d191bd8d | 3404 | |
f1d45cc3 | 3405 | static int snd_soc_platform_drv_probe(struct snd_soc_component *component) |
e2c330b9 LPC |
3406 | { |
3407 | struct snd_soc_platform *platform = snd_soc_component_to_platform(component); | |
d191bd8d | 3408 | |
f1d45cc3 | 3409 | return platform->driver->probe(platform); |
e2c330b9 LPC |
3410 | } |
3411 | ||
f1d45cc3 | 3412 | static void snd_soc_platform_drv_remove(struct snd_soc_component *component) |
e2c330b9 LPC |
3413 | { |
3414 | struct snd_soc_platform *platform = snd_soc_component_to_platform(component); | |
3415 | ||
f1d45cc3 | 3416 | platform->driver->remove(platform); |
d191bd8d | 3417 | } |
d191bd8d | 3418 | |
99b04f4c KM |
3419 | static int snd_soc_platform_drv_pcm_new(struct snd_soc_pcm_runtime *rtd) |
3420 | { | |
3421 | struct snd_soc_platform *platform = rtd->platform; | |
3422 | ||
d6c098a1 BN |
3423 | if (platform->driver->pcm_new) |
3424 | return platform->driver->pcm_new(rtd); | |
3425 | else | |
3426 | return 0; | |
99b04f4c KM |
3427 | } |
3428 | ||
3429 | static void snd_soc_platform_drv_pcm_free(struct snd_pcm *pcm) | |
3430 | { | |
3431 | struct snd_soc_pcm_runtime *rtd = pcm->private_data; | |
3432 | struct snd_soc_platform *platform = rtd->platform; | |
3433 | ||
d6c098a1 BN |
3434 | if (platform->driver->pcm_free) |
3435 | platform->driver->pcm_free(pcm); | |
99b04f4c KM |
3436 | } |
3437 | ||
12a48a8c | 3438 | /** |
71a45cda LPC |
3439 | * snd_soc_add_platform - Add a platform to the ASoC core |
3440 | * @dev: The parent device for the platform | |
3441 | * @platform: The platform to add | |
7d1442b4 | 3442 | * @platform_drv: The driver for the platform |
12a48a8c | 3443 | */ |
71a45cda | 3444 | int snd_soc_add_platform(struct device *dev, struct snd_soc_platform *platform, |
d79e57db | 3445 | const struct snd_soc_platform_driver *platform_drv) |
12a48a8c | 3446 | { |
b37f1d12 LPC |
3447 | int ret; |
3448 | ||
bb13109d LPC |
3449 | ret = snd_soc_component_initialize(&platform->component, |
3450 | &platform_drv->component_driver, dev); | |
3451 | if (ret) | |
3452 | return ret; | |
12a48a8c | 3453 | |
f0fba2ad LG |
3454 | platform->dev = dev; |
3455 | platform->driver = platform_drv; | |
f1d45cc3 LPC |
3456 | |
3457 | if (platform_drv->probe) | |
3458 | platform->component.probe = snd_soc_platform_drv_probe; | |
3459 | if (platform_drv->remove) | |
3460 | platform->component.remove = snd_soc_platform_drv_remove; | |
99b04f4c KM |
3461 | if (platform_drv->pcm_new) |
3462 | platform->component.pcm_new = snd_soc_platform_drv_pcm_new; | |
3463 | if (platform_drv->pcm_free) | |
3464 | platform->component.pcm_free = snd_soc_platform_drv_pcm_free; | |
12a48a8c | 3465 | |
81c7cfd1 LPC |
3466 | #ifdef CONFIG_DEBUG_FS |
3467 | platform->component.debugfs_prefix = "platform"; | |
3468 | #endif | |
12a48a8c MB |
3469 | |
3470 | mutex_lock(&client_mutex); | |
bb13109d | 3471 | snd_soc_component_add_unlocked(&platform->component); |
12a48a8c MB |
3472 | list_add(&platform->list, &platform_list); |
3473 | mutex_unlock(&client_mutex); | |
3474 | ||
f4333203 LPC |
3475 | dev_dbg(dev, "ASoC: Registered platform '%s'\n", |
3476 | platform->component.name); | |
12a48a8c MB |
3477 | |
3478 | return 0; | |
3479 | } | |
71a45cda | 3480 | EXPORT_SYMBOL_GPL(snd_soc_add_platform); |
12a48a8c MB |
3481 | |
3482 | /** | |
71a45cda | 3483 | * snd_soc_register_platform - Register a platform with the ASoC core |
12a48a8c | 3484 | * |
628536ea JC |
3485 | * @dev: The device for the platform |
3486 | * @platform_drv: The driver for the platform | |
12a48a8c | 3487 | */ |
71a45cda LPC |
3488 | int snd_soc_register_platform(struct device *dev, |
3489 | const struct snd_soc_platform_driver *platform_drv) | |
12a48a8c | 3490 | { |
f0fba2ad | 3491 | struct snd_soc_platform *platform; |
71a45cda | 3492 | int ret; |
f0fba2ad | 3493 | |
71a45cda | 3494 | dev_dbg(dev, "ASoC: platform register %s\n", dev_name(dev)); |
f0fba2ad | 3495 | |
71a45cda LPC |
3496 | platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL); |
3497 | if (platform == NULL) | |
3498 | return -ENOMEM; | |
3499 | ||
3500 | ret = snd_soc_add_platform(dev, platform, platform_drv); | |
3501 | if (ret) | |
3502 | kfree(platform); | |
3503 | ||
3504 | return ret; | |
3505 | } | |
3506 | EXPORT_SYMBOL_GPL(snd_soc_register_platform); | |
3507 | ||
3508 | /** | |
3509 | * snd_soc_remove_platform - Remove a platform from the ASoC core | |
3510 | * @platform: the platform to remove | |
3511 | */ | |
3512 | void snd_soc_remove_platform(struct snd_soc_platform *platform) | |
3513 | { | |
b37f1d12 | 3514 | |
12a48a8c MB |
3515 | mutex_lock(&client_mutex); |
3516 | list_del(&platform->list); | |
bb13109d | 3517 | snd_soc_component_del_unlocked(&platform->component); |
12a48a8c MB |
3518 | mutex_unlock(&client_mutex); |
3519 | ||
71a45cda | 3520 | dev_dbg(platform->dev, "ASoC: Unregistered platform '%s'\n", |
f4333203 | 3521 | platform->component.name); |
decc27b0 DM |
3522 | |
3523 | snd_soc_component_cleanup(&platform->component); | |
71a45cda LPC |
3524 | } |
3525 | EXPORT_SYMBOL_GPL(snd_soc_remove_platform); | |
3526 | ||
3527 | struct snd_soc_platform *snd_soc_lookup_platform(struct device *dev) | |
3528 | { | |
3529 | struct snd_soc_platform *platform; | |
3530 | ||
34e81ab4 | 3531 | mutex_lock(&client_mutex); |
71a45cda | 3532 | list_for_each_entry(platform, &platform_list, list) { |
34e81ab4 LPC |
3533 | if (dev == platform->dev) { |
3534 | mutex_unlock(&client_mutex); | |
71a45cda | 3535 | return platform; |
34e81ab4 | 3536 | } |
71a45cda | 3537 | } |
34e81ab4 | 3538 | mutex_unlock(&client_mutex); |
71a45cda LPC |
3539 | |
3540 | return NULL; | |
3541 | } | |
3542 | EXPORT_SYMBOL_GPL(snd_soc_lookup_platform); | |
3543 | ||
3544 | /** | |
3545 | * snd_soc_unregister_platform - Unregister a platform from the ASoC core | |
3546 | * | |
628536ea | 3547 | * @dev: platform to unregister |
71a45cda LPC |
3548 | */ |
3549 | void snd_soc_unregister_platform(struct device *dev) | |
3550 | { | |
3551 | struct snd_soc_platform *platform; | |
3552 | ||
3553 | platform = snd_soc_lookup_platform(dev); | |
3554 | if (!platform) | |
3555 | return; | |
3556 | ||
3557 | snd_soc_remove_platform(platform); | |
f0fba2ad | 3558 | kfree(platform); |
12a48a8c MB |
3559 | } |
3560 | EXPORT_SYMBOL_GPL(snd_soc_unregister_platform); | |
3561 | ||
151ab22c MB |
3562 | static u64 codec_format_map[] = { |
3563 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE, | |
3564 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE, | |
3565 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE, | |
3566 | SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE, | |
3567 | SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE, | |
3568 | SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE, | |
3569 | SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
3570 | SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE, | |
3571 | SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE, | |
3572 | SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE, | |
3573 | SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE, | |
3574 | SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE, | |
3575 | SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE, | |
3576 | SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE, | |
3577 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | |
3578 | | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE, | |
3579 | }; | |
3580 | ||
3581 | /* Fix up the DAI formats for endianness: codecs don't actually see | |
3582 | * the endianness of the data but we're using the CPU format | |
3583 | * definitions which do need to include endianness so we ensure that | |
3584 | * codec DAIs always have both big and little endian variants set. | |
3585 | */ | |
3586 | static void fixup_codec_formats(struct snd_soc_pcm_stream *stream) | |
3587 | { | |
3588 | int i; | |
3589 | ||
3590 | for (i = 0; i < ARRAY_SIZE(codec_format_map); i++) | |
3591 | if (stream->formats & codec_format_map[i]) | |
3592 | stream->formats |= codec_format_map[i]; | |
3593 | } | |
3594 | ||
f1d45cc3 LPC |
3595 | static int snd_soc_codec_drv_probe(struct snd_soc_component *component) |
3596 | { | |
3597 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3598 | ||
3599 | return codec->driver->probe(codec); | |
3600 | } | |
3601 | ||
3602 | static void snd_soc_codec_drv_remove(struct snd_soc_component *component) | |
3603 | { | |
3604 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3605 | ||
3606 | codec->driver->remove(codec); | |
3607 | } | |
3608 | ||
9178feb4 KM |
3609 | static int snd_soc_codec_drv_suspend(struct snd_soc_component *component) |
3610 | { | |
3611 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3612 | ||
3613 | return codec->driver->suspend(codec); | |
3614 | } | |
3615 | ||
3616 | static int snd_soc_codec_drv_resume(struct snd_soc_component *component) | |
3617 | { | |
3618 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3619 | ||
3620 | return codec->driver->resume(codec); | |
3621 | } | |
3622 | ||
e2c330b9 LPC |
3623 | static int snd_soc_codec_drv_write(struct snd_soc_component *component, |
3624 | unsigned int reg, unsigned int val) | |
3625 | { | |
3626 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3627 | ||
3628 | return codec->driver->write(codec, reg, val); | |
3629 | } | |
3630 | ||
3631 | static int snd_soc_codec_drv_read(struct snd_soc_component *component, | |
3632 | unsigned int reg, unsigned int *val) | |
3633 | { | |
3634 | struct snd_soc_codec *codec = snd_soc_component_to_codec(component); | |
3635 | ||
3636 | *val = codec->driver->read(codec, reg); | |
3637 | ||
3638 | return 0; | |
3639 | } | |
3640 | ||
68f831c2 LPC |
3641 | static int snd_soc_codec_set_bias_level(struct snd_soc_dapm_context *dapm, |
3642 | enum snd_soc_bias_level level) | |
3643 | { | |
3644 | struct snd_soc_codec *codec = snd_soc_dapm_to_codec(dapm); | |
3645 | ||
3646 | return codec->driver->set_bias_level(codec, level); | |
3647 | } | |
3648 | ||
0d0cf00a MB |
3649 | /** |
3650 | * snd_soc_register_codec - Register a codec with the ASoC core | |
3651 | * | |
628536ea JC |
3652 | * @dev: The parent device for this codec |
3653 | * @codec_drv: Codec driver | |
3654 | * @dai_drv: The associated DAI driver | |
3655 | * @num_dai: Number of DAIs | |
0d0cf00a | 3656 | */ |
f0fba2ad | 3657 | int snd_soc_register_codec(struct device *dev, |
001ae4c0 MB |
3658 | const struct snd_soc_codec_driver *codec_drv, |
3659 | struct snd_soc_dai_driver *dai_drv, | |
3660 | int num_dai) | |
0d0cf00a | 3661 | { |
4890140f | 3662 | struct snd_soc_dapm_context *dapm; |
f0fba2ad | 3663 | struct snd_soc_codec *codec; |
bb13109d | 3664 | struct snd_soc_dai *dai; |
f0fba2ad | 3665 | int ret, i; |
151ab22c | 3666 | |
f0fba2ad LG |
3667 | dev_dbg(dev, "codec register %s\n", dev_name(dev)); |
3668 | ||
3669 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); | |
3670 | if (codec == NULL) | |
3671 | return -ENOMEM; | |
0d0cf00a | 3672 | |
f1d45cc3 | 3673 | codec->component.codec = codec; |
ce0fc93a | 3674 | |
bb13109d LPC |
3675 | ret = snd_soc_component_initialize(&codec->component, |
3676 | &codec_drv->component_driver, dev); | |
3677 | if (ret) | |
3678 | goto err_free; | |
f0fba2ad | 3679 | |
f1d45cc3 LPC |
3680 | if (codec_drv->probe) |
3681 | codec->component.probe = snd_soc_codec_drv_probe; | |
3682 | if (codec_drv->remove) | |
3683 | codec->component.remove = snd_soc_codec_drv_remove; | |
9178feb4 KM |
3684 | if (codec_drv->suspend) |
3685 | codec->component.suspend = snd_soc_codec_drv_suspend; | |
3686 | if (codec_drv->resume) | |
3687 | codec->component.resume = snd_soc_codec_drv_resume; | |
e2c330b9 LPC |
3688 | if (codec_drv->write) |
3689 | codec->component.write = snd_soc_codec_drv_write; | |
3690 | if (codec_drv->read) | |
3691 | codec->component.read = snd_soc_codec_drv_read; | |
3d59400f | 3692 | codec->component.ignore_pmdown_time = codec_drv->ignore_pmdown_time; |
4890140f LPC |
3693 | |
3694 | dapm = snd_soc_codec_get_dapm(codec); | |
3695 | dapm->idle_bias_off = codec_drv->idle_bias_off; | |
3696 | dapm->suspend_bias_off = codec_drv->suspend_bias_off; | |
14e8bdeb | 3697 | if (codec_drv->seq_notifier) |
4890140f | 3698 | dapm->seq_notifier = codec_drv->seq_notifier; |
68f831c2 | 3699 | if (codec_drv->set_bias_level) |
4890140f | 3700 | dapm->set_bias_level = snd_soc_codec_set_bias_level; |
7a30a3db DP |
3701 | codec->dev = dev; |
3702 | codec->driver = codec_drv; | |
e2c330b9 | 3703 | codec->component.val_bytes = codec_drv->reg_word_size; |
ce6120cc | 3704 | |
81c7cfd1 LPC |
3705 | #ifdef CONFIG_DEBUG_FS |
3706 | codec->component.init_debugfs = soc_init_codec_debugfs; | |
3707 | codec->component.debugfs_prefix = "codec"; | |
3708 | #endif | |
3709 | ||
886f5692 LPC |
3710 | if (codec_drv->get_regmap) |
3711 | codec->component.regmap = codec_drv->get_regmap(dev); | |
a39f75f7 | 3712 | |
f0fba2ad LG |
3713 | for (i = 0; i < num_dai; i++) { |
3714 | fixup_codec_formats(&dai_drv[i].playback); | |
3715 | fixup_codec_formats(&dai_drv[i].capture); | |
3716 | } | |
3717 | ||
bb13109d | 3718 | ret = snd_soc_register_dais(&codec->component, dai_drv, num_dai, false); |
5acd7dfb | 3719 | if (ret < 0) { |
f42cf8d6 | 3720 | dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); |
bb13109d | 3721 | goto err_cleanup; |
26b01ccd | 3722 | } |
f0fba2ad | 3723 | |
bb13109d LPC |
3724 | list_for_each_entry(dai, &codec->component.dai_list, list) |
3725 | dai->codec = codec; | |
f0fba2ad | 3726 | |
e1328a83 | 3727 | mutex_lock(&client_mutex); |
bb13109d | 3728 | snd_soc_component_add_unlocked(&codec->component); |
054880fe | 3729 | list_add(&codec->list, &codec_list); |
e1328a83 KM |
3730 | mutex_unlock(&client_mutex); |
3731 | ||
f4333203 LPC |
3732 | dev_dbg(codec->dev, "ASoC: Registered codec '%s'\n", |
3733 | codec->component.name); | |
0d0cf00a | 3734 | return 0; |
f0fba2ad | 3735 | |
bb13109d LPC |
3736 | err_cleanup: |
3737 | snd_soc_component_cleanup(&codec->component); | |
3738 | err_free: | |
f0fba2ad LG |
3739 | kfree(codec); |
3740 | return ret; | |
0d0cf00a MB |
3741 | } |
3742 | EXPORT_SYMBOL_GPL(snd_soc_register_codec); | |
3743 | ||
3744 | /** | |
3745 | * snd_soc_unregister_codec - Unregister a codec from the ASoC core | |
3746 | * | |
628536ea | 3747 | * @dev: codec to unregister |
0d0cf00a | 3748 | */ |
f0fba2ad | 3749 | void snd_soc_unregister_codec(struct device *dev) |
0d0cf00a | 3750 | { |
f0fba2ad | 3751 | struct snd_soc_codec *codec; |
f0fba2ad | 3752 | |
34e81ab4 | 3753 | mutex_lock(&client_mutex); |
f0fba2ad LG |
3754 | list_for_each_entry(codec, &codec_list, list) { |
3755 | if (dev == codec->dev) | |
3756 | goto found; | |
3757 | } | |
34e81ab4 | 3758 | mutex_unlock(&client_mutex); |
f0fba2ad LG |
3759 | return; |
3760 | ||
3761 | found: | |
0d0cf00a | 3762 | list_del(&codec->list); |
bb13109d | 3763 | snd_soc_component_del_unlocked(&codec->component); |
0d0cf00a MB |
3764 | mutex_unlock(&client_mutex); |
3765 | ||
f4333203 LPC |
3766 | dev_dbg(codec->dev, "ASoC: Unregistered codec '%s'\n", |
3767 | codec->component.name); | |
f0fba2ad | 3768 | |
bb13109d | 3769 | snd_soc_component_cleanup(&codec->component); |
7a30a3db | 3770 | snd_soc_cache_exit(codec); |
f0fba2ad | 3771 | kfree(codec); |
0d0cf00a MB |
3772 | } |
3773 | EXPORT_SYMBOL_GPL(snd_soc_unregister_codec); | |
3774 | ||
bec4fa05 | 3775 | /* Retrieve a card's name from device tree */ |
b07609ce KM |
3776 | int snd_soc_of_parse_card_name(struct snd_soc_card *card, |
3777 | const char *propname) | |
bec4fa05 | 3778 | { |
b07609ce | 3779 | struct device_node *np; |
bec4fa05 SW |
3780 | int ret; |
3781 | ||
7e07e7c0 TB |
3782 | if (!card->dev) { |
3783 | pr_err("card->dev is not set before calling %s\n", __func__); | |
3784 | return -EINVAL; | |
3785 | } | |
3786 | ||
b07609ce | 3787 | np = card->dev->of_node; |
7e07e7c0 | 3788 | |
bec4fa05 SW |
3789 | ret = of_property_read_string_index(np, propname, 0, &card->name); |
3790 | /* | |
3791 | * EINVAL means the property does not exist. This is fine providing | |
3792 | * card->name was previously set, which is checked later in | |
3793 | * snd_soc_register_card. | |
3794 | */ | |
3795 | if (ret < 0 && ret != -EINVAL) { | |
3796 | dev_err(card->dev, | |
f110bfc7 | 3797 | "ASoC: Property '%s' could not be read: %d\n", |
bec4fa05 SW |
3798 | propname, ret); |
3799 | return ret; | |
3800 | } | |
3801 | ||
3802 | return 0; | |
3803 | } | |
b07609ce | 3804 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); |
bec4fa05 | 3805 | |
9a6d4860 XL |
3806 | static const struct snd_soc_dapm_widget simple_widgets[] = { |
3807 | SND_SOC_DAPM_MIC("Microphone", NULL), | |
3808 | SND_SOC_DAPM_LINE("Line", NULL), | |
3809 | SND_SOC_DAPM_HP("Headphone", NULL), | |
3810 | SND_SOC_DAPM_SPK("Speaker", NULL), | |
3811 | }; | |
3812 | ||
21efde50 | 3813 | int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, |
9a6d4860 XL |
3814 | const char *propname) |
3815 | { | |
21efde50 | 3816 | struct device_node *np = card->dev->of_node; |
9a6d4860 XL |
3817 | struct snd_soc_dapm_widget *widgets; |
3818 | const char *template, *wname; | |
3819 | int i, j, num_widgets, ret; | |
3820 | ||
3821 | num_widgets = of_property_count_strings(np, propname); | |
3822 | if (num_widgets < 0) { | |
3823 | dev_err(card->dev, | |
3824 | "ASoC: Property '%s' does not exist\n", propname); | |
3825 | return -EINVAL; | |
3826 | } | |
3827 | if (num_widgets & 1) { | |
3828 | dev_err(card->dev, | |
3829 | "ASoC: Property '%s' length is not even\n", propname); | |
3830 | return -EINVAL; | |
3831 | } | |
3832 | ||
3833 | num_widgets /= 2; | |
3834 | if (!num_widgets) { | |
3835 | dev_err(card->dev, "ASoC: Property '%s's length is zero\n", | |
3836 | propname); | |
3837 | return -EINVAL; | |
3838 | } | |
3839 | ||
3840 | widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), | |
3841 | GFP_KERNEL); | |
3842 | if (!widgets) { | |
3843 | dev_err(card->dev, | |
3844 | "ASoC: Could not allocate memory for widgets\n"); | |
3845 | return -ENOMEM; | |
3846 | } | |
3847 | ||
3848 | for (i = 0; i < num_widgets; i++) { | |
3849 | ret = of_property_read_string_index(np, propname, | |
3850 | 2 * i, &template); | |
3851 | if (ret) { | |
3852 | dev_err(card->dev, | |
3853 | "ASoC: Property '%s' index %d read error:%d\n", | |
3854 | propname, 2 * i, ret); | |
3855 | return -EINVAL; | |
3856 | } | |
3857 | ||
3858 | for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { | |
3859 | if (!strncmp(template, simple_widgets[j].name, | |
3860 | strlen(simple_widgets[j].name))) { | |
3861 | widgets[i] = simple_widgets[j]; | |
3862 | break; | |
3863 | } | |
3864 | } | |
3865 | ||
3866 | if (j >= ARRAY_SIZE(simple_widgets)) { | |
3867 | dev_err(card->dev, | |
3868 | "ASoC: DAPM widget '%s' is not supported\n", | |
3869 | template); | |
3870 | return -EINVAL; | |
3871 | } | |
3872 | ||
3873 | ret = of_property_read_string_index(np, propname, | |
3874 | (2 * i) + 1, | |
3875 | &wname); | |
3876 | if (ret) { | |
3877 | dev_err(card->dev, | |
3878 | "ASoC: Property '%s' index %d read error:%d\n", | |
3879 | propname, (2 * i) + 1, ret); | |
3880 | return -EINVAL; | |
3881 | } | |
3882 | ||
3883 | widgets[i].name = wname; | |
3884 | } | |
3885 | ||
f23e860e NC |
3886 | card->of_dapm_widgets = widgets; |
3887 | card->num_of_dapm_widgets = num_widgets; | |
9a6d4860 XL |
3888 | |
3889 | return 0; | |
3890 | } | |
21efde50 | 3891 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); |
9a6d4860 | 3892 | |
6131084a JS |
3893 | static int snd_soc_of_get_slot_mask(struct device_node *np, |
3894 | const char *prop_name, | |
3895 | unsigned int *mask) | |
3896 | { | |
3897 | u32 val; | |
6c84e591 | 3898 | const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); |
6131084a JS |
3899 | int i; |
3900 | ||
3901 | if (!of_slot_mask) | |
3902 | return 0; | |
3903 | val /= sizeof(u32); | |
3904 | for (i = 0; i < val; i++) | |
3905 | if (be32_to_cpup(&of_slot_mask[i])) | |
3906 | *mask |= (1 << i); | |
3907 | ||
3908 | return val; | |
3909 | } | |
3910 | ||
89c67857 | 3911 | int snd_soc_of_parse_tdm_slot(struct device_node *np, |
6131084a JS |
3912 | unsigned int *tx_mask, |
3913 | unsigned int *rx_mask, | |
89c67857 XL |
3914 | unsigned int *slots, |
3915 | unsigned int *slot_width) | |
3916 | { | |
3917 | u32 val; | |
3918 | int ret; | |
3919 | ||
6131084a JS |
3920 | if (tx_mask) |
3921 | snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); | |
3922 | if (rx_mask) | |
3923 | snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); | |
3924 | ||
89c67857 XL |
3925 | if (of_property_read_bool(np, "dai-tdm-slot-num")) { |
3926 | ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); | |
3927 | if (ret) | |
3928 | return ret; | |
3929 | ||
3930 | if (slots) | |
3931 | *slots = val; | |
3932 | } | |
3933 | ||
3934 | if (of_property_read_bool(np, "dai-tdm-slot-width")) { | |
3935 | ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); | |
3936 | if (ret) | |
3937 | return ret; | |
3938 | ||
3939 | if (slot_width) | |
3940 | *slot_width = val; | |
3941 | } | |
3942 | ||
3943 | return 0; | |
3944 | } | |
3945 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); | |
3946 | ||
440a3006 | 3947 | void snd_soc_of_parse_audio_prefix(struct snd_soc_card *card, |
5e3cdaa2 KM |
3948 | struct snd_soc_codec_conf *codec_conf, |
3949 | struct device_node *of_node, | |
3950 | const char *propname) | |
3951 | { | |
440a3006 | 3952 | struct device_node *np = card->dev->of_node; |
5e3cdaa2 KM |
3953 | const char *str; |
3954 | int ret; | |
3955 | ||
3956 | ret = of_property_read_string(np, propname, &str); | |
3957 | if (ret < 0) { | |
3958 | /* no prefix is not error */ | |
3959 | return; | |
3960 | } | |
3961 | ||
3962 | codec_conf->of_node = of_node; | |
3963 | codec_conf->name_prefix = str; | |
3964 | } | |
440a3006 | 3965 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_prefix); |
5e3cdaa2 | 3966 | |
2bc644af | 3967 | int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, |
a4a54dd5 SW |
3968 | const char *propname) |
3969 | { | |
2bc644af | 3970 | struct device_node *np = card->dev->of_node; |
e3b1e6a1 | 3971 | int num_routes; |
a4a54dd5 SW |
3972 | struct snd_soc_dapm_route *routes; |
3973 | int i, ret; | |
3974 | ||
3975 | num_routes = of_property_count_strings(np, propname); | |
c34ce320 | 3976 | if (num_routes < 0 || num_routes & 1) { |
10e8aa9a MM |
3977 | dev_err(card->dev, |
3978 | "ASoC: Property '%s' does not exist or its length is not even\n", | |
3979 | propname); | |
a4a54dd5 SW |
3980 | return -EINVAL; |
3981 | } | |
3982 | num_routes /= 2; | |
3983 | if (!num_routes) { | |
f110bfc7 | 3984 | dev_err(card->dev, "ASoC: Property '%s's length is zero\n", |
a4a54dd5 SW |
3985 | propname); |
3986 | return -EINVAL; | |
3987 | } | |
3988 | ||
e3b1e6a1 | 3989 | routes = devm_kzalloc(card->dev, num_routes * sizeof(*routes), |
a4a54dd5 SW |
3990 | GFP_KERNEL); |
3991 | if (!routes) { | |
3992 | dev_err(card->dev, | |
f110bfc7 | 3993 | "ASoC: Could not allocate DAPM route table\n"); |
a4a54dd5 SW |
3994 | return -EINVAL; |
3995 | } | |
3996 | ||
3997 | for (i = 0; i < num_routes; i++) { | |
3998 | ret = of_property_read_string_index(np, propname, | |
e3b1e6a1 | 3999 | 2 * i, &routes[i].sink); |
a4a54dd5 | 4000 | if (ret) { |
c871bd0b MB |
4001 | dev_err(card->dev, |
4002 | "ASoC: Property '%s' index %d could not be read: %d\n", | |
4003 | propname, 2 * i, ret); | |
a4a54dd5 SW |
4004 | return -EINVAL; |
4005 | } | |
4006 | ret = of_property_read_string_index(np, propname, | |
e3b1e6a1 | 4007 | (2 * i) + 1, &routes[i].source); |
a4a54dd5 SW |
4008 | if (ret) { |
4009 | dev_err(card->dev, | |
c871bd0b MB |
4010 | "ASoC: Property '%s' index %d could not be read: %d\n", |
4011 | propname, (2 * i) + 1, ret); | |
a4a54dd5 SW |
4012 | return -EINVAL; |
4013 | } | |
4014 | } | |
4015 | ||
f23e860e NC |
4016 | card->num_of_dapm_routes = num_routes; |
4017 | card->of_dapm_routes = routes; | |
a4a54dd5 SW |
4018 | |
4019 | return 0; | |
4020 | } | |
2bc644af | 4021 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); |
a4a54dd5 | 4022 | |
a7930ed4 | 4023 | unsigned int snd_soc_of_parse_daifmt(struct device_node *np, |
389cb834 JS |
4024 | const char *prefix, |
4025 | struct device_node **bitclkmaster, | |
4026 | struct device_node **framemaster) | |
a7930ed4 KM |
4027 | { |
4028 | int ret, i; | |
4029 | char prop[128]; | |
4030 | unsigned int format = 0; | |
4031 | int bit, frame; | |
4032 | const char *str; | |
4033 | struct { | |
4034 | char *name; | |
4035 | unsigned int val; | |
4036 | } of_fmt_table[] = { | |
4037 | { "i2s", SND_SOC_DAIFMT_I2S }, | |
4038 | { "right_j", SND_SOC_DAIFMT_RIGHT_J }, | |
4039 | { "left_j", SND_SOC_DAIFMT_LEFT_J }, | |
4040 | { "dsp_a", SND_SOC_DAIFMT_DSP_A }, | |
4041 | { "dsp_b", SND_SOC_DAIFMT_DSP_B }, | |
4042 | { "ac97", SND_SOC_DAIFMT_AC97 }, | |
4043 | { "pdm", SND_SOC_DAIFMT_PDM}, | |
4044 | { "msb", SND_SOC_DAIFMT_MSB }, | |
4045 | { "lsb", SND_SOC_DAIFMT_LSB }, | |
a7930ed4 KM |
4046 | }; |
4047 | ||
4048 | if (!prefix) | |
4049 | prefix = ""; | |
4050 | ||
4051 | /* | |
5711c979 KM |
4052 | * check "dai-format = xxx" |
4053 | * or "[prefix]format = xxx" | |
a7930ed4 KM |
4054 | * SND_SOC_DAIFMT_FORMAT_MASK area |
4055 | */ | |
5711c979 KM |
4056 | ret = of_property_read_string(np, "dai-format", &str); |
4057 | if (ret < 0) { | |
4058 | snprintf(prop, sizeof(prop), "%sformat", prefix); | |
4059 | ret = of_property_read_string(np, prop, &str); | |
4060 | } | |
a7930ed4 KM |
4061 | if (ret == 0) { |
4062 | for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { | |
4063 | if (strcmp(str, of_fmt_table[i].name) == 0) { | |
4064 | format |= of_fmt_table[i].val; | |
4065 | break; | |
4066 | } | |
4067 | } | |
4068 | } | |
4069 | ||
4070 | /* | |
8c2d6a9f | 4071 | * check "[prefix]continuous-clock" |
a7930ed4 KM |
4072 | * SND_SOC_DAIFMT_CLOCK_MASK area |
4073 | */ | |
8c2d6a9f | 4074 | snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); |
51930295 | 4075 | if (of_property_read_bool(np, prop)) |
8c2d6a9f KM |
4076 | format |= SND_SOC_DAIFMT_CONT; |
4077 | else | |
4078 | format |= SND_SOC_DAIFMT_GATED; | |
a7930ed4 KM |
4079 | |
4080 | /* | |
4081 | * check "[prefix]bitclock-inversion" | |
4082 | * check "[prefix]frame-inversion" | |
4083 | * SND_SOC_DAIFMT_INV_MASK area | |
4084 | */ | |
4085 | snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); | |
4086 | bit = !!of_get_property(np, prop, NULL); | |
4087 | ||
4088 | snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); | |
4089 | frame = !!of_get_property(np, prop, NULL); | |
4090 | ||
4091 | switch ((bit << 4) + frame) { | |
4092 | case 0x11: | |
4093 | format |= SND_SOC_DAIFMT_IB_IF; | |
4094 | break; | |
4095 | case 0x10: | |
4096 | format |= SND_SOC_DAIFMT_IB_NF; | |
4097 | break; | |
4098 | case 0x01: | |
4099 | format |= SND_SOC_DAIFMT_NB_IF; | |
4100 | break; | |
4101 | default: | |
4102 | /* SND_SOC_DAIFMT_NB_NF is default */ | |
4103 | break; | |
4104 | } | |
4105 | ||
4106 | /* | |
4107 | * check "[prefix]bitclock-master" | |
4108 | * check "[prefix]frame-master" | |
4109 | * SND_SOC_DAIFMT_MASTER_MASK area | |
4110 | */ | |
4111 | snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); | |
4112 | bit = !!of_get_property(np, prop, NULL); | |
389cb834 JS |
4113 | if (bit && bitclkmaster) |
4114 | *bitclkmaster = of_parse_phandle(np, prop, 0); | |
a7930ed4 KM |
4115 | |
4116 | snprintf(prop, sizeof(prop), "%sframe-master", prefix); | |
4117 | frame = !!of_get_property(np, prop, NULL); | |
389cb834 JS |
4118 | if (frame && framemaster) |
4119 | *framemaster = of_parse_phandle(np, prop, 0); | |
a7930ed4 KM |
4120 | |
4121 | switch ((bit << 4) + frame) { | |
4122 | case 0x11: | |
4123 | format |= SND_SOC_DAIFMT_CBM_CFM; | |
4124 | break; | |
4125 | case 0x10: | |
4126 | format |= SND_SOC_DAIFMT_CBM_CFS; | |
4127 | break; | |
4128 | case 0x01: | |
4129 | format |= SND_SOC_DAIFMT_CBS_CFM; | |
4130 | break; | |
4131 | default: | |
4132 | format |= SND_SOC_DAIFMT_CBS_CFS; | |
4133 | break; | |
4134 | } | |
4135 | ||
4136 | return format; | |
4137 | } | |
4138 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); | |
4139 | ||
a180e8b9 KM |
4140 | int snd_soc_get_dai_id(struct device_node *ep) |
4141 | { | |
4142 | struct snd_soc_component *pos; | |
4143 | struct device_node *node; | |
4144 | int ret; | |
4145 | ||
4146 | node = of_graph_get_port_parent(ep); | |
4147 | ||
4148 | /* | |
4149 | * For example HDMI case, HDMI has video/sound port, | |
4150 | * but ALSA SoC needs sound port number only. | |
4151 | * Thus counting HDMI DT port/endpoint doesn't work. | |
4152 | * Then, it should have .of_xlate_dai_id | |
4153 | */ | |
4154 | ret = -ENOTSUPP; | |
4155 | mutex_lock(&client_mutex); | |
4156 | list_for_each_entry(pos, &component_list, list) { | |
4157 | struct device_node *component_of_node = pos->dev->of_node; | |
4158 | ||
4159 | if (!component_of_node && pos->dev->parent) | |
4160 | component_of_node = pos->dev->parent->of_node; | |
4161 | ||
4162 | if (component_of_node != node) | |
4163 | continue; | |
4164 | ||
4165 | if (pos->driver->of_xlate_dai_id) | |
4166 | ret = pos->driver->of_xlate_dai_id(pos, ep); | |
4167 | ||
4168 | break; | |
4169 | } | |
4170 | mutex_unlock(&client_mutex); | |
4171 | ||
4172 | return ret; | |
4173 | } | |
4174 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); | |
4175 | ||
1ad8ec53 | 4176 | int snd_soc_get_dai_name(struct of_phandle_args *args, |
93b0f3ee | 4177 | const char **dai_name) |
cb470087 KM |
4178 | { |
4179 | struct snd_soc_component *pos; | |
3e0aa8d8 | 4180 | struct device_node *component_of_node; |
93b0f3ee | 4181 | int ret = -EPROBE_DEFER; |
cb470087 KM |
4182 | |
4183 | mutex_lock(&client_mutex); | |
4184 | list_for_each_entry(pos, &component_list, list) { | |
3e0aa8d8 JS |
4185 | component_of_node = pos->dev->of_node; |
4186 | if (!component_of_node && pos->dev->parent) | |
4187 | component_of_node = pos->dev->parent->of_node; | |
4188 | ||
4189 | if (component_of_node != args->np) | |
cb470087 KM |
4190 | continue; |
4191 | ||
6833c452 | 4192 | if (pos->driver->of_xlate_dai_name) { |
93b0f3ee JFM |
4193 | ret = pos->driver->of_xlate_dai_name(pos, |
4194 | args, | |
4195 | dai_name); | |
6833c452 KM |
4196 | } else { |
4197 | int id = -1; | |
4198 | ||
93b0f3ee | 4199 | switch (args->args_count) { |
6833c452 KM |
4200 | case 0: |
4201 | id = 0; /* same as dai_drv[0] */ | |
4202 | break; | |
4203 | case 1: | |
93b0f3ee | 4204 | id = args->args[0]; |
6833c452 KM |
4205 | break; |
4206 | default: | |
4207 | /* not supported */ | |
4208 | break; | |
4209 | } | |
4210 | ||
4211 | if (id < 0 || id >= pos->num_dai) { | |
4212 | ret = -EINVAL; | |
3dcba280 | 4213 | continue; |
6833c452 | 4214 | } |
e41975ed XL |
4215 | |
4216 | ret = 0; | |
4217 | ||
4218 | *dai_name = pos->dai_drv[id].name; | |
4219 | if (!*dai_name) | |
4220 | *dai_name = pos->name; | |
cb470087 KM |
4221 | } |
4222 | ||
cb470087 KM |
4223 | break; |
4224 | } | |
4225 | mutex_unlock(&client_mutex); | |
93b0f3ee JFM |
4226 | return ret; |
4227 | } | |
1ad8ec53 | 4228 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); |
93b0f3ee JFM |
4229 | |
4230 | int snd_soc_of_get_dai_name(struct device_node *of_node, | |
4231 | const char **dai_name) | |
4232 | { | |
4233 | struct of_phandle_args args; | |
4234 | int ret; | |
4235 | ||
4236 | ret = of_parse_phandle_with_args(of_node, "sound-dai", | |
4237 | "#sound-dai-cells", 0, &args); | |
4238 | if (ret) | |
4239 | return ret; | |
4240 | ||
4241 | ret = snd_soc_get_dai_name(&args, dai_name); | |
cb470087 KM |
4242 | |
4243 | of_node_put(args.np); | |
4244 | ||
4245 | return ret; | |
4246 | } | |
4247 | EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); | |
4248 | ||
93b0f3ee JFM |
4249 | /* |
4250 | * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree | |
4251 | * @dev: Card device | |
4252 | * @of_node: Device node | |
4253 | * @dai_link: DAI link | |
4254 | * | |
4255 | * Builds an array of CODEC DAI components from the DAI link property | |
4256 | * 'sound-dai'. | |
4257 | * The array is set in the DAI link and the number of DAIs is set accordingly. | |
4258 | * The device nodes in the array (of_node) must be dereferenced by the caller. | |
4259 | * | |
4260 | * Returns 0 for success | |
4261 | */ | |
4262 | int snd_soc_of_get_dai_link_codecs(struct device *dev, | |
4263 | struct device_node *of_node, | |
4264 | struct snd_soc_dai_link *dai_link) | |
4265 | { | |
4266 | struct of_phandle_args args; | |
4267 | struct snd_soc_dai_link_component *component; | |
4268 | char *name; | |
4269 | int index, num_codecs, ret; | |
4270 | ||
4271 | /* Count the number of CODECs */ | |
4272 | name = "sound-dai"; | |
4273 | num_codecs = of_count_phandle_with_args(of_node, name, | |
4274 | "#sound-dai-cells"); | |
4275 | if (num_codecs <= 0) { | |
4276 | if (num_codecs == -ENOENT) | |
4277 | dev_err(dev, "No 'sound-dai' property\n"); | |
4278 | else | |
4279 | dev_err(dev, "Bad phandle in 'sound-dai'\n"); | |
4280 | return num_codecs; | |
4281 | } | |
4282 | component = devm_kzalloc(dev, | |
4283 | sizeof *component * num_codecs, | |
4284 | GFP_KERNEL); | |
4285 | if (!component) | |
4286 | return -ENOMEM; | |
4287 | dai_link->codecs = component; | |
4288 | dai_link->num_codecs = num_codecs; | |
4289 | ||
4290 | /* Parse the list */ | |
4291 | for (index = 0, component = dai_link->codecs; | |
4292 | index < dai_link->num_codecs; | |
4293 | index++, component++) { | |
4294 | ret = of_parse_phandle_with_args(of_node, name, | |
4295 | "#sound-dai-cells", | |
4296 | index, &args); | |
4297 | if (ret) | |
4298 | goto err; | |
4299 | component->of_node = args.np; | |
4300 | ret = snd_soc_get_dai_name(&args, &component->dai_name); | |
4301 | if (ret < 0) | |
4302 | goto err; | |
4303 | } | |
4304 | return 0; | |
4305 | err: | |
4306 | for (index = 0, component = dai_link->codecs; | |
4307 | index < dai_link->num_codecs; | |
4308 | index++, component++) { | |
4309 | if (!component->of_node) | |
4310 | break; | |
4311 | of_node_put(component->of_node); | |
4312 | component->of_node = NULL; | |
4313 | } | |
4314 | dai_link->codecs = NULL; | |
4315 | dai_link->num_codecs = 0; | |
4316 | return ret; | |
4317 | } | |
4318 | EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); | |
4319 | ||
c9b3a40f | 4320 | static int __init snd_soc_init(void) |
db2a4165 | 4321 | { |
6553bf06 | 4322 | snd_soc_debugfs_init(); |
fb257897 MB |
4323 | snd_soc_util_init(); |
4324 | ||
db2a4165 FM |
4325 | return platform_driver_register(&soc_driver); |
4326 | } | |
4abe8e16 | 4327 | module_init(snd_soc_init); |
db2a4165 | 4328 | |
7d8c16a6 | 4329 | static void __exit snd_soc_exit(void) |
db2a4165 | 4330 | { |
fb257897 | 4331 | snd_soc_util_exit(); |
6553bf06 | 4332 | snd_soc_debugfs_exit(); |
fb257897 | 4333 | |
3ff3f64b | 4334 | platform_driver_unregister(&soc_driver); |
db2a4165 | 4335 | } |
db2a4165 FM |
4336 | module_exit(snd_soc_exit); |
4337 | ||
4338 | /* Module information */ | |
d331124d | 4339 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
db2a4165 FM |
4340 | MODULE_DESCRIPTION("ALSA SoC Core"); |
4341 | MODULE_LICENSE("GPL"); | |
8b45a209 | 4342 | MODULE_ALIAS("platform:soc-audio"); |