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