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