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