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