]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - sound/soc/soc-core.c
Merge branch 'for-2.6.36' into for-2.6.37
[mirror_ubuntu-artful-kernel.git] / sound / soc / soc-core.c
1 /*
2 * soc-core.c -- ALSA SoC Audio Layer
3 *
4 * Copyright 2005 Wolfson Microelectronics PLC.
5 * Copyright 2005 Openedhand Ltd.
6 * Copyright (C) 2010 Slimlogic Ltd.
7 * Copyright (C) 2010 Texas Instruments Inc.
8 *
9 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 * with code, comments and ideas from :-
11 * Richard Purdie <richard@openedhand.com>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
17 *
18 * TODO:
19 * o Add hw rules to enforce rates, etc.
20 * o More testing with other codecs/machines.
21 * o Add more codecs and platforms to ensure good API coverage.
22 * o Support TDM on PCM and I2S
23 */
24
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31 #include <linux/debugfs.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <sound/ac97_codec.h>
35 #include <sound/core.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/soc.h>
39 #include <sound/soc-dapm.h>
40 #include <sound/initval.h>
41
42 #define NAME_SIZE 32
43
44 static DEFINE_MUTEX(pcm_mutex);
45 static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
46
47 #ifdef CONFIG_DEBUG_FS
48 static struct dentry *debugfs_root;
49 #endif
50
51 static DEFINE_MUTEX(client_mutex);
52 static LIST_HEAD(card_list);
53 static LIST_HEAD(dai_list);
54 static LIST_HEAD(platform_list);
55 static LIST_HEAD(codec_list);
56
57 static int snd_soc_register_card(struct snd_soc_card *card);
58 static int snd_soc_unregister_card(struct snd_soc_card *card);
59 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num);
60
61 /*
62 * This is a timeout to do a DAPM powerdown after a stream is closed().
63 * It can be used to eliminate pops between different playback streams, e.g.
64 * between two audio tracks.
65 */
66 static int pmdown_time = 5000;
67 module_param(pmdown_time, int, 0);
68 MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
69
70 /*
71 * This function forces any delayed work to be queued and run.
72 */
73 static int run_delayed_work(struct delayed_work *dwork)
74 {
75 int ret;
76
77 /* cancel any work waiting to be queued. */
78 ret = cancel_delayed_work(dwork);
79
80 /* if there was any work waiting then we run it now and
81 * wait for it's completion */
82 if (ret) {
83 schedule_delayed_work(dwork, 0);
84 flush_scheduled_work();
85 }
86 return ret;
87 }
88
89 /* codec register dump */
90 static ssize_t soc_codec_reg_show(struct snd_soc_codec *codec, char *buf)
91 {
92 int ret, i, step = 1, count = 0;
93
94 if (!codec->driver->reg_cache_size)
95 return 0;
96
97 if (codec->driver->reg_cache_step)
98 step = codec->driver->reg_cache_step;
99
100 count += sprintf(buf, "%s registers\n", codec->name);
101 for (i = 0; i < codec->driver->reg_cache_size; i += step) {
102 if (codec->driver->readable_register && !codec->driver->readable_register(i))
103 continue;
104
105 count += sprintf(buf + count, "%2x: ", i);
106 if (count >= PAGE_SIZE - 1)
107 break;
108
109 if (codec->driver->display_register) {
110 count += codec->driver->display_register(codec, buf + count,
111 PAGE_SIZE - count, i);
112 } else {
113 /* If the read fails it's almost certainly due to
114 * the register being volatile and the device being
115 * powered off.
116 */
117 ret = codec->driver->read(codec, i);
118 if (ret >= 0)
119 count += snprintf(buf + count,
120 PAGE_SIZE - count,
121 "%4x", ret);
122 else
123 count += snprintf(buf + count,
124 PAGE_SIZE - count,
125 "<no data: %d>", ret);
126 }
127
128 if (count >= PAGE_SIZE - 1)
129 break;
130
131 count += snprintf(buf + count, PAGE_SIZE - count, "\n");
132 if (count >= PAGE_SIZE - 1)
133 break;
134 }
135
136 /* Truncate count; min() would cause a warning */
137 if (count >= PAGE_SIZE)
138 count = PAGE_SIZE - 1;
139
140 return count;
141 }
142 static ssize_t codec_reg_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144 {
145 struct snd_soc_pcm_runtime *rtd =
146 container_of(dev, struct snd_soc_pcm_runtime, dev);
147
148 return soc_codec_reg_show(rtd->codec, buf);
149 }
150
151 static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
152
153 static ssize_t pmdown_time_show(struct device *dev,
154 struct device_attribute *attr, char *buf)
155 {
156 struct snd_soc_pcm_runtime *rtd =
157 container_of(dev, struct snd_soc_pcm_runtime, dev);
158
159 return sprintf(buf, "%ld\n", rtd->pmdown_time);
160 }
161
162 static ssize_t pmdown_time_set(struct device *dev,
163 struct device_attribute *attr,
164 const char *buf, size_t count)
165 {
166 struct snd_soc_pcm_runtime *rtd =
167 container_of(dev, struct snd_soc_pcm_runtime, dev);
168
169 strict_strtol(buf, 10, &rtd->pmdown_time);
170
171 return count;
172 }
173
174 static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set);
175
176 #ifdef CONFIG_DEBUG_FS
177 static int codec_reg_open_file(struct inode *inode, struct file *file)
178 {
179 file->private_data = inode->i_private;
180 return 0;
181 }
182
183 static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
184 size_t count, loff_t *ppos)
185 {
186 ssize_t ret;
187 struct snd_soc_codec *codec = file->private_data;
188 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
189 if (!buf)
190 return -ENOMEM;
191 ret = soc_codec_reg_show(codec, buf);
192 if (ret >= 0)
193 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
194 kfree(buf);
195 return ret;
196 }
197
198 static ssize_t codec_reg_write_file(struct file *file,
199 const char __user *user_buf, size_t count, loff_t *ppos)
200 {
201 char buf[32];
202 int buf_size;
203 char *start = buf;
204 unsigned long reg, value;
205 int step = 1;
206 struct snd_soc_codec *codec = file->private_data;
207
208 buf_size = min(count, (sizeof(buf)-1));
209 if (copy_from_user(buf, user_buf, buf_size))
210 return -EFAULT;
211 buf[buf_size] = 0;
212
213 if (codec->driver->reg_cache_step)
214 step = codec->driver->reg_cache_step;
215
216 while (*start == ' ')
217 start++;
218 reg = simple_strtoul(start, &start, 16);
219 if ((reg >= codec->driver->reg_cache_size) || (reg % step))
220 return -EINVAL;
221 while (*start == ' ')
222 start++;
223 if (strict_strtoul(start, 16, &value))
224 return -EINVAL;
225 codec->driver->write(codec, reg, value);
226 return buf_size;
227 }
228
229 static const struct file_operations codec_reg_fops = {
230 .open = codec_reg_open_file,
231 .read = codec_reg_read_file,
232 .write = codec_reg_write_file,
233 };
234
235 static void soc_init_codec_debugfs(struct snd_soc_codec *codec)
236 {
237 codec->debugfs_codec_root = debugfs_create_dir(codec->name ,
238 debugfs_root);
239 if (!codec->debugfs_codec_root) {
240 printk(KERN_WARNING
241 "ASoC: Failed to create codec debugfs directory\n");
242 return;
243 }
244
245 codec->debugfs_reg = debugfs_create_file("codec_reg", 0644,
246 codec->debugfs_codec_root,
247 codec, &codec_reg_fops);
248 if (!codec->debugfs_reg)
249 printk(KERN_WARNING
250 "ASoC: Failed to create codec register debugfs file\n");
251
252 codec->debugfs_pop_time = debugfs_create_u32("dapm_pop_time", 0744,
253 codec->debugfs_codec_root,
254 &codec->pop_time);
255 if (!codec->debugfs_pop_time)
256 printk(KERN_WARNING
257 "Failed to create pop time debugfs file\n");
258
259 codec->debugfs_dapm = debugfs_create_dir("dapm",
260 codec->debugfs_codec_root);
261 if (!codec->debugfs_dapm)
262 printk(KERN_WARNING
263 "Failed to create DAPM debugfs directory\n");
264
265 snd_soc_dapm_debugfs_init(codec);
266 }
267
268 static void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
269 {
270 debugfs_remove_recursive(codec->debugfs_codec_root);
271 }
272
273 #else
274
275 static inline void soc_init_codec_debugfs(struct snd_soc_codec *codec)
276 {
277 }
278
279 static inline void soc_cleanup_codec_debugfs(struct snd_soc_codec *codec)
280 {
281 }
282 #endif
283
284 #ifdef CONFIG_SND_SOC_AC97_BUS
285 /* unregister ac97 codec */
286 static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
287 {
288 if (codec->ac97->dev.bus)
289 device_unregister(&codec->ac97->dev);
290 return 0;
291 }
292
293 /* stop no dev release warning */
294 static void soc_ac97_device_release(struct device *dev){}
295
296 /* register ac97 codec to bus */
297 static int soc_ac97_dev_register(struct snd_soc_codec *codec)
298 {
299 int err;
300
301 codec->ac97->dev.bus = &ac97_bus_type;
302 codec->ac97->dev.parent = codec->card->dev;
303 codec->ac97->dev.release = soc_ac97_device_release;
304
305 dev_set_name(&codec->ac97->dev, "%d-%d:%s",
306 codec->card->snd_card->number, 0, codec->name);
307 err = device_register(&codec->ac97->dev);
308 if (err < 0) {
309 snd_printk(KERN_ERR "Can't register ac97 bus\n");
310 codec->ac97->dev.bus = NULL;
311 return err;
312 }
313 return 0;
314 }
315 #endif
316
317 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream)
318 {
319 struct snd_soc_pcm_runtime *rtd = substream->private_data;
320 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
321 struct snd_soc_dai *codec_dai = rtd->codec_dai;
322 int ret;
323
324 if (codec_dai->driver->symmetric_rates || cpu_dai->driver->symmetric_rates ||
325 rtd->dai_link->symmetric_rates) {
326 dev_dbg(&rtd->dev, "Symmetry forces %dHz rate\n",
327 rtd->rate);
328
329 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
330 SNDRV_PCM_HW_PARAM_RATE,
331 rtd->rate,
332 rtd->rate);
333 if (ret < 0) {
334 dev_err(&rtd->dev,
335 "Unable to apply rate symmetry constraint: %d\n", ret);
336 return ret;
337 }
338 }
339
340 return 0;
341 }
342
343 /*
344 * Called by ALSA when a PCM substream is opened, the runtime->hw record is
345 * then initialized and any private data can be allocated. This also calls
346 * startup for the cpu DAI, platform, machine and codec DAI.
347 */
348 static int soc_pcm_open(struct snd_pcm_substream *substream)
349 {
350 struct snd_soc_pcm_runtime *rtd = substream->private_data;
351 struct snd_pcm_runtime *runtime = substream->runtime;
352 struct snd_soc_platform *platform = rtd->platform;
353 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
354 struct snd_soc_dai *codec_dai = rtd->codec_dai;
355 struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
356 struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
357 int ret = 0;
358
359 mutex_lock(&pcm_mutex);
360
361 /* startup the audio subsystem */
362 if (cpu_dai->driver->ops->startup) {
363 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
364 if (ret < 0) {
365 printk(KERN_ERR "asoc: can't open interface %s\n",
366 cpu_dai->name);
367 goto out;
368 }
369 }
370
371 if (platform->driver->ops->open) {
372 ret = platform->driver->ops->open(substream);
373 if (ret < 0) {
374 printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
375 goto platform_err;
376 }
377 }
378
379 if (codec_dai->driver->ops->startup) {
380 ret = codec_dai->driver->ops->startup(substream, codec_dai);
381 if (ret < 0) {
382 printk(KERN_ERR "asoc: can't open codec %s\n",
383 codec_dai->name);
384 goto codec_dai_err;
385 }
386 }
387
388 if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
389 ret = rtd->dai_link->ops->startup(substream);
390 if (ret < 0) {
391 printk(KERN_ERR "asoc: %s startup failed\n", rtd->dai_link->name);
392 goto machine_err;
393 }
394 }
395
396 /* Check that the codec and cpu DAI's are compatible */
397 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
398 runtime->hw.rate_min =
399 max(codec_dai_drv->playback.rate_min,
400 cpu_dai_drv->playback.rate_min);
401 runtime->hw.rate_max =
402 min(codec_dai_drv->playback.rate_max,
403 cpu_dai_drv->playback.rate_max);
404 runtime->hw.channels_min =
405 max(codec_dai_drv->playback.channels_min,
406 cpu_dai_drv->playback.channels_min);
407 runtime->hw.channels_max =
408 min(codec_dai_drv->playback.channels_max,
409 cpu_dai_drv->playback.channels_max);
410 runtime->hw.formats =
411 codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
412 runtime->hw.rates =
413 codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
414 if (codec_dai_drv->playback.rates
415 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
416 runtime->hw.rates |= cpu_dai_drv->playback.rates;
417 if (cpu_dai_drv->playback.rates
418 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
419 runtime->hw.rates |= codec_dai_drv->playback.rates;
420 } else {
421 runtime->hw.rate_min =
422 max(codec_dai_drv->capture.rate_min,
423 cpu_dai_drv->capture.rate_min);
424 runtime->hw.rate_max =
425 min(codec_dai_drv->capture.rate_max,
426 cpu_dai_drv->capture.rate_max);
427 runtime->hw.channels_min =
428 max(codec_dai_drv->capture.channels_min,
429 cpu_dai_drv->capture.channels_min);
430 runtime->hw.channels_max =
431 min(codec_dai_drv->capture.channels_max,
432 cpu_dai_drv->capture.channels_max);
433 runtime->hw.formats =
434 codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
435 runtime->hw.rates =
436 codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
437 if (codec_dai_drv->capture.rates
438 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
439 runtime->hw.rates |= cpu_dai_drv->capture.rates;
440 if (cpu_dai_drv->capture.rates
441 & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
442 runtime->hw.rates |= codec_dai_drv->capture.rates;
443 }
444
445 snd_pcm_limit_hw_rates(runtime);
446 if (!runtime->hw.rates) {
447 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
448 codec_dai->name, cpu_dai->name);
449 goto config_err;
450 }
451 if (!runtime->hw.formats) {
452 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
453 codec_dai->name, cpu_dai->name);
454 goto config_err;
455 }
456 if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
457 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
458 codec_dai->name, cpu_dai->name);
459 goto config_err;
460 }
461
462 /* Symmetry only applies if we've already got an active stream. */
463 if (cpu_dai->active || codec_dai->active) {
464 ret = soc_pcm_apply_symmetry(substream);
465 if (ret != 0)
466 goto config_err;
467 }
468
469 pr_debug("asoc: %s <-> %s info:\n",
470 codec_dai->name, cpu_dai->name);
471 pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
472 pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
473 runtime->hw.channels_max);
474 pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
475 runtime->hw.rate_max);
476
477 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
478 cpu_dai->playback_active++;
479 codec_dai->playback_active++;
480 } else {
481 cpu_dai->capture_active++;
482 codec_dai->capture_active++;
483 }
484 cpu_dai->active++;
485 codec_dai->active++;
486 rtd->codec->active++;
487 mutex_unlock(&pcm_mutex);
488 return 0;
489
490 config_err:
491 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
492 rtd->dai_link->ops->shutdown(substream);
493
494 machine_err:
495 if (codec_dai->driver->ops->shutdown)
496 codec_dai->driver->ops->shutdown(substream, codec_dai);
497
498 codec_dai_err:
499 if (platform->driver->ops->close)
500 platform->driver->ops->close(substream);
501
502 platform_err:
503 if (cpu_dai->driver->ops->shutdown)
504 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
505 out:
506 mutex_unlock(&pcm_mutex);
507 return ret;
508 }
509
510 /*
511 * Power down the audio subsystem pmdown_time msecs after close is called.
512 * This is to ensure there are no pops or clicks in between any music tracks
513 * due to DAPM power cycling.
514 */
515 static void close_delayed_work(struct work_struct *work)
516 {
517 struct snd_soc_pcm_runtime *rtd =
518 container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
519 struct snd_soc_dai *codec_dai = rtd->codec_dai;
520
521 mutex_lock(&pcm_mutex);
522
523 pr_debug("pop wq checking: %s status: %s waiting: %s\n",
524 codec_dai->driver->playback.stream_name,
525 codec_dai->playback_active ? "active" : "inactive",
526 codec_dai->pop_wait ? "yes" : "no");
527
528 /* are we waiting on this codec DAI stream */
529 if (codec_dai->pop_wait == 1) {
530 codec_dai->pop_wait = 0;
531 snd_soc_dapm_stream_event(rtd,
532 codec_dai->driver->playback.stream_name,
533 SND_SOC_DAPM_STREAM_STOP);
534 }
535
536 mutex_unlock(&pcm_mutex);
537 }
538
539 /*
540 * Called by ALSA when a PCM substream is closed. Private data can be
541 * freed here. The cpu DAI, codec DAI, machine and platform are also
542 * shutdown.
543 */
544 static int soc_codec_close(struct snd_pcm_substream *substream)
545 {
546 struct snd_soc_pcm_runtime *rtd = substream->private_data;
547 struct snd_soc_platform *platform = rtd->platform;
548 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
549 struct snd_soc_dai *codec_dai = rtd->codec_dai;
550 struct snd_soc_codec *codec = rtd->codec;
551
552 mutex_lock(&pcm_mutex);
553
554 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
555 cpu_dai->playback_active--;
556 codec_dai->playback_active--;
557 } else {
558 cpu_dai->capture_active--;
559 codec_dai->capture_active--;
560 }
561
562 cpu_dai->active--;
563 codec_dai->active--;
564 codec->active--;
565
566 /* Muting the DAC suppresses artifacts caused during digital
567 * shutdown, for example from stopping clocks.
568 */
569 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
570 snd_soc_dai_digital_mute(codec_dai, 1);
571
572 if (cpu_dai->driver->ops->shutdown)
573 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
574
575 if (codec_dai->driver->ops->shutdown)
576 codec_dai->driver->ops->shutdown(substream, codec_dai);
577
578 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
579 rtd->dai_link->ops->shutdown(substream);
580
581 if (platform->driver->ops->close)
582 platform->driver->ops->close(substream);
583 cpu_dai->runtime = NULL;
584
585 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
586 /* start delayed pop wq here for playback streams */
587 codec_dai->pop_wait = 1;
588 schedule_delayed_work(&rtd->delayed_work,
589 msecs_to_jiffies(rtd->pmdown_time));
590 } else {
591 /* capture streams can be powered down now */
592 snd_soc_dapm_stream_event(rtd,
593 codec_dai->driver->capture.stream_name,
594 SND_SOC_DAPM_STREAM_STOP);
595 }
596
597 mutex_unlock(&pcm_mutex);
598 return 0;
599 }
600
601 /*
602 * Called by ALSA when the PCM substream is prepared, can set format, sample
603 * rate, etc. This function is non atomic and can be called multiple times,
604 * it can refer to the runtime info.
605 */
606 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
607 {
608 struct snd_soc_pcm_runtime *rtd = substream->private_data;
609 struct snd_soc_platform *platform = rtd->platform;
610 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
611 struct snd_soc_dai *codec_dai = rtd->codec_dai;
612 int ret = 0;
613
614 mutex_lock(&pcm_mutex);
615
616 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
617 ret = rtd->dai_link->ops->prepare(substream);
618 if (ret < 0) {
619 printk(KERN_ERR "asoc: machine prepare error\n");
620 goto out;
621 }
622 }
623
624 if (platform->driver->ops->prepare) {
625 ret = platform->driver->ops->prepare(substream);
626 if (ret < 0) {
627 printk(KERN_ERR "asoc: platform prepare error\n");
628 goto out;
629 }
630 }
631
632 if (codec_dai->driver->ops->prepare) {
633 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
634 if (ret < 0) {
635 printk(KERN_ERR "asoc: codec DAI prepare error\n");
636 goto out;
637 }
638 }
639
640 if (cpu_dai->driver->ops->prepare) {
641 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
642 if (ret < 0) {
643 printk(KERN_ERR "asoc: cpu DAI prepare error\n");
644 goto out;
645 }
646 }
647
648 /* cancel any delayed stream shutdown that is pending */
649 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
650 codec_dai->pop_wait) {
651 codec_dai->pop_wait = 0;
652 cancel_delayed_work(&rtd->delayed_work);
653 }
654
655 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
656 snd_soc_dapm_stream_event(rtd,
657 codec_dai->driver->playback.stream_name,
658 SND_SOC_DAPM_STREAM_START);
659 else
660 snd_soc_dapm_stream_event(rtd,
661 codec_dai->driver->capture.stream_name,
662 SND_SOC_DAPM_STREAM_START);
663
664 snd_soc_dai_digital_mute(codec_dai, 0);
665
666 out:
667 mutex_unlock(&pcm_mutex);
668 return ret;
669 }
670
671 /*
672 * Called by ALSA when the hardware params are set by application. This
673 * function can also be called multiple times and can allocate buffers
674 * (using snd_pcm_lib_* ). It's non-atomic.
675 */
676 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
677 struct snd_pcm_hw_params *params)
678 {
679 struct snd_soc_pcm_runtime *rtd = substream->private_data;
680 struct snd_soc_platform *platform = rtd->platform;
681 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
682 struct snd_soc_dai *codec_dai = rtd->codec_dai;
683 int ret = 0;
684
685 mutex_lock(&pcm_mutex);
686
687 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
688 ret = rtd->dai_link->ops->hw_params(substream, params);
689 if (ret < 0) {
690 printk(KERN_ERR "asoc: machine hw_params failed\n");
691 goto out;
692 }
693 }
694
695 if (codec_dai->driver->ops->hw_params) {
696 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
697 if (ret < 0) {
698 printk(KERN_ERR "asoc: can't set codec %s hw params\n",
699 codec_dai->name);
700 goto codec_err;
701 }
702 }
703
704 if (cpu_dai->driver->ops->hw_params) {
705 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
706 if (ret < 0) {
707 printk(KERN_ERR "asoc: interface %s hw params failed\n",
708 cpu_dai->name);
709 goto interface_err;
710 }
711 }
712
713 if (platform->driver->ops->hw_params) {
714 ret = platform->driver->ops->hw_params(substream, params);
715 if (ret < 0) {
716 printk(KERN_ERR "asoc: platform %s hw params failed\n",
717 platform->name);
718 goto platform_err;
719 }
720 }
721
722 rtd->rate = params_rate(params);
723
724 out:
725 mutex_unlock(&pcm_mutex);
726 return ret;
727
728 platform_err:
729 if (cpu_dai->driver->ops->hw_free)
730 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
731
732 interface_err:
733 if (codec_dai->driver->ops->hw_free)
734 codec_dai->driver->ops->hw_free(substream, codec_dai);
735
736 codec_err:
737 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
738 rtd->dai_link->ops->hw_free(substream);
739
740 mutex_unlock(&pcm_mutex);
741 return ret;
742 }
743
744 /*
745 * Free's resources allocated by hw_params, can be called multiple times
746 */
747 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
748 {
749 struct snd_soc_pcm_runtime *rtd = substream->private_data;
750 struct snd_soc_platform *platform = rtd->platform;
751 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
752 struct snd_soc_dai *codec_dai = rtd->codec_dai;
753 struct snd_soc_codec *codec = rtd->codec;
754
755 mutex_lock(&pcm_mutex);
756
757 /* apply codec digital mute */
758 if (!codec->active)
759 snd_soc_dai_digital_mute(codec_dai, 1);
760
761 /* free any machine hw params */
762 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
763 rtd->dai_link->ops->hw_free(substream);
764
765 /* free any DMA resources */
766 if (platform->driver->ops->hw_free)
767 platform->driver->ops->hw_free(substream);
768
769 /* now free hw params for the DAI's */
770 if (codec_dai->driver->ops->hw_free)
771 codec_dai->driver->ops->hw_free(substream, codec_dai);
772
773 if (cpu_dai->driver->ops->hw_free)
774 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
775
776 mutex_unlock(&pcm_mutex);
777 return 0;
778 }
779
780 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
781 {
782 struct snd_soc_pcm_runtime *rtd = substream->private_data;
783 struct snd_soc_platform *platform = rtd->platform;
784 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
785 struct snd_soc_dai *codec_dai = rtd->codec_dai;
786 int ret;
787
788 if (codec_dai->driver->ops->trigger) {
789 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
790 if (ret < 0)
791 return ret;
792 }
793
794 if (platform->driver->ops->trigger) {
795 ret = platform->driver->ops->trigger(substream, cmd);
796 if (ret < 0)
797 return ret;
798 }
799
800 if (cpu_dai->driver->ops->trigger) {
801 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
802 if (ret < 0)
803 return ret;
804 }
805 return 0;
806 }
807
808 /*
809 * soc level wrapper for pointer callback
810 * If cpu_dai, codec_dai, platform driver has the delay callback, than
811 * the runtime->delay will be updated accordingly.
812 */
813 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
814 {
815 struct snd_soc_pcm_runtime *rtd = substream->private_data;
816 struct snd_soc_platform *platform = rtd->platform;
817 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
818 struct snd_soc_dai *codec_dai = rtd->codec_dai;
819 struct snd_pcm_runtime *runtime = substream->runtime;
820 snd_pcm_uframes_t offset = 0;
821 snd_pcm_sframes_t delay = 0;
822
823 if (platform->driver->ops->pointer)
824 offset = platform->driver->ops->pointer(substream);
825
826 if (cpu_dai->driver->ops->delay)
827 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
828
829 if (codec_dai->driver->ops->delay)
830 delay += codec_dai->driver->ops->delay(substream, codec_dai);
831
832 if (platform->driver->delay)
833 delay += platform->driver->delay(substream, codec_dai);
834
835 runtime->delay = delay;
836
837 return offset;
838 }
839
840 /* ASoC PCM operations */
841 static struct snd_pcm_ops soc_pcm_ops = {
842 .open = soc_pcm_open,
843 .close = soc_codec_close,
844 .hw_params = soc_pcm_hw_params,
845 .hw_free = soc_pcm_hw_free,
846 .prepare = soc_pcm_prepare,
847 .trigger = soc_pcm_trigger,
848 .pointer = soc_pcm_pointer,
849 };
850
851 #ifdef CONFIG_PM
852 /* powers down audio subsystem for suspend */
853 static int soc_suspend(struct device *dev)
854 {
855 struct platform_device *pdev = to_platform_device(dev);
856 struct snd_soc_card *card = platform_get_drvdata(pdev);
857 int i;
858
859 /* If the initialization of this soc device failed, there is no codec
860 * associated with it. Just bail out in this case.
861 */
862 if (list_empty(&card->codec_dev_list))
863 return 0;
864
865 /* Due to the resume being scheduled into a workqueue we could
866 * suspend before that's finished - wait for it to complete.
867 */
868 snd_power_lock(card->snd_card);
869 snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0);
870 snd_power_unlock(card->snd_card);
871
872 /* we're going to block userspace touching us until resume completes */
873 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot);
874
875 /* mute any active DAC's */
876 for (i = 0; i < card->num_rtd; i++) {
877 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
878 struct snd_soc_dai_driver *drv = dai->driver;
879
880 if (card->rtd[i].dai_link->ignore_suspend)
881 continue;
882
883 if (drv->ops->digital_mute && dai->playback_active)
884 drv->ops->digital_mute(dai, 1);
885 }
886
887 /* suspend all pcms */
888 for (i = 0; i < card->num_rtd; i++) {
889 if (card->rtd[i].dai_link->ignore_suspend)
890 continue;
891
892 snd_pcm_suspend_all(card->rtd[i].pcm);
893 }
894
895 if (card->suspend_pre)
896 card->suspend_pre(pdev, PMSG_SUSPEND);
897
898 for (i = 0; i < card->num_rtd; i++) {
899 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
900 struct snd_soc_platform *platform = card->rtd[i].platform;
901
902 if (card->rtd[i].dai_link->ignore_suspend)
903 continue;
904
905 if (cpu_dai->driver->suspend && !cpu_dai->driver->ac97_control)
906 cpu_dai->driver->suspend(cpu_dai);
907 if (platform->driver->suspend && !platform->suspended) {
908 platform->driver->suspend(cpu_dai);
909 platform->suspended = 1;
910 }
911 }
912
913 /* close any waiting streams and save state */
914 for (i = 0; i < card->num_rtd; i++) {
915 run_delayed_work(&card->rtd[i].delayed_work);
916 card->rtd[i].codec->suspend_bias_level = card->rtd[i].codec->bias_level;
917 }
918
919 for (i = 0; i < card->num_rtd; i++) {
920 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
921
922 if (card->rtd[i].dai_link->ignore_suspend)
923 continue;
924
925 if (driver->playback.stream_name != NULL)
926 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
927 SND_SOC_DAPM_STREAM_SUSPEND);
928
929 if (driver->capture.stream_name != NULL)
930 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
931 SND_SOC_DAPM_STREAM_SUSPEND);
932 }
933
934 /* suspend all CODECs */
935 for (i = 0; i < card->num_rtd; i++) {
936 struct snd_soc_codec *codec = card->rtd[i].codec;
937 /* If there are paths active then the CODEC will be held with
938 * bias _ON and should not be suspended. */
939 if (!codec->suspended && codec->driver->suspend) {
940 switch (codec->bias_level) {
941 case SND_SOC_BIAS_STANDBY:
942 case SND_SOC_BIAS_OFF:
943 codec->driver->suspend(codec, PMSG_SUSPEND);
944 codec->suspended = 1;
945 break;
946 default:
947 dev_dbg(codec->dev, "CODEC is on over suspend\n");
948 break;
949 }
950 }
951 }
952
953 for (i = 0; i < card->num_rtd; i++) {
954 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
955
956 if (card->rtd[i].dai_link->ignore_suspend)
957 continue;
958
959 if (cpu_dai->driver->suspend && cpu_dai->driver->ac97_control)
960 cpu_dai->driver->suspend(cpu_dai);
961 }
962
963 if (card->suspend_post)
964 card->suspend_post(pdev, PMSG_SUSPEND);
965
966 return 0;
967 }
968
969 /* deferred resume work, so resume can complete before we finished
970 * setting our codec back up, which can be very slow on I2C
971 */
972 static void soc_resume_deferred(struct work_struct *work)
973 {
974 struct snd_soc_card *card =
975 container_of(work, struct snd_soc_card, deferred_resume_work);
976 struct platform_device *pdev = to_platform_device(card->dev);
977 int i;
978
979 /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
980 * so userspace apps are blocked from touching us
981 */
982
983 dev_dbg(card->dev, "starting resume work\n");
984
985 /* Bring us up into D2 so that DAPM starts enabling things */
986 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2);
987
988 if (card->resume_pre)
989 card->resume_pre(pdev);
990
991 /* resume AC97 DAIs */
992 for (i = 0; i < card->num_rtd; i++) {
993 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
994
995 if (card->rtd[i].dai_link->ignore_suspend)
996 continue;
997
998 if (cpu_dai->driver->resume && cpu_dai->driver->ac97_control)
999 cpu_dai->driver->resume(cpu_dai);
1000 }
1001
1002 for (i = 0; i < card->num_rtd; i++) {
1003 struct snd_soc_codec *codec = card->rtd[i].codec;
1004 /* If the CODEC was idle over suspend then it will have been
1005 * left with bias OFF or STANDBY and suspended so we must now
1006 * resume. Otherwise the suspend was suppressed.
1007 */
1008 if (codec->driver->resume && codec->suspended) {
1009 switch (codec->bias_level) {
1010 case SND_SOC_BIAS_STANDBY:
1011 case SND_SOC_BIAS_OFF:
1012 codec->driver->resume(codec);
1013 codec->suspended = 0;
1014 break;
1015 default:
1016 dev_dbg(codec->dev, "CODEC was on over suspend\n");
1017 break;
1018 }
1019 }
1020 }
1021
1022 for (i = 0; i < card->num_rtd; i++) {
1023 struct snd_soc_dai_driver *driver = card->rtd[i].codec_dai->driver;
1024
1025 if (card->rtd[i].dai_link->ignore_suspend)
1026 continue;
1027
1028 if (driver->playback.stream_name != NULL)
1029 snd_soc_dapm_stream_event(&card->rtd[i], driver->playback.stream_name,
1030 SND_SOC_DAPM_STREAM_RESUME);
1031
1032 if (driver->capture.stream_name != NULL)
1033 snd_soc_dapm_stream_event(&card->rtd[i], driver->capture.stream_name,
1034 SND_SOC_DAPM_STREAM_RESUME);
1035 }
1036
1037 /* unmute any active DACs */
1038 for (i = 0; i < card->num_rtd; i++) {
1039 struct snd_soc_dai *dai = card->rtd[i].codec_dai;
1040 struct snd_soc_dai_driver *drv = dai->driver;
1041
1042 if (card->rtd[i].dai_link->ignore_suspend)
1043 continue;
1044
1045 if (drv->ops->digital_mute && dai->playback_active)
1046 drv->ops->digital_mute(dai, 0);
1047 }
1048
1049 for (i = 0; i < card->num_rtd; i++) {
1050 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1051 struct snd_soc_platform *platform = card->rtd[i].platform;
1052
1053 if (card->rtd[i].dai_link->ignore_suspend)
1054 continue;
1055
1056 if (cpu_dai->driver->resume && !cpu_dai->driver->ac97_control)
1057 cpu_dai->driver->resume(cpu_dai);
1058 if (platform->driver->resume && platform->suspended) {
1059 platform->driver->resume(cpu_dai);
1060 platform->suspended = 0;
1061 }
1062 }
1063
1064 if (card->resume_post)
1065 card->resume_post(pdev);
1066
1067 dev_dbg(card->dev, "resume work completed\n");
1068
1069 /* userspace can access us now we are back as we were before */
1070 snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0);
1071 }
1072
1073 /* powers up audio subsystem after a suspend */
1074 static int soc_resume(struct device *dev)
1075 {
1076 struct platform_device *pdev = to_platform_device(dev);
1077 struct snd_soc_card *card = platform_get_drvdata(pdev);
1078 int i;
1079
1080 /* AC97 devices might have other drivers hanging off them so
1081 * need to resume immediately. Other drivers don't have that
1082 * problem and may take a substantial amount of time to resume
1083 * due to I/O costs and anti-pop so handle them out of line.
1084 */
1085 for (i = 0; i < card->num_rtd; i++) {
1086 struct snd_soc_dai *cpu_dai = card->rtd[i].cpu_dai;
1087 if (cpu_dai->driver->ac97_control) {
1088 dev_dbg(dev, "Resuming AC97 immediately\n");
1089 soc_resume_deferred(&card->deferred_resume_work);
1090 } else {
1091 dev_dbg(dev, "Scheduling resume work\n");
1092 if (!schedule_work(&card->deferred_resume_work))
1093 dev_err(dev, "resume work item may be lost\n");
1094 }
1095 }
1096
1097 return 0;
1098 }
1099 #else
1100 #define soc_suspend NULL
1101 #define soc_resume NULL
1102 #endif
1103
1104 static struct snd_soc_dai_ops null_dai_ops = {
1105 };
1106
1107 static int soc_bind_dai_link(struct snd_soc_card *card, int num)
1108 {
1109 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1110 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1111 struct snd_soc_codec *codec;
1112 struct snd_soc_platform *platform;
1113 struct snd_soc_dai *codec_dai, *cpu_dai;
1114
1115 if (rtd->complete)
1116 return 1;
1117 dev_dbg(card->dev, "binding %s at idx %d\n", dai_link->name, num);
1118
1119 /* do we already have the CPU DAI for this link ? */
1120 if (rtd->cpu_dai) {
1121 goto find_codec;
1122 }
1123 /* no, then find CPU DAI from registered DAIs*/
1124 list_for_each_entry(cpu_dai, &dai_list, list) {
1125 if (!strcmp(cpu_dai->name, dai_link->cpu_dai_name)) {
1126
1127 if (!try_module_get(cpu_dai->dev->driver->owner))
1128 return -ENODEV;
1129
1130 rtd->cpu_dai = cpu_dai;
1131 goto find_codec;
1132 }
1133 }
1134 dev_dbg(card->dev, "CPU DAI %s not registered\n",
1135 dai_link->cpu_dai_name);
1136
1137 find_codec:
1138 /* do we already have the CODEC for this link ? */
1139 if (rtd->codec) {
1140 goto find_platform;
1141 }
1142
1143 /* no, then find CODEC from registered CODECs*/
1144 list_for_each_entry(codec, &codec_list, list) {
1145 if (!strcmp(codec->name, dai_link->codec_name)) {
1146 rtd->codec = codec;
1147
1148 if (!try_module_get(codec->dev->driver->owner))
1149 return -ENODEV;
1150
1151 /* CODEC found, so find CODEC DAI from registered DAIs from this CODEC*/
1152 list_for_each_entry(codec_dai, &dai_list, list) {
1153 if (codec->dev == codec_dai->dev &&
1154 !strcmp(codec_dai->name, dai_link->codec_dai_name)) {
1155 rtd->codec_dai = codec_dai;
1156 goto find_platform;
1157 }
1158 }
1159 dev_dbg(card->dev, "CODEC DAI %s not registered\n",
1160 dai_link->codec_dai_name);
1161
1162 goto find_platform;
1163 }
1164 }
1165 dev_dbg(card->dev, "CODEC %s not registered\n",
1166 dai_link->codec_name);
1167
1168 find_platform:
1169 /* do we already have the CODEC DAI for this link ? */
1170 if (rtd->platform) {
1171 goto out;
1172 }
1173 /* no, then find CPU DAI from registered DAIs*/
1174 list_for_each_entry(platform, &platform_list, list) {
1175 if (!strcmp(platform->name, dai_link->platform_name)) {
1176
1177 if (!try_module_get(platform->dev->driver->owner))
1178 return -ENODEV;
1179
1180 rtd->platform = platform;
1181 goto out;
1182 }
1183 }
1184
1185 dev_dbg(card->dev, "platform %s not registered\n",
1186 dai_link->platform_name);
1187 return 0;
1188
1189 out:
1190 /* mark rtd as complete if we found all 4 of our client devices */
1191 if (rtd->codec && rtd->codec_dai && rtd->platform && rtd->cpu_dai) {
1192 rtd->complete = 1;
1193 card->num_rtd++;
1194 }
1195 return 1;
1196 }
1197
1198 static void soc_remove_dai_link(struct snd_soc_card *card, int num)
1199 {
1200 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1201 struct snd_soc_codec *codec = rtd->codec;
1202 struct snd_soc_platform *platform = rtd->platform;
1203 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1204 int err;
1205
1206 /* unregister the rtd device */
1207 if (rtd->dev_registered) {
1208 device_remove_file(&rtd->dev, &dev_attr_pmdown_time);
1209 device_unregister(&rtd->dev);
1210 rtd->dev_registered = 0;
1211 }
1212
1213 /* remove the CODEC DAI */
1214 if (codec_dai && codec_dai->probed) {
1215 if (codec_dai->driver->remove) {
1216 err = codec_dai->driver->remove(codec_dai);
1217 if (err < 0)
1218 printk(KERN_ERR "asoc: failed to remove %s\n", codec_dai->name);
1219 }
1220 codec_dai->probed = 0;
1221 list_del(&codec_dai->card_list);
1222 }
1223
1224 /* remove the platform */
1225 if (platform && platform->probed) {
1226 if (platform->driver->remove) {
1227 err = platform->driver->remove(platform);
1228 if (err < 0)
1229 printk(KERN_ERR "asoc: failed to remove %s\n", platform->name);
1230 }
1231 platform->probed = 0;
1232 list_del(&platform->card_list);
1233 module_put(platform->dev->driver->owner);
1234 }
1235
1236 /* remove the CODEC */
1237 if (codec && codec->probed) {
1238 if (codec->driver->remove) {
1239 err = codec->driver->remove(codec);
1240 if (err < 0)
1241 printk(KERN_ERR "asoc: failed to remove %s\n", codec->name);
1242 }
1243
1244 /* Make sure all DAPM widgets are freed */
1245 snd_soc_dapm_free(codec);
1246
1247 soc_cleanup_codec_debugfs(codec);
1248 device_remove_file(&rtd->dev, &dev_attr_codec_reg);
1249 codec->probed = 0;
1250 list_del(&codec->card_list);
1251 module_put(codec->dev->driver->owner);
1252 }
1253
1254 /* remove the cpu_dai */
1255 if (cpu_dai && cpu_dai->probed) {
1256 if (cpu_dai->driver->remove) {
1257 err = cpu_dai->driver->remove(cpu_dai);
1258 if (err < 0)
1259 printk(KERN_ERR "asoc: failed to remove %s\n", cpu_dai->name);
1260 }
1261 cpu_dai->probed = 0;
1262 list_del(&cpu_dai->card_list);
1263 module_put(cpu_dai->dev->driver->owner);
1264 }
1265 }
1266
1267 static void rtd_release(struct device *dev) {}
1268
1269 static int soc_probe_dai_link(struct snd_soc_card *card, int num)
1270 {
1271 struct snd_soc_dai_link *dai_link = &card->dai_link[num];
1272 struct snd_soc_pcm_runtime *rtd = &card->rtd[num];
1273 struct snd_soc_codec *codec = rtd->codec;
1274 struct snd_soc_platform *platform = rtd->platform;
1275 struct snd_soc_dai *codec_dai = rtd->codec_dai, *cpu_dai = rtd->cpu_dai;
1276 int ret;
1277
1278 dev_dbg(card->dev, "probe %s dai link %d\n", card->name, num);
1279
1280 /* config components */
1281 codec_dai->codec = codec;
1282 codec->card = card;
1283 cpu_dai->platform = platform;
1284 rtd->card = card;
1285 rtd->dev.parent = card->dev;
1286 codec_dai->card = card;
1287 cpu_dai->card = card;
1288
1289 /* set default power off timeout */
1290 rtd->pmdown_time = pmdown_time;
1291
1292 /* probe the cpu_dai */
1293 if (!cpu_dai->probed) {
1294 if (cpu_dai->driver->probe) {
1295 ret = cpu_dai->driver->probe(cpu_dai);
1296 if (ret < 0) {
1297 printk(KERN_ERR "asoc: failed to probe CPU DAI %s\n",
1298 cpu_dai->name);
1299 return ret;
1300 }
1301 }
1302 cpu_dai->probed = 1;
1303 /* mark cpu_dai as probed and add to card cpu_dai list */
1304 list_add(&cpu_dai->card_list, &card->dai_dev_list);
1305 }
1306
1307 /* probe the CODEC */
1308 if (!codec->probed) {
1309 if (codec->driver->probe) {
1310 ret = codec->driver->probe(codec);
1311 if (ret < 0) {
1312 printk(KERN_ERR "asoc: failed to probe CODEC %s\n",
1313 codec->name);
1314 return ret;
1315 }
1316 }
1317
1318 soc_init_codec_debugfs(codec);
1319
1320 /* mark codec as probed and add to card codec list */
1321 codec->probed = 1;
1322 list_add(&codec->card_list, &card->codec_dev_list);
1323 }
1324
1325 /* probe the platform */
1326 if (!platform->probed) {
1327 if (platform->driver->probe) {
1328 ret = platform->driver->probe(platform);
1329 if (ret < 0) {
1330 printk(KERN_ERR "asoc: failed to probe platform %s\n",
1331 platform->name);
1332 return ret;
1333 }
1334 }
1335 /* mark platform as probed and add to card platform list */
1336 platform->probed = 1;
1337 list_add(&platform->card_list, &card->platform_dev_list);
1338 }
1339
1340 /* probe the CODEC DAI */
1341 if (!codec_dai->probed) {
1342 if (codec_dai->driver->probe) {
1343 ret = codec_dai->driver->probe(codec_dai);
1344 if (ret < 0) {
1345 printk(KERN_ERR "asoc: failed to probe CODEC DAI %s\n",
1346 codec_dai->name);
1347 return ret;
1348 }
1349 }
1350
1351 /* mark cpu_dai as probed and add to card cpu_dai list */
1352 codec_dai->probed = 1;
1353 list_add(&codec_dai->card_list, &card->dai_dev_list);
1354 }
1355
1356 /* DAPM dai link stream work */
1357 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
1358
1359 /* now that all clients have probed, initialise the DAI link */
1360 if (dai_link->init) {
1361 ret = dai_link->init(rtd);
1362 if (ret < 0) {
1363 printk(KERN_ERR "asoc: failed to init %s\n", dai_link->stream_name);
1364 return ret;
1365 }
1366 }
1367
1368 /* Make sure all DAPM widgets are instantiated */
1369 snd_soc_dapm_new_widgets(codec);
1370 snd_soc_dapm_sync(codec);
1371
1372 /* register the rtd device */
1373 rtd->dev.init_name = rtd->dai_link->stream_name;
1374 rtd->dev.release = rtd_release;
1375 rtd->dev.init_name = dai_link->name;
1376 ret = device_register(&rtd->dev);
1377 if (ret < 0) {
1378 printk(KERN_ERR "asoc: failed to register DAI runtime device %d\n", ret);
1379 return ret;
1380 }
1381
1382 rtd->dev_registered = 1;
1383 ret = device_create_file(&rtd->dev, &dev_attr_pmdown_time);
1384 if (ret < 0)
1385 printk(KERN_WARNING "asoc: failed to add pmdown_time sysfs\n");
1386
1387 /* add DAPM sysfs entries for this codec */
1388 ret = snd_soc_dapm_sys_add(&rtd->dev);
1389 if (ret < 0)
1390 printk(KERN_WARNING "asoc: failed to add codec dapm sysfs entries\n");
1391
1392 /* add codec sysfs entries */
1393 ret = device_create_file(&rtd->dev, &dev_attr_codec_reg);
1394 if (ret < 0)
1395 printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
1396
1397 /* create the pcm */
1398 ret = soc_new_pcm(rtd, num);
1399 if (ret < 0) {
1400 printk(KERN_ERR "asoc: can't create pcm %s\n", dai_link->stream_name);
1401 return ret;
1402 }
1403
1404 /* add platform data for AC97 devices */
1405 if (rtd->codec_dai->driver->ac97_control)
1406 snd_ac97_dev_add_pdata(codec->ac97, rtd->cpu_dai->ac97_pdata);
1407
1408 return 0;
1409 }
1410
1411 #ifdef CONFIG_SND_SOC_AC97_BUS
1412 static int soc_register_ac97_dai_link(struct snd_soc_pcm_runtime *rtd)
1413 {
1414 int ret;
1415
1416 /* Only instantiate AC97 if not already done by the adaptor
1417 * for the generic AC97 subsystem.
1418 */
1419 if (rtd->codec_dai->driver->ac97_control && !rtd->codec->ac97_registered) {
1420
1421 ret = soc_ac97_dev_register(rtd->codec);
1422 if (ret < 0) {
1423 printk(KERN_ERR "asoc: AC97 device register failed\n");
1424 return ret;
1425 }
1426
1427 rtd->codec->ac97_registered = 1;
1428 }
1429 return 0;
1430 }
1431
1432 static void soc_unregister_ac97_dai_link(struct snd_soc_codec *codec)
1433 {
1434 if (codec->ac97_registered) {
1435 soc_ac97_dev_unregister(codec);
1436 codec->ac97_registered = 0;
1437 }
1438 }
1439 #endif
1440
1441 static void snd_soc_instantiate_card(struct snd_soc_card *card)
1442 {
1443 struct platform_device *pdev = to_platform_device(card->dev);
1444 int ret, i;
1445
1446 mutex_lock(&card->mutex);
1447
1448 if (card->instantiated) {
1449 mutex_unlock(&card->mutex);
1450 return;
1451 }
1452
1453 /* bind DAIs */
1454 for (i = 0; i < card->num_links; i++)
1455 soc_bind_dai_link(card, i);
1456
1457 /* bind completed ? */
1458 if (card->num_rtd != card->num_links) {
1459 mutex_unlock(&card->mutex);
1460 return;
1461 }
1462
1463 /* card bind complete so register a sound card */
1464 ret = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
1465 card->owner, 0, &card->snd_card);
1466 if (ret < 0) {
1467 printk(KERN_ERR "asoc: can't create sound card for card %s\n",
1468 card->name);
1469 mutex_unlock(&card->mutex);
1470 return;
1471 }
1472 card->snd_card->dev = card->dev;
1473
1474 #ifdef CONFIG_PM
1475 /* deferred resume work */
1476 INIT_WORK(&card->deferred_resume_work, soc_resume_deferred);
1477 #endif
1478
1479 /* initialise the sound card only once */
1480 if (card->probe) {
1481 ret = card->probe(pdev);
1482 if (ret < 0)
1483 goto card_probe_error;
1484 }
1485
1486 for (i = 0; i < card->num_links; i++) {
1487 ret = soc_probe_dai_link(card, i);
1488 if (ret < 0) {
1489 printk(KERN_ERR "asoc: failed to instanciate card %s\n", card->name);
1490 goto probe_dai_err;
1491 }
1492 }
1493
1494 snprintf(card->snd_card->shortname, sizeof(card->snd_card->shortname),
1495 "%s", card->name);
1496 snprintf(card->snd_card->longname, sizeof(card->snd_card->longname),
1497 "%s", card->name);
1498
1499 ret = snd_card_register(card->snd_card);
1500 if (ret < 0) {
1501 printk(KERN_ERR "asoc: failed to register soundcard for %s\n", card->name);
1502 goto probe_dai_err;
1503 }
1504
1505 #ifdef CONFIG_SND_SOC_AC97_BUS
1506 /* register any AC97 codecs */
1507 for (i = 0; i < card->num_rtd; i++) {
1508 ret = soc_register_ac97_dai_link(&card->rtd[i]);
1509 if (ret < 0) {
1510 printk(KERN_ERR "asoc: failed to register AC97 %s\n", card->name);
1511 goto probe_dai_err;
1512 }
1513 }
1514 #endif
1515
1516 card->instantiated = 1;
1517 mutex_unlock(&card->mutex);
1518 return;
1519
1520 probe_dai_err:
1521 for (i = 0; i < card->num_links; i++)
1522 soc_remove_dai_link(card, i);
1523
1524 card_probe_error:
1525 if (card->remove)
1526 card->remove(pdev);
1527
1528 snd_card_free(card->snd_card);
1529
1530 mutex_unlock(&card->mutex);
1531 }
1532
1533 /*
1534 * Attempt to initialise any uninitialised cards. Must be called with
1535 * client_mutex.
1536 */
1537 static void snd_soc_instantiate_cards(void)
1538 {
1539 struct snd_soc_card *card;
1540 list_for_each_entry(card, &card_list, list)
1541 snd_soc_instantiate_card(card);
1542 }
1543
1544 /* probes a new socdev */
1545 static int soc_probe(struct platform_device *pdev)
1546 {
1547 struct snd_soc_card *card = platform_get_drvdata(pdev);
1548 int ret = 0;
1549
1550 /* Bodge while we unpick instantiation */
1551 card->dev = &pdev->dev;
1552 INIT_LIST_HEAD(&card->dai_dev_list);
1553 INIT_LIST_HEAD(&card->codec_dev_list);
1554 INIT_LIST_HEAD(&card->platform_dev_list);
1555
1556 ret = snd_soc_register_card(card);
1557 if (ret != 0) {
1558 dev_err(&pdev->dev, "Failed to register card\n");
1559 return ret;
1560 }
1561
1562 return 0;
1563 }
1564
1565 /* removes a socdev */
1566 static int soc_remove(struct platform_device *pdev)
1567 {
1568 struct snd_soc_card *card = platform_get_drvdata(pdev);
1569 int i;
1570
1571 if (card->instantiated) {
1572
1573 /* make sure any delayed work runs */
1574 for (i = 0; i < card->num_rtd; i++) {
1575 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1576 run_delayed_work(&rtd->delayed_work);
1577 }
1578
1579 /* remove and free each DAI */
1580 for (i = 0; i < card->num_rtd; i++)
1581 soc_remove_dai_link(card, i);
1582
1583 /* remove the card */
1584 if (card->remove)
1585 card->remove(pdev);
1586
1587 kfree(card->rtd);
1588 snd_card_free(card->snd_card);
1589 }
1590 snd_soc_unregister_card(card);
1591 return 0;
1592 }
1593
1594 static int soc_poweroff(struct device *dev)
1595 {
1596 struct platform_device *pdev = to_platform_device(dev);
1597 struct snd_soc_card *card = platform_get_drvdata(pdev);
1598 int i;
1599
1600 if (!card->instantiated)
1601 return 0;
1602
1603 /* Flush out pmdown_time work - we actually do want to run it
1604 * now, we're shutting down so no imminent restart. */
1605 for (i = 0; i < card->num_rtd; i++) {
1606 struct snd_soc_pcm_runtime *rtd = &card->rtd[i];
1607 run_delayed_work(&rtd->delayed_work);
1608 }
1609
1610 snd_soc_dapm_shutdown(card);
1611
1612 return 0;
1613 }
1614
1615 static const struct dev_pm_ops soc_pm_ops = {
1616 .suspend = soc_suspend,
1617 .resume = soc_resume,
1618 .poweroff = soc_poweroff,
1619 };
1620
1621 /* ASoC platform driver */
1622 static struct platform_driver soc_driver = {
1623 .driver = {
1624 .name = "soc-audio",
1625 .owner = THIS_MODULE,
1626 .pm = &soc_pm_ops,
1627 },
1628 .probe = soc_probe,
1629 .remove = soc_remove,
1630 };
1631
1632 /* create a new pcm */
1633 static int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
1634 {
1635 struct snd_soc_codec *codec = rtd->codec;
1636 struct snd_soc_platform *platform = rtd->platform;
1637 struct snd_soc_dai *codec_dai = rtd->codec_dai;
1638 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
1639 struct snd_pcm *pcm;
1640 char new_name[64];
1641 int ret = 0, playback = 0, capture = 0;
1642
1643 /* check client and interface hw capabilities */
1644 snprintf(new_name, sizeof(new_name), "%s %s-%d",
1645 rtd->dai_link->stream_name, codec_dai->name, num);
1646
1647 if (codec_dai->driver->playback.channels_min)
1648 playback = 1;
1649 if (codec_dai->driver->capture.channels_min)
1650 capture = 1;
1651
1652 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
1653 ret = snd_pcm_new(rtd->card->snd_card, new_name,
1654 num, playback, capture, &pcm);
1655 if (ret < 0) {
1656 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
1657 return ret;
1658 }
1659
1660 rtd->pcm = pcm;
1661 pcm->private_data = rtd;
1662 soc_pcm_ops.mmap = platform->driver->ops->mmap;
1663 soc_pcm_ops.pointer = platform->driver->ops->pointer;
1664 soc_pcm_ops.ioctl = platform->driver->ops->ioctl;
1665 soc_pcm_ops.copy = platform->driver->ops->copy;
1666 soc_pcm_ops.silence = platform->driver->ops->silence;
1667 soc_pcm_ops.ack = platform->driver->ops->ack;
1668 soc_pcm_ops.page = platform->driver->ops->page;
1669
1670 if (playback)
1671 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
1672
1673 if (capture)
1674 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
1675
1676 ret = platform->driver->pcm_new(rtd->card->snd_card, codec_dai, pcm);
1677 if (ret < 0) {
1678 printk(KERN_ERR "asoc: platform pcm constructor failed\n");
1679 return ret;
1680 }
1681
1682 pcm->private_free = platform->driver->pcm_free;
1683 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
1684 cpu_dai->name);
1685 return ret;
1686 }
1687
1688 /**
1689 * snd_soc_codec_volatile_register: Report if a register is volatile.
1690 *
1691 * @codec: CODEC to query.
1692 * @reg: Register to query.
1693 *
1694 * Boolean function indiciating if a CODEC register is volatile.
1695 */
1696 int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg)
1697 {
1698 if (codec->driver->volatile_register)
1699 return codec->driver->volatile_register(reg);
1700 else
1701 return 0;
1702 }
1703 EXPORT_SYMBOL_GPL(snd_soc_codec_volatile_register);
1704
1705 /**
1706 * snd_soc_new_ac97_codec - initailise AC97 device
1707 * @codec: audio codec
1708 * @ops: AC97 bus operations
1709 * @num: AC97 codec number
1710 *
1711 * Initialises AC97 codec resources for use by ad-hoc devices only.
1712 */
1713 int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
1714 struct snd_ac97_bus_ops *ops, int num)
1715 {
1716 mutex_lock(&codec->mutex);
1717
1718 codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
1719 if (codec->ac97 == NULL) {
1720 mutex_unlock(&codec->mutex);
1721 return -ENOMEM;
1722 }
1723
1724 codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
1725 if (codec->ac97->bus == NULL) {
1726 kfree(codec->ac97);
1727 codec->ac97 = NULL;
1728 mutex_unlock(&codec->mutex);
1729 return -ENOMEM;
1730 }
1731
1732 codec->ac97->bus->ops = ops;
1733 codec->ac97->num = num;
1734 mutex_unlock(&codec->mutex);
1735 return 0;
1736 }
1737 EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
1738
1739 /**
1740 * snd_soc_free_ac97_codec - free AC97 codec device
1741 * @codec: audio codec
1742 *
1743 * Frees AC97 codec device resources.
1744 */
1745 void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
1746 {
1747 mutex_lock(&codec->mutex);
1748 #ifdef CONFIG_SND_SOC_AC97_BUS
1749 soc_unregister_ac97_dai_link(codec);
1750 #endif
1751 kfree(codec->ac97->bus);
1752 kfree(codec->ac97);
1753 codec->ac97 = NULL;
1754 mutex_unlock(&codec->mutex);
1755 }
1756 EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
1757
1758 /**
1759 * snd_soc_update_bits - update codec register bits
1760 * @codec: audio codec
1761 * @reg: codec register
1762 * @mask: register mask
1763 * @value: new value
1764 *
1765 * Writes new register value.
1766 *
1767 * Returns 1 for change else 0.
1768 */
1769 int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
1770 unsigned int mask, unsigned int value)
1771 {
1772 int change;
1773 unsigned int old, new;
1774
1775 old = snd_soc_read(codec, reg);
1776 new = (old & ~mask) | value;
1777 change = old != new;
1778 if (change)
1779 snd_soc_write(codec, reg, new);
1780
1781 return change;
1782 }
1783 EXPORT_SYMBOL_GPL(snd_soc_update_bits);
1784
1785 /**
1786 * snd_soc_update_bits_locked - update codec register bits
1787 * @codec: audio codec
1788 * @reg: codec register
1789 * @mask: register mask
1790 * @value: new value
1791 *
1792 * Writes new register value, and takes the codec mutex.
1793 *
1794 * Returns 1 for change else 0.
1795 */
1796 int snd_soc_update_bits_locked(struct snd_soc_codec *codec,
1797 unsigned short reg, unsigned int mask,
1798 unsigned int value)
1799 {
1800 int change;
1801
1802 mutex_lock(&codec->mutex);
1803 change = snd_soc_update_bits(codec, reg, mask, value);
1804 mutex_unlock(&codec->mutex);
1805
1806 return change;
1807 }
1808 EXPORT_SYMBOL_GPL(snd_soc_update_bits_locked);
1809
1810 /**
1811 * snd_soc_test_bits - test register for change
1812 * @codec: audio codec
1813 * @reg: codec register
1814 * @mask: register mask
1815 * @value: new value
1816 *
1817 * Tests a register with a new value and checks if the new value is
1818 * different from the old value.
1819 *
1820 * Returns 1 for change else 0.
1821 */
1822 int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
1823 unsigned int mask, unsigned int value)
1824 {
1825 int change;
1826 unsigned int old, new;
1827
1828 old = snd_soc_read(codec, reg);
1829 new = (old & ~mask) | value;
1830 change = old != new;
1831
1832 return change;
1833 }
1834 EXPORT_SYMBOL_GPL(snd_soc_test_bits);
1835
1836 /**
1837 * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
1838 * @substream: the pcm substream
1839 * @hw: the hardware parameters
1840 *
1841 * Sets the substream runtime hardware parameters.
1842 */
1843 int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
1844 const struct snd_pcm_hardware *hw)
1845 {
1846 struct snd_pcm_runtime *runtime = substream->runtime;
1847 runtime->hw.info = hw->info;
1848 runtime->hw.formats = hw->formats;
1849 runtime->hw.period_bytes_min = hw->period_bytes_min;
1850 runtime->hw.period_bytes_max = hw->period_bytes_max;
1851 runtime->hw.periods_min = hw->periods_min;
1852 runtime->hw.periods_max = hw->periods_max;
1853 runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
1854 runtime->hw.fifo_size = hw->fifo_size;
1855 return 0;
1856 }
1857 EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
1858
1859 /**
1860 * snd_soc_cnew - create new control
1861 * @_template: control template
1862 * @data: control private data
1863 * @long_name: control long name
1864 *
1865 * Create a new mixer control from a template control.
1866 *
1867 * Returns 0 for success, else error.
1868 */
1869 struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
1870 void *data, char *long_name)
1871 {
1872 struct snd_kcontrol_new template;
1873
1874 memcpy(&template, _template, sizeof(template));
1875 if (long_name)
1876 template.name = long_name;
1877 template.index = 0;
1878
1879 return snd_ctl_new1(&template, data);
1880 }
1881 EXPORT_SYMBOL_GPL(snd_soc_cnew);
1882
1883 /**
1884 * snd_soc_add_controls - add an array of controls to a codec.
1885 * Convienience function to add a list of controls. Many codecs were
1886 * duplicating this code.
1887 *
1888 * @codec: codec to add controls to
1889 * @controls: array of controls to add
1890 * @num_controls: number of elements in the array
1891 *
1892 * Return 0 for success, else error.
1893 */
1894 int snd_soc_add_controls(struct snd_soc_codec *codec,
1895 const struct snd_kcontrol_new *controls, int num_controls)
1896 {
1897 struct snd_card *card = codec->card->snd_card;
1898 int err, i;
1899
1900 for (i = 0; i < num_controls; i++) {
1901 const struct snd_kcontrol_new *control = &controls[i];
1902 err = snd_ctl_add(card, snd_soc_cnew(control, codec, NULL));
1903 if (err < 0) {
1904 dev_err(codec->dev, "%s: Failed to add %s\n",
1905 codec->name, control->name);
1906 return err;
1907 }
1908 }
1909
1910 return 0;
1911 }
1912 EXPORT_SYMBOL_GPL(snd_soc_add_controls);
1913
1914 /**
1915 * snd_soc_info_enum_double - enumerated double mixer info callback
1916 * @kcontrol: mixer control
1917 * @uinfo: control element information
1918 *
1919 * Callback to provide information about a double enumerated
1920 * mixer control.
1921 *
1922 * Returns 0 for success.
1923 */
1924 int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_info *uinfo)
1926 {
1927 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1928
1929 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1930 uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
1931 uinfo->value.enumerated.items = e->max;
1932
1933 if (uinfo->value.enumerated.item > e->max - 1)
1934 uinfo->value.enumerated.item = e->max - 1;
1935 strcpy(uinfo->value.enumerated.name,
1936 e->texts[uinfo->value.enumerated.item]);
1937 return 0;
1938 }
1939 EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
1940
1941 /**
1942 * snd_soc_get_enum_double - enumerated double mixer get callback
1943 * @kcontrol: mixer control
1944 * @ucontrol: control element information
1945 *
1946 * Callback to get the value of a double enumerated mixer.
1947 *
1948 * Returns 0 for success.
1949 */
1950 int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
1951 struct snd_ctl_elem_value *ucontrol)
1952 {
1953 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1954 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1955 unsigned int val, bitmask;
1956
1957 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1958 ;
1959 val = snd_soc_read(codec, e->reg);
1960 ucontrol->value.enumerated.item[0]
1961 = (val >> e->shift_l) & (bitmask - 1);
1962 if (e->shift_l != e->shift_r)
1963 ucontrol->value.enumerated.item[1] =
1964 (val >> e->shift_r) & (bitmask - 1);
1965
1966 return 0;
1967 }
1968 EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
1969
1970 /**
1971 * snd_soc_put_enum_double - enumerated double mixer put callback
1972 * @kcontrol: mixer control
1973 * @ucontrol: control element information
1974 *
1975 * Callback to set the value of a double enumerated mixer.
1976 *
1977 * Returns 0 for success.
1978 */
1979 int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981 {
1982 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
1983 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1984 unsigned int val;
1985 unsigned int mask, bitmask;
1986
1987 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1988 ;
1989 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1990 return -EINVAL;
1991 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1992 mask = (bitmask - 1) << e->shift_l;
1993 if (e->shift_l != e->shift_r) {
1994 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1995 return -EINVAL;
1996 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1997 mask |= (bitmask - 1) << e->shift_r;
1998 }
1999
2000 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2001 }
2002 EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
2003
2004 /**
2005 * snd_soc_get_value_enum_double - semi enumerated double mixer get callback
2006 * @kcontrol: mixer control
2007 * @ucontrol: control element information
2008 *
2009 * Callback to get the value of a double semi enumerated mixer.
2010 *
2011 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2012 * used for handling bitfield coded enumeration for example.
2013 *
2014 * Returns 0 for success.
2015 */
2016 int snd_soc_get_value_enum_double(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
2018 {
2019 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2020 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2021 unsigned int reg_val, val, mux;
2022
2023 reg_val = snd_soc_read(codec, e->reg);
2024 val = (reg_val >> e->shift_l) & e->mask;
2025 for (mux = 0; mux < e->max; mux++) {
2026 if (val == e->values[mux])
2027 break;
2028 }
2029 ucontrol->value.enumerated.item[0] = mux;
2030 if (e->shift_l != e->shift_r) {
2031 val = (reg_val >> e->shift_r) & e->mask;
2032 for (mux = 0; mux < e->max; mux++) {
2033 if (val == e->values[mux])
2034 break;
2035 }
2036 ucontrol->value.enumerated.item[1] = mux;
2037 }
2038
2039 return 0;
2040 }
2041 EXPORT_SYMBOL_GPL(snd_soc_get_value_enum_double);
2042
2043 /**
2044 * snd_soc_put_value_enum_double - semi enumerated double mixer put callback
2045 * @kcontrol: mixer control
2046 * @ucontrol: control element information
2047 *
2048 * Callback to set the value of a double semi enumerated mixer.
2049 *
2050 * Semi enumerated mixer: the enumerated items are referred as values. Can be
2051 * used for handling bitfield coded enumeration for example.
2052 *
2053 * Returns 0 for success.
2054 */
2055 int snd_soc_put_value_enum_double(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057 {
2058 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2059 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2060 unsigned int val;
2061 unsigned int mask;
2062
2063 if (ucontrol->value.enumerated.item[0] > e->max - 1)
2064 return -EINVAL;
2065 val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
2066 mask = e->mask << e->shift_l;
2067 if (e->shift_l != e->shift_r) {
2068 if (ucontrol->value.enumerated.item[1] > e->max - 1)
2069 return -EINVAL;
2070 val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
2071 mask |= e->mask << e->shift_r;
2072 }
2073
2074 return snd_soc_update_bits_locked(codec, e->reg, mask, val);
2075 }
2076 EXPORT_SYMBOL_GPL(snd_soc_put_value_enum_double);
2077
2078 /**
2079 * snd_soc_info_enum_ext - external enumerated single mixer info callback
2080 * @kcontrol: mixer control
2081 * @uinfo: control element information
2082 *
2083 * Callback to provide information about an external enumerated
2084 * single mixer.
2085 *
2086 * Returns 0 for success.
2087 */
2088 int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
2089 struct snd_ctl_elem_info *uinfo)
2090 {
2091 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
2092
2093 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2094 uinfo->count = 1;
2095 uinfo->value.enumerated.items = e->max;
2096
2097 if (uinfo->value.enumerated.item > e->max - 1)
2098 uinfo->value.enumerated.item = e->max - 1;
2099 strcpy(uinfo->value.enumerated.name,
2100 e->texts[uinfo->value.enumerated.item]);
2101 return 0;
2102 }
2103 EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
2104
2105 /**
2106 * snd_soc_info_volsw_ext - external single mixer info callback
2107 * @kcontrol: mixer control
2108 * @uinfo: control element information
2109 *
2110 * Callback to provide information about a single external mixer control.
2111 *
2112 * Returns 0 for success.
2113 */
2114 int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
2115 struct snd_ctl_elem_info *uinfo)
2116 {
2117 int max = kcontrol->private_value;
2118
2119 if (max == 1 && !strstr(kcontrol->id.name, " Volume"))
2120 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2121 else
2122 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2123
2124 uinfo->count = 1;
2125 uinfo->value.integer.min = 0;
2126 uinfo->value.integer.max = max;
2127 return 0;
2128 }
2129 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
2130
2131 /**
2132 * snd_soc_info_volsw - single mixer info callback
2133 * @kcontrol: mixer control
2134 * @uinfo: control element information
2135 *
2136 * Callback to provide information about a single mixer control.
2137 *
2138 * Returns 0 for success.
2139 */
2140 int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
2141 struct snd_ctl_elem_info *uinfo)
2142 {
2143 struct soc_mixer_control *mc =
2144 (struct soc_mixer_control *)kcontrol->private_value;
2145 int platform_max;
2146 unsigned int shift = mc->shift;
2147 unsigned int rshift = mc->rshift;
2148
2149 if (!mc->platform_max)
2150 mc->platform_max = mc->max;
2151 platform_max = mc->platform_max;
2152
2153 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2154 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2155 else
2156 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2157
2158 uinfo->count = shift == rshift ? 1 : 2;
2159 uinfo->value.integer.min = 0;
2160 uinfo->value.integer.max = platform_max;
2161 return 0;
2162 }
2163 EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
2164
2165 /**
2166 * snd_soc_get_volsw - single mixer get callback
2167 * @kcontrol: mixer control
2168 * @ucontrol: control element information
2169 *
2170 * Callback to get the value of a single mixer control.
2171 *
2172 * Returns 0 for success.
2173 */
2174 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176 {
2177 struct soc_mixer_control *mc =
2178 (struct soc_mixer_control *)kcontrol->private_value;
2179 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2180 unsigned int reg = mc->reg;
2181 unsigned int shift = mc->shift;
2182 unsigned int rshift = mc->rshift;
2183 int max = mc->max;
2184 unsigned int mask = (1 << fls(max)) - 1;
2185 unsigned int invert = mc->invert;
2186
2187 ucontrol->value.integer.value[0] =
2188 (snd_soc_read(codec, reg) >> shift) & mask;
2189 if (shift != rshift)
2190 ucontrol->value.integer.value[1] =
2191 (snd_soc_read(codec, reg) >> rshift) & mask;
2192 if (invert) {
2193 ucontrol->value.integer.value[0] =
2194 max - ucontrol->value.integer.value[0];
2195 if (shift != rshift)
2196 ucontrol->value.integer.value[1] =
2197 max - ucontrol->value.integer.value[1];
2198 }
2199
2200 return 0;
2201 }
2202 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
2203
2204 /**
2205 * snd_soc_put_volsw - single mixer put callback
2206 * @kcontrol: mixer control
2207 * @ucontrol: control element information
2208 *
2209 * Callback to set the value of a single mixer control.
2210 *
2211 * Returns 0 for success.
2212 */
2213 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_value *ucontrol)
2215 {
2216 struct soc_mixer_control *mc =
2217 (struct soc_mixer_control *)kcontrol->private_value;
2218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2219 unsigned int reg = mc->reg;
2220 unsigned int shift = mc->shift;
2221 unsigned int rshift = mc->rshift;
2222 int max = mc->max;
2223 unsigned int mask = (1 << fls(max)) - 1;
2224 unsigned int invert = mc->invert;
2225 unsigned int val, val2, val_mask;
2226
2227 val = (ucontrol->value.integer.value[0] & mask);
2228 if (invert)
2229 val = max - val;
2230 val_mask = mask << shift;
2231 val = val << shift;
2232 if (shift != rshift) {
2233 val2 = (ucontrol->value.integer.value[1] & mask);
2234 if (invert)
2235 val2 = max - val2;
2236 val_mask |= mask << rshift;
2237 val |= val2 << rshift;
2238 }
2239 return snd_soc_update_bits_locked(codec, reg, val_mask, val);
2240 }
2241 EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
2242
2243 /**
2244 * snd_soc_info_volsw_2r - double mixer info callback
2245 * @kcontrol: mixer control
2246 * @uinfo: control element information
2247 *
2248 * Callback to provide information about a double mixer control that
2249 * spans 2 codec registers.
2250 *
2251 * Returns 0 for success.
2252 */
2253 int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
2254 struct snd_ctl_elem_info *uinfo)
2255 {
2256 struct soc_mixer_control *mc =
2257 (struct soc_mixer_control *)kcontrol->private_value;
2258 int platform_max;
2259
2260 if (!mc->platform_max)
2261 mc->platform_max = mc->max;
2262 platform_max = mc->platform_max;
2263
2264 if (platform_max == 1 && !strstr(kcontrol->id.name, " Volume"))
2265 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2266 else
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2268
2269 uinfo->count = 2;
2270 uinfo->value.integer.min = 0;
2271 uinfo->value.integer.max = platform_max;
2272 return 0;
2273 }
2274 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
2275
2276 /**
2277 * snd_soc_get_volsw_2r - double mixer get callback
2278 * @kcontrol: mixer control
2279 * @ucontrol: control element information
2280 *
2281 * Callback to get the value of a double mixer control that spans 2 registers.
2282 *
2283 * Returns 0 for success.
2284 */
2285 int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
2286 struct snd_ctl_elem_value *ucontrol)
2287 {
2288 struct soc_mixer_control *mc =
2289 (struct soc_mixer_control *)kcontrol->private_value;
2290 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2291 unsigned int reg = mc->reg;
2292 unsigned int reg2 = mc->rreg;
2293 unsigned int shift = mc->shift;
2294 int max = mc->max;
2295 unsigned int mask = (1 << fls(max)) - 1;
2296 unsigned int invert = mc->invert;
2297
2298 ucontrol->value.integer.value[0] =
2299 (snd_soc_read(codec, reg) >> shift) & mask;
2300 ucontrol->value.integer.value[1] =
2301 (snd_soc_read(codec, reg2) >> shift) & mask;
2302 if (invert) {
2303 ucontrol->value.integer.value[0] =
2304 max - ucontrol->value.integer.value[0];
2305 ucontrol->value.integer.value[1] =
2306 max - ucontrol->value.integer.value[1];
2307 }
2308
2309 return 0;
2310 }
2311 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
2312
2313 /**
2314 * snd_soc_put_volsw_2r - double mixer set callback
2315 * @kcontrol: mixer control
2316 * @ucontrol: control element information
2317 *
2318 * Callback to set the value of a double mixer control that spans 2 registers.
2319 *
2320 * Returns 0 for success.
2321 */
2322 int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
2323 struct snd_ctl_elem_value *ucontrol)
2324 {
2325 struct soc_mixer_control *mc =
2326 (struct soc_mixer_control *)kcontrol->private_value;
2327 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2328 unsigned int reg = mc->reg;
2329 unsigned int reg2 = mc->rreg;
2330 unsigned int shift = mc->shift;
2331 int max = mc->max;
2332 unsigned int mask = (1 << fls(max)) - 1;
2333 unsigned int invert = mc->invert;
2334 int err;
2335 unsigned int val, val2, val_mask;
2336
2337 val_mask = mask << shift;
2338 val = (ucontrol->value.integer.value[0] & mask);
2339 val2 = (ucontrol->value.integer.value[1] & mask);
2340
2341 if (invert) {
2342 val = max - val;
2343 val2 = max - val2;
2344 }
2345
2346 val = val << shift;
2347 val2 = val2 << shift;
2348
2349 err = snd_soc_update_bits_locked(codec, reg, val_mask, val);
2350 if (err < 0)
2351 return err;
2352
2353 err = snd_soc_update_bits_locked(codec, reg2, val_mask, val2);
2354 return err;
2355 }
2356 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
2357
2358 /**
2359 * snd_soc_info_volsw_s8 - signed mixer info callback
2360 * @kcontrol: mixer control
2361 * @uinfo: control element information
2362 *
2363 * Callback to provide information about a signed mixer control.
2364 *
2365 * Returns 0 for success.
2366 */
2367 int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2369 {
2370 struct soc_mixer_control *mc =
2371 (struct soc_mixer_control *)kcontrol->private_value;
2372 int platform_max;
2373 int min = mc->min;
2374
2375 if (!mc->platform_max)
2376 mc->platform_max = mc->max;
2377 platform_max = mc->platform_max;
2378
2379 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2380 uinfo->count = 2;
2381 uinfo->value.integer.min = 0;
2382 uinfo->value.integer.max = platform_max - min;
2383 return 0;
2384 }
2385 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
2386
2387 /**
2388 * snd_soc_get_volsw_s8 - signed mixer get callback
2389 * @kcontrol: mixer control
2390 * @ucontrol: control element information
2391 *
2392 * Callback to get the value of a signed mixer control.
2393 *
2394 * Returns 0 for success.
2395 */
2396 int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
2397 struct snd_ctl_elem_value *ucontrol)
2398 {
2399 struct soc_mixer_control *mc =
2400 (struct soc_mixer_control *)kcontrol->private_value;
2401 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2402 unsigned int reg = mc->reg;
2403 int min = mc->min;
2404 int val = snd_soc_read(codec, reg);
2405
2406 ucontrol->value.integer.value[0] =
2407 ((signed char)(val & 0xff))-min;
2408 ucontrol->value.integer.value[1] =
2409 ((signed char)((val >> 8) & 0xff))-min;
2410 return 0;
2411 }
2412 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
2413
2414 /**
2415 * snd_soc_put_volsw_sgn - signed mixer put callback
2416 * @kcontrol: mixer control
2417 * @ucontrol: control element information
2418 *
2419 * Callback to set the value of a signed mixer control.
2420 *
2421 * Returns 0 for success.
2422 */
2423 int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2425 {
2426 struct soc_mixer_control *mc =
2427 (struct soc_mixer_control *)kcontrol->private_value;
2428 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2429 unsigned int reg = mc->reg;
2430 int min = mc->min;
2431 unsigned int val;
2432
2433 val = (ucontrol->value.integer.value[0]+min) & 0xff;
2434 val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
2435
2436 return snd_soc_update_bits_locked(codec, reg, 0xffff, val);
2437 }
2438 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
2439
2440 /**
2441 * snd_soc_limit_volume - Set new limit to an existing volume control.
2442 *
2443 * @codec: where to look for the control
2444 * @name: Name of the control
2445 * @max: new maximum limit
2446 *
2447 * Return 0 for success, else error.
2448 */
2449 int snd_soc_limit_volume(struct snd_soc_codec *codec,
2450 const char *name, int max)
2451 {
2452 struct snd_card *card = codec->card->snd_card;
2453 struct snd_kcontrol *kctl;
2454 struct soc_mixer_control *mc;
2455 int found = 0;
2456 int ret = -EINVAL;
2457
2458 /* Sanity check for name and max */
2459 if (unlikely(!name || max <= 0))
2460 return -EINVAL;
2461
2462 list_for_each_entry(kctl, &card->controls, list) {
2463 if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) {
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (found) {
2469 mc = (struct soc_mixer_control *)kctl->private_value;
2470 if (max <= mc->max) {
2471 mc->platform_max = max;
2472 ret = 0;
2473 }
2474 }
2475 return ret;
2476 }
2477 EXPORT_SYMBOL_GPL(snd_soc_limit_volume);
2478
2479 /**
2480 * snd_soc_info_volsw_2r_sx - double with tlv and variable data size
2481 * mixer info callback
2482 * @kcontrol: mixer control
2483 * @uinfo: control element information
2484 *
2485 * Returns 0 for success.
2486 */
2487 int snd_soc_info_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_info *uinfo)
2489 {
2490 struct soc_mixer_control *mc =
2491 (struct soc_mixer_control *)kcontrol->private_value;
2492 int max = mc->max;
2493 int min = mc->min;
2494
2495 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2496 uinfo->count = 2;
2497 uinfo->value.integer.min = 0;
2498 uinfo->value.integer.max = max-min;
2499
2500 return 0;
2501 }
2502 EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r_sx);
2503
2504 /**
2505 * snd_soc_get_volsw_2r_sx - double with tlv and variable data size
2506 * mixer get callback
2507 * @kcontrol: mixer control
2508 * @uinfo: control element information
2509 *
2510 * Returns 0 for success.
2511 */
2512 int snd_soc_get_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2513 struct snd_ctl_elem_value *ucontrol)
2514 {
2515 struct soc_mixer_control *mc =
2516 (struct soc_mixer_control *)kcontrol->private_value;
2517 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2518 unsigned int mask = (1<<mc->shift)-1;
2519 int min = mc->min;
2520 int val = snd_soc_read(codec, mc->reg) & mask;
2521 int valr = snd_soc_read(codec, mc->rreg) & mask;
2522
2523 ucontrol->value.integer.value[0] = ((val & 0xff)-min) & mask;
2524 ucontrol->value.integer.value[1] = ((valr & 0xff)-min) & mask;
2525 return 0;
2526 }
2527 EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r_sx);
2528
2529 /**
2530 * snd_soc_put_volsw_2r_sx - double with tlv and variable data size
2531 * mixer put callback
2532 * @kcontrol: mixer control
2533 * @uinfo: control element information
2534 *
2535 * Returns 0 for success.
2536 */
2537 int snd_soc_put_volsw_2r_sx(struct snd_kcontrol *kcontrol,
2538 struct snd_ctl_elem_value *ucontrol)
2539 {
2540 struct soc_mixer_control *mc =
2541 (struct soc_mixer_control *)kcontrol->private_value;
2542 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
2543 unsigned int mask = (1<<mc->shift)-1;
2544 int min = mc->min;
2545 int ret;
2546 unsigned int val, valr, oval, ovalr;
2547
2548 val = ((ucontrol->value.integer.value[0]+min) & 0xff);
2549 val &= mask;
2550 valr = ((ucontrol->value.integer.value[1]+min) & 0xff);
2551 valr &= mask;
2552
2553 oval = snd_soc_read(codec, mc->reg) & mask;
2554 ovalr = snd_soc_read(codec, mc->rreg) & mask;
2555
2556 ret = 0;
2557 if (oval != val) {
2558 ret = snd_soc_write(codec, mc->reg, val);
2559 if (ret < 0)
2560 return ret;
2561 }
2562 if (ovalr != valr) {
2563 ret = snd_soc_write(codec, mc->rreg, valr);
2564 if (ret < 0)
2565 return ret;
2566 }
2567
2568 return 0;
2569 }
2570 EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r_sx);
2571
2572 /**
2573 * snd_soc_dai_set_sysclk - configure DAI system or master clock.
2574 * @dai: DAI
2575 * @clk_id: DAI specific clock ID
2576 * @freq: new clock frequency in Hz
2577 * @dir: new clock direction - input/output.
2578 *
2579 * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
2580 */
2581 int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
2582 unsigned int freq, int dir)
2583 {
2584 if (dai->driver && dai->driver->ops->set_sysclk)
2585 return dai->driver->ops->set_sysclk(dai, clk_id, freq, dir);
2586 else
2587 return -EINVAL;
2588 }
2589 EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
2590
2591 /**
2592 * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
2593 * @dai: DAI
2594 * @div_id: DAI specific clock divider ID
2595 * @div: new clock divisor.
2596 *
2597 * Configures the clock dividers. This is used to derive the best DAI bit and
2598 * frame clocks from the system or master clock. It's best to set the DAI bit
2599 * and frame clocks as low as possible to save system power.
2600 */
2601 int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
2602 int div_id, int div)
2603 {
2604 if (dai->driver && dai->driver->ops->set_clkdiv)
2605 return dai->driver->ops->set_clkdiv(dai, div_id, div);
2606 else
2607 return -EINVAL;
2608 }
2609 EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
2610
2611 /**
2612 * snd_soc_dai_set_pll - configure DAI PLL.
2613 * @dai: DAI
2614 * @pll_id: DAI specific PLL ID
2615 * @source: DAI specific source for the PLL
2616 * @freq_in: PLL input clock frequency in Hz
2617 * @freq_out: requested PLL output clock frequency in Hz
2618 *
2619 * Configures and enables PLL to generate output clock based on input clock.
2620 */
2621 int snd_soc_dai_set_pll(struct snd_soc_dai *dai, int pll_id, int source,
2622 unsigned int freq_in, unsigned int freq_out)
2623 {
2624 if (dai->driver && dai->driver->ops->set_pll)
2625 return dai->driver->ops->set_pll(dai, pll_id, source,
2626 freq_in, freq_out);
2627 else
2628 return -EINVAL;
2629 }
2630 EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
2631
2632 /**
2633 * snd_soc_dai_set_fmt - configure DAI hardware audio format.
2634 * @dai: DAI
2635 * @fmt: SND_SOC_DAIFMT_ format value.
2636 *
2637 * Configures the DAI hardware format and clocking.
2638 */
2639 int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
2640 {
2641 if (dai->driver && dai->driver->ops->set_fmt)
2642 return dai->driver->ops->set_fmt(dai, fmt);
2643 else
2644 return -EINVAL;
2645 }
2646 EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
2647
2648 /**
2649 * snd_soc_dai_set_tdm_slot - configure DAI TDM.
2650 * @dai: DAI
2651 * @tx_mask: bitmask representing active TX slots.
2652 * @rx_mask: bitmask representing active RX slots.
2653 * @slots: Number of slots in use.
2654 * @slot_width: Width in bits for each slot.
2655 *
2656 * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
2657 * specific.
2658 */
2659 int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
2660 unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
2661 {
2662 if (dai->driver && dai->driver->ops->set_tdm_slot)
2663 return dai->driver->ops->set_tdm_slot(dai, tx_mask, rx_mask,
2664 slots, slot_width);
2665 else
2666 return -EINVAL;
2667 }
2668 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
2669
2670 /**
2671 * snd_soc_dai_set_channel_map - configure DAI audio channel map
2672 * @dai: DAI
2673 * @tx_num: how many TX channels
2674 * @tx_slot: pointer to an array which imply the TX slot number channel
2675 * 0~num-1 uses
2676 * @rx_num: how many RX channels
2677 * @rx_slot: pointer to an array which imply the RX slot number channel
2678 * 0~num-1 uses
2679 *
2680 * configure the relationship between channel number and TDM slot number.
2681 */
2682 int snd_soc_dai_set_channel_map(struct snd_soc_dai *dai,
2683 unsigned int tx_num, unsigned int *tx_slot,
2684 unsigned int rx_num, unsigned int *rx_slot)
2685 {
2686 if (dai->driver && dai->driver->ops->set_channel_map)
2687 return dai->driver->ops->set_channel_map(dai, tx_num, tx_slot,
2688 rx_num, rx_slot);
2689 else
2690 return -EINVAL;
2691 }
2692 EXPORT_SYMBOL_GPL(snd_soc_dai_set_channel_map);
2693
2694 /**
2695 * snd_soc_dai_set_tristate - configure DAI system or master clock.
2696 * @dai: DAI
2697 * @tristate: tristate enable
2698 *
2699 * Tristates the DAI so that others can use it.
2700 */
2701 int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
2702 {
2703 if (dai->driver && dai->driver->ops->set_tristate)
2704 return dai->driver->ops->set_tristate(dai, tristate);
2705 else
2706 return -EINVAL;
2707 }
2708 EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
2709
2710 /**
2711 * snd_soc_dai_digital_mute - configure DAI system or master clock.
2712 * @dai: DAI
2713 * @mute: mute enable
2714 *
2715 * Mutes the DAI DAC.
2716 */
2717 int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
2718 {
2719 if (dai->driver && dai->driver->ops->digital_mute)
2720 return dai->driver->ops->digital_mute(dai, mute);
2721 else
2722 return -EINVAL;
2723 }
2724 EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
2725
2726 /**
2727 * snd_soc_register_card - Register a card with the ASoC core
2728 *
2729 * @card: Card to register
2730 *
2731 * Note that currently this is an internal only function: it will be
2732 * exposed to machine drivers after further backporting of ASoC v2
2733 * registration APIs.
2734 */
2735 static int snd_soc_register_card(struct snd_soc_card *card)
2736 {
2737 int i;
2738
2739 if (!card->name || !card->dev)
2740 return -EINVAL;
2741
2742 card->rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime) * card->num_links,
2743 GFP_KERNEL);
2744 if (card->rtd == NULL)
2745 return -ENOMEM;
2746
2747 for (i = 0; i < card->num_links; i++)
2748 card->rtd[i].dai_link = &card->dai_link[i];
2749
2750 INIT_LIST_HEAD(&card->list);
2751 card->instantiated = 0;
2752 mutex_init(&card->mutex);
2753
2754 mutex_lock(&client_mutex);
2755 list_add(&card->list, &card_list);
2756 snd_soc_instantiate_cards();
2757 mutex_unlock(&client_mutex);
2758
2759 dev_dbg(card->dev, "Registered card '%s'\n", card->name);
2760
2761 return 0;
2762 }
2763
2764 /**
2765 * snd_soc_unregister_card - Unregister a card with the ASoC core
2766 *
2767 * @card: Card to unregister
2768 *
2769 * Note that currently this is an internal only function: it will be
2770 * exposed to machine drivers after further backporting of ASoC v2
2771 * registration APIs.
2772 */
2773 static int snd_soc_unregister_card(struct snd_soc_card *card)
2774 {
2775 mutex_lock(&client_mutex);
2776 list_del(&card->list);
2777 mutex_unlock(&client_mutex);
2778 dev_dbg(card->dev, "Unregistered card '%s'\n", card->name);
2779
2780 return 0;
2781 }
2782
2783 /*
2784 * Simplify DAI link configuration by removing ".-1" from device names
2785 * and sanitizing names.
2786 */
2787 static inline char *fmt_single_name(struct device *dev, int *id)
2788 {
2789 char *found, name[NAME_SIZE];
2790 int id1, id2;
2791
2792 if (dev_name(dev) == NULL)
2793 return NULL;
2794
2795 strncpy(name, dev_name(dev), NAME_SIZE);
2796
2797 /* are we a "%s.%d" name (platform and SPI components) */
2798 found = strstr(name, dev->driver->name);
2799 if (found) {
2800 /* get ID */
2801 if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) {
2802
2803 /* discard ID from name if ID == -1 */
2804 if (*id == -1)
2805 found[strlen(dev->driver->name)] = '\0';
2806 }
2807
2808 } else {
2809 /* I2C component devices are named "bus-addr" */
2810 if (sscanf(name, "%x-%x", &id1, &id2) == 2) {
2811 char tmp[NAME_SIZE];
2812
2813 /* create unique ID number from I2C addr and bus */
2814 *id = ((id1 && 0xffff) << 16) + id2;
2815
2816 /* sanitize component name for DAI link creation */
2817 snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, name);
2818 strncpy(name, tmp, NAME_SIZE);
2819 } else
2820 *id = 0;
2821 }
2822
2823 return kstrdup(name, GFP_KERNEL);
2824 }
2825
2826 /*
2827 * Simplify DAI link naming for single devices with multiple DAIs by removing
2828 * any ".-1" and using the DAI name (instead of device name).
2829 */
2830 static inline char *fmt_multiple_name(struct device *dev,
2831 struct snd_soc_dai_driver *dai_drv)
2832 {
2833 if (dai_drv->name == NULL) {
2834 printk(KERN_ERR "asoc: error - multiple DAI %s registered with no name\n",
2835 dev_name(dev));
2836 return NULL;
2837 }
2838
2839 return kstrdup(dai_drv->name, GFP_KERNEL);
2840 }
2841
2842 /**
2843 * snd_soc_register_dai - Register a DAI with the ASoC core
2844 *
2845 * @dai: DAI to register
2846 */
2847 int snd_soc_register_dai(struct device *dev,
2848 struct snd_soc_dai_driver *dai_drv)
2849 {
2850 struct snd_soc_dai *dai;
2851
2852 dev_dbg(dev, "dai register %s\n", dev_name(dev));
2853
2854 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2855 if (dai == NULL)
2856 return -ENOMEM;
2857
2858 /* create DAI component name */
2859 dai->name = fmt_single_name(dev, &dai->id);
2860 if (dai->name == NULL) {
2861 kfree(dai);
2862 return -ENOMEM;
2863 }
2864
2865 dai->dev = dev;
2866 dai->driver = dai_drv;
2867 if (!dai->driver->ops)
2868 dai->driver->ops = &null_dai_ops;
2869
2870 mutex_lock(&client_mutex);
2871 list_add(&dai->list, &dai_list);
2872 snd_soc_instantiate_cards();
2873 mutex_unlock(&client_mutex);
2874
2875 pr_debug("Registered DAI '%s'\n", dai->name);
2876
2877 return 0;
2878 }
2879 EXPORT_SYMBOL_GPL(snd_soc_register_dai);
2880
2881 /**
2882 * snd_soc_unregister_dai - Unregister a DAI from the ASoC core
2883 *
2884 * @dai: DAI to unregister
2885 */
2886 void snd_soc_unregister_dai(struct device *dev)
2887 {
2888 struct snd_soc_dai *dai;
2889
2890 list_for_each_entry(dai, &dai_list, list) {
2891 if (dev == dai->dev)
2892 goto found;
2893 }
2894 return;
2895
2896 found:
2897 mutex_lock(&client_mutex);
2898 list_del(&dai->list);
2899 mutex_unlock(&client_mutex);
2900
2901 pr_debug("Unregistered DAI '%s'\n", dai->name);
2902 kfree(dai->name);
2903 kfree(dai);
2904 }
2905 EXPORT_SYMBOL_GPL(snd_soc_unregister_dai);
2906
2907 /**
2908 * snd_soc_register_dais - Register multiple DAIs with the ASoC core
2909 *
2910 * @dai: Array of DAIs to register
2911 * @count: Number of DAIs
2912 */
2913 int snd_soc_register_dais(struct device *dev,
2914 struct snd_soc_dai_driver *dai_drv, size_t count)
2915 {
2916 struct snd_soc_dai *dai;
2917 int i, ret = 0;
2918
2919 dev_dbg(dev, "dai register %s #%d\n", dev_name(dev), count);
2920
2921 for (i = 0; i < count; i++) {
2922
2923 dai = kzalloc(sizeof(struct snd_soc_dai), GFP_KERNEL);
2924 if (dai == NULL)
2925 return -ENOMEM;
2926
2927 /* create DAI component name */
2928 dai->name = fmt_multiple_name(dev, &dai_drv[i]);
2929 if (dai->name == NULL) {
2930 kfree(dai);
2931 ret = -EINVAL;
2932 goto err;
2933 }
2934
2935 dai->dev = dev;
2936 dai->id = i;
2937 dai->driver = &dai_drv[i];
2938 if (!dai->driver->ops)
2939 dai->driver->ops = &null_dai_ops;
2940
2941 mutex_lock(&client_mutex);
2942 list_add(&dai->list, &dai_list);
2943 mutex_unlock(&client_mutex);
2944
2945 pr_debug("Registered DAI '%s'\n", dai->name);
2946 }
2947
2948 snd_soc_instantiate_cards();
2949 return 0;
2950
2951 err:
2952 for (i--; i >= 0; i--)
2953 snd_soc_unregister_dai(dev);
2954
2955 return ret;
2956 }
2957 EXPORT_SYMBOL_GPL(snd_soc_register_dais);
2958
2959 /**
2960 * snd_soc_unregister_dais - Unregister multiple DAIs from the ASoC core
2961 *
2962 * @dai: Array of DAIs to unregister
2963 * @count: Number of DAIs
2964 */
2965 void snd_soc_unregister_dais(struct device *dev, size_t count)
2966 {
2967 int i;
2968
2969 for (i = 0; i < count; i++)
2970 snd_soc_unregister_dai(dev);
2971 }
2972 EXPORT_SYMBOL_GPL(snd_soc_unregister_dais);
2973
2974 /**
2975 * snd_soc_register_platform - Register a platform with the ASoC core
2976 *
2977 * @platform: platform to register
2978 */
2979 int snd_soc_register_platform(struct device *dev,
2980 struct snd_soc_platform_driver *platform_drv)
2981 {
2982 struct snd_soc_platform *platform;
2983
2984 dev_dbg(dev, "platform register %s\n", dev_name(dev));
2985
2986 platform = kzalloc(sizeof(struct snd_soc_platform), GFP_KERNEL);
2987 if (platform == NULL)
2988 return -ENOMEM;
2989
2990 /* create platform component name */
2991 platform->name = fmt_single_name(dev, &platform->id);
2992 if (platform->name == NULL) {
2993 kfree(platform);
2994 return -ENOMEM;
2995 }
2996
2997 platform->dev = dev;
2998 platform->driver = platform_drv;
2999
3000 mutex_lock(&client_mutex);
3001 list_add(&platform->list, &platform_list);
3002 snd_soc_instantiate_cards();
3003 mutex_unlock(&client_mutex);
3004
3005 pr_debug("Registered platform '%s'\n", platform->name);
3006
3007 return 0;
3008 }
3009 EXPORT_SYMBOL_GPL(snd_soc_register_platform);
3010
3011 /**
3012 * snd_soc_unregister_platform - Unregister a platform from the ASoC core
3013 *
3014 * @platform: platform to unregister
3015 */
3016 void snd_soc_unregister_platform(struct device *dev)
3017 {
3018 struct snd_soc_platform *platform;
3019
3020 list_for_each_entry(platform, &platform_list, list) {
3021 if (dev == platform->dev)
3022 goto found;
3023 }
3024 return;
3025
3026 found:
3027 mutex_lock(&client_mutex);
3028 list_del(&platform->list);
3029 mutex_unlock(&client_mutex);
3030
3031 pr_debug("Unregistered platform '%s'\n", platform->name);
3032 kfree(platform->name);
3033 kfree(platform);
3034 }
3035 EXPORT_SYMBOL_GPL(snd_soc_unregister_platform);
3036
3037 static u64 codec_format_map[] = {
3038 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE,
3039 SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE,
3040 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE,
3041 SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_U24_BE,
3042 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE,
3043 SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE,
3044 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3045 SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_U24_3BE,
3046 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE,
3047 SNDRV_PCM_FMTBIT_U20_3LE | SNDRV_PCM_FMTBIT_U20_3BE,
3048 SNDRV_PCM_FMTBIT_S18_3LE | SNDRV_PCM_FMTBIT_S18_3BE,
3049 SNDRV_PCM_FMTBIT_U18_3LE | SNDRV_PCM_FMTBIT_U18_3BE,
3050 SNDRV_PCM_FMTBIT_FLOAT_LE | SNDRV_PCM_FMTBIT_FLOAT_BE,
3051 SNDRV_PCM_FMTBIT_FLOAT64_LE | SNDRV_PCM_FMTBIT_FLOAT64_BE,
3052 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
3053 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
3054 };
3055
3056 /* Fix up the DAI formats for endianness: codecs don't actually see
3057 * the endianness of the data but we're using the CPU format
3058 * definitions which do need to include endianness so we ensure that
3059 * codec DAIs always have both big and little endian variants set.
3060 */
3061 static void fixup_codec_formats(struct snd_soc_pcm_stream *stream)
3062 {
3063 int i;
3064
3065 for (i = 0; i < ARRAY_SIZE(codec_format_map); i++)
3066 if (stream->formats & codec_format_map[i])
3067 stream->formats |= codec_format_map[i];
3068 }
3069
3070 /**
3071 * snd_soc_register_codec - Register a codec with the ASoC core
3072 *
3073 * @codec: codec to register
3074 */
3075 int snd_soc_register_codec(struct device *dev,
3076 struct snd_soc_codec_driver *codec_drv,
3077 struct snd_soc_dai_driver *dai_drv, int num_dai)
3078 {
3079 struct snd_soc_codec *codec;
3080 int ret, i;
3081
3082 dev_dbg(dev, "codec register %s\n", dev_name(dev));
3083
3084 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
3085 if (codec == NULL)
3086 return -ENOMEM;
3087
3088 /* create CODEC component name */
3089 codec->name = fmt_single_name(dev, &codec->id);
3090 if (codec->name == NULL) {
3091 kfree(codec);
3092 return -ENOMEM;
3093 }
3094
3095 /* allocate CODEC register cache */
3096 if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
3097
3098 if (codec_drv->reg_cache_default)
3099 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
3100 codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
3101 else
3102 codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
3103 codec_drv->reg_word_size, GFP_KERNEL);
3104
3105 if (codec->reg_cache == NULL) {
3106 kfree(codec->name);
3107 kfree(codec);
3108 return -ENOMEM;
3109 }
3110 }
3111
3112 codec->dev = dev;
3113 codec->driver = codec_drv;
3114 codec->bias_level = SND_SOC_BIAS_OFF;
3115 codec->num_dai = num_dai;
3116 mutex_init(&codec->mutex);
3117 INIT_LIST_HEAD(&codec->dapm_widgets);
3118 INIT_LIST_HEAD(&codec->dapm_paths);
3119
3120 for (i = 0; i < num_dai; i++) {
3121 fixup_codec_formats(&dai_drv[i].playback);
3122 fixup_codec_formats(&dai_drv[i].capture);
3123 }
3124
3125 /* register DAIs */
3126 ret = snd_soc_register_dais(dev, dai_drv, num_dai);
3127 if (ret < 0)
3128 goto error;
3129
3130 mutex_lock(&client_mutex);
3131 list_add(&codec->list, &codec_list);
3132 snd_soc_instantiate_cards();
3133 mutex_unlock(&client_mutex);
3134
3135 pr_debug("Registered codec '%s'\n", codec->name);
3136 return 0;
3137
3138 error:
3139 for (i--; i >= 0; i--)
3140 snd_soc_unregister_dai(dev);
3141
3142 if (codec->reg_cache)
3143 kfree(codec->reg_cache);
3144 kfree(codec->name);
3145 kfree(codec);
3146 return ret;
3147 }
3148 EXPORT_SYMBOL_GPL(snd_soc_register_codec);
3149
3150 /**
3151 * snd_soc_unregister_codec - Unregister a codec from the ASoC core
3152 *
3153 * @codec: codec to unregister
3154 */
3155 void snd_soc_unregister_codec(struct device *dev)
3156 {
3157 struct snd_soc_codec *codec;
3158 int i;
3159
3160 list_for_each_entry(codec, &codec_list, list) {
3161 if (dev == codec->dev)
3162 goto found;
3163 }
3164 return;
3165
3166 found:
3167 for (i = 0; i < codec->num_dai; i++)
3168 snd_soc_unregister_dai(dev);
3169
3170 mutex_lock(&client_mutex);
3171 list_del(&codec->list);
3172 mutex_unlock(&client_mutex);
3173
3174 pr_debug("Unregistered codec '%s'\n", codec->name);
3175
3176 if (codec->reg_cache)
3177 kfree(codec->reg_cache);
3178 kfree(codec);
3179 }
3180 EXPORT_SYMBOL_GPL(snd_soc_unregister_codec);
3181
3182 static int __init snd_soc_init(void)
3183 {
3184 #ifdef CONFIG_DEBUG_FS
3185 debugfs_root = debugfs_create_dir("asoc", NULL);
3186 if (IS_ERR(debugfs_root) || !debugfs_root) {
3187 printk(KERN_WARNING
3188 "ASoC: Failed to create debugfs directory\n");
3189 debugfs_root = NULL;
3190 }
3191 #endif
3192
3193 return platform_driver_register(&soc_driver);
3194 }
3195
3196 static void __exit snd_soc_exit(void)
3197 {
3198 #ifdef CONFIG_DEBUG_FS
3199 debugfs_remove_recursive(debugfs_root);
3200 #endif
3201 platform_driver_unregister(&soc_driver);
3202 }
3203
3204 module_init(snd_soc_init);
3205 module_exit(snd_soc_exit);
3206
3207 /* Module information */
3208 MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
3209 MODULE_DESCRIPTION("ALSA SoC Core");
3210 MODULE_LICENSE("GPL");
3211 MODULE_ALIAS("platform:soc-audio");