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