Kuninori Morimoto | 873486e | 2018-07-02 06:24:18 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // soc-core.c -- ALSA SoC Audio Layer |
| 4 | // |
| 5 | // Copyright 2005 Wolfson Microelectronics PLC. |
| 6 | // Copyright 2005 Openedhand Ltd. |
| 7 | // Copyright (C) 2010 Slimlogic Ltd. |
| 8 | // Copyright (C) 2010 Texas Instruments Inc. |
| 9 | // |
| 10 | // Author: Liam Girdwood <lrg@slimlogic.co.uk> |
| 11 | // with code, comments and ideas from :- |
| 12 | // Richard Purdie <richard@openedhand.com> |
| 13 | // |
| 14 | // TODO: |
| 15 | // o Add hw rules to enforce rates, etc. |
| 16 | // o More testing with other codecs/machines. |
| 17 | // o Add more codecs and platforms to ensure good API coverage. |
| 18 | // o Support TDM on PCM and I2S |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 19 | |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/moduleparam.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/pm.h> |
| 25 | #include <linux/bitops.h> |
Troy Kisky | 12ef193 | 2008-10-13 17:42:14 -0700 | [diff] [blame] | 26 | #include <linux/debugfs.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Markus Pargmann | 741a509 | 2013-08-19 17:05:55 +0200 | [diff] [blame] | 28 | #include <linux/pinctrl/consumer.h> |
Mark Brown | f0e8ed8 | 2011-09-20 11:41:54 +0100 | [diff] [blame] | 29 | #include <linux/ctype.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 31 | #include <linux/of.h> |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 32 | #include <linux/of_graph.h> |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 33 | #include <linux/dmi.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 34 | #include <sound/core.h> |
Mark Brown | 3028eb8 | 2010-12-05 12:22:46 +0000 | [diff] [blame] | 35 | #include <sound/jack.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 36 | #include <sound/pcm.h> |
| 37 | #include <sound/pcm_params.h> |
| 38 | #include <sound/soc.h> |
Liam Girdwood | 01d7584 | 2012-04-25 12:12:49 +0100 | [diff] [blame] | 39 | #include <sound/soc-dpcm.h> |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 40 | #include <sound/soc-topology.h> |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 41 | #include <sound/initval.h> |
| 42 | |
Mark Brown | a8b1d34 | 2010-11-03 18:05:58 -0400 | [diff] [blame] | 43 | #define CREATE_TRACE_POINTS |
| 44 | #include <trace/events/asoc.h> |
| 45 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 46 | #define NAME_SIZE 32 |
| 47 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 48 | static DEFINE_MUTEX(client_mutex); |
Kuninori Morimoto | 030e79f | 2013-03-11 18:27:21 -0700 | [diff] [blame] | 49 | static LIST_HEAD(component_list); |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 50 | static LIST_HEAD(unbind_card_list); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 51 | |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 52 | #define for_each_component(component) \ |
| 53 | list_for_each_entry(component, &component_list, list) |
| 54 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 55 | /* |
Kuninori Morimoto | 587c984 | 2019-06-06 13:07:42 +0900 | [diff] [blame] | 56 | * This is used if driver don't need to have CPU/Codec/Platform |
| 57 | * dai_link. see soc.h |
| 58 | */ |
| 59 | struct snd_soc_dai_link_component null_dailink_component[0]; |
| 60 | EXPORT_SYMBOL_GPL(null_dailink_component); |
| 61 | |
| 62 | /* |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 63 | * This is a timeout to do a DAPM powerdown after a stream is closed(). |
| 64 | * It can be used to eliminate pops between different playback streams, e.g. |
| 65 | * between two audio tracks. |
| 66 | */ |
| 67 | static int pmdown_time = 5000; |
| 68 | module_param(pmdown_time, int, 0); |
| 69 | MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)"); |
| 70 | |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 71 | static ssize_t pmdown_time_show(struct device *dev, |
| 72 | struct device_attribute *attr, char *buf) |
| 73 | { |
Mark Brown | 36ae1a9 | 2012-01-06 17:12:45 -0800 | [diff] [blame] | 74 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 75 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 76 | return sprintf(buf, "%ld\n", rtd->pmdown_time); |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static ssize_t pmdown_time_set(struct device *dev, |
| 80 | struct device_attribute *attr, |
| 81 | const char *buf, size_t count) |
| 82 | { |
Mark Brown | 36ae1a9 | 2012-01-06 17:12:45 -0800 | [diff] [blame] | 83 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
Mark Brown | c593b52 | 2010-10-27 20:11:17 -0700 | [diff] [blame] | 84 | int ret; |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 85 | |
Jingoo Han | b785a49 | 2013-07-19 16:24:59 +0900 | [diff] [blame] | 86 | ret = kstrtol(buf, 10, &rtd->pmdown_time); |
Mark Brown | c593b52 | 2010-10-27 20:11:17 -0700 | [diff] [blame] | 87 | if (ret) |
| 88 | return ret; |
Mark Brown | dbe2140 | 2010-02-12 11:37:24 +0000 | [diff] [blame] | 89 | |
| 90 | return count; |
| 91 | } |
| 92 | |
| 93 | static DEVICE_ATTR(pmdown_time, 0644, pmdown_time_show, pmdown_time_set); |
| 94 | |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 95 | static struct attribute *soc_dev_attrs[] = { |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 96 | &dev_attr_pmdown_time.attr, |
| 97 | NULL |
| 98 | }; |
| 99 | |
| 100 | static umode_t soc_dev_attr_is_visible(struct kobject *kobj, |
| 101 | struct attribute *attr, int idx) |
| 102 | { |
| 103 | struct device *dev = kobj_to_dev(kobj); |
| 104 | struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev); |
| 105 | |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 106 | if (!rtd) |
| 107 | return 0; |
| 108 | |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 109 | if (attr == &dev_attr_pmdown_time.attr) |
| 110 | return attr->mode; /* always visible */ |
Kuninori Morimoto | 3b6eed8d | 2017-12-05 04:20:42 +0000 | [diff] [blame] | 111 | return rtd->num_codecs ? attr->mode : 0; /* enabled only with codec */ |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static const struct attribute_group soc_dapm_dev_group = { |
| 115 | .attrs = soc_dapm_dev_attrs, |
| 116 | .is_visible = soc_dev_attr_is_visible, |
| 117 | }; |
| 118 | |
Mark Brown | f7e73b26 | 2018-03-09 12:46:27 +0000 | [diff] [blame] | 119 | static const struct attribute_group soc_dev_group = { |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 120 | .attrs = soc_dev_attrs, |
| 121 | .is_visible = soc_dev_attr_is_visible, |
| 122 | }; |
| 123 | |
| 124 | static const struct attribute_group *soc_dev_attr_groups[] = { |
| 125 | &soc_dapm_dev_group, |
Mark Brown | f7e73b26 | 2018-03-09 12:46:27 +0000 | [diff] [blame] | 126 | &soc_dev_group, |
Takashi Iwai | d29697d | 2015-01-30 20:16:37 +0100 | [diff] [blame] | 127 | NULL |
| 128 | }; |
| 129 | |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_DEBUG_FS |
Kuninori Morimoto | a4072cd | 2019-12-11 13:32:20 +0900 | [diff] [blame] | 131 | struct dentry *snd_soc_debugfs_root; |
| 132 | EXPORT_SYMBOL_GPL(snd_soc_debugfs_root); |
| 133 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 134 | static void soc_init_component_debugfs(struct snd_soc_component *component) |
Russell King | e73f3de | 2014-06-26 15:22:50 +0100 | [diff] [blame] | 135 | { |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 136 | if (!component->card->debugfs_card_root) |
| 137 | return; |
| 138 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 139 | if (component->debugfs_prefix) { |
| 140 | char *name; |
Russell King | e73f3de | 2014-06-26 15:22:50 +0100 | [diff] [blame] | 141 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 142 | name = kasprintf(GFP_KERNEL, "%s:%s", |
| 143 | component->debugfs_prefix, component->name); |
| 144 | if (name) { |
| 145 | component->debugfs_root = debugfs_create_dir(name, |
| 146 | component->card->debugfs_card_root); |
| 147 | kfree(name); |
| 148 | } |
| 149 | } else { |
| 150 | component->debugfs_root = debugfs_create_dir(component->name, |
| 151 | component->card->debugfs_card_root); |
| 152 | } |
Russell King | e73f3de | 2014-06-26 15:22:50 +0100 | [diff] [blame] | 153 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 154 | snd_soc_dapm_debugfs_init(snd_soc_component_get_dapm(component), |
| 155 | component->debugfs_root); |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void soc_cleanup_component_debugfs(struct snd_soc_component *component) |
| 159 | { |
Kuninori Morimoto | ad64bfb | 2019-08-07 10:30:13 +0900 | [diff] [blame] | 160 | if (!component->debugfs_root) |
| 161 | return; |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 162 | debugfs_remove_recursive(component->debugfs_root); |
Kuninori Morimoto | ad64bfb | 2019-08-07 10:30:13 +0900 | [diff] [blame] | 163 | component->debugfs_root = NULL; |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Peng Donglin | c15b2a1 | 2018-02-14 22:48:07 +0800 | [diff] [blame] | 166 | static int dai_list_show(struct seq_file *m, void *v) |
Mark Brown | f320878 | 2010-09-15 18:19:07 +0100 | [diff] [blame] | 167 | { |
Lars-Peter Clausen | 1438c2f | 2014-03-09 17:41:47 +0100 | [diff] [blame] | 168 | struct snd_soc_component *component; |
Mark Brown | f320878 | 2010-09-15 18:19:07 +0100 | [diff] [blame] | 169 | struct snd_soc_dai *dai; |
| 170 | |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 171 | mutex_lock(&client_mutex); |
| 172 | |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 173 | for_each_component(component) |
Kuninori Morimoto | 15a0c64 | 2018-09-21 05:23:17 +0000 | [diff] [blame] | 174 | for_each_component_dais(component, dai) |
Donglin Peng | 700c17c | 2018-01-18 13:31:26 +0800 | [diff] [blame] | 175 | seq_printf(m, "%s\n", dai->name); |
Mark Brown | f320878 | 2010-09-15 18:19:07 +0100 | [diff] [blame] | 176 | |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 177 | mutex_unlock(&client_mutex); |
| 178 | |
Donglin Peng | 700c17c | 2018-01-18 13:31:26 +0800 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Peng Donglin | c15b2a1 | 2018-02-14 22:48:07 +0800 | [diff] [blame] | 181 | DEFINE_SHOW_ATTRIBUTE(dai_list); |
Mark Brown | f320878 | 2010-09-15 18:19:07 +0100 | [diff] [blame] | 182 | |
Kuninori Morimoto | db795f9 | 2018-05-08 03:21:00 +0000 | [diff] [blame] | 183 | static int component_list_show(struct seq_file *m, void *v) |
| 184 | { |
| 185 | struct snd_soc_component *component; |
| 186 | |
| 187 | mutex_lock(&client_mutex); |
| 188 | |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 189 | for_each_component(component) |
Kuninori Morimoto | db795f9 | 2018-05-08 03:21:00 +0000 | [diff] [blame] | 190 | seq_printf(m, "%s\n", component->name); |
| 191 | |
| 192 | mutex_unlock(&client_mutex); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | DEFINE_SHOW_ATTRIBUTE(component_list); |
| 197 | |
Jarkko Nikula | a605215 | 2010-11-05 20:35:19 +0200 | [diff] [blame] | 198 | static void soc_init_card_debugfs(struct snd_soc_card *card) |
| 199 | { |
| 200 | card->debugfs_card_root = debugfs_create_dir(card->name, |
Mark Brown | 8a9dab1 | 2011-01-10 22:25:21 +0000 | [diff] [blame] | 201 | snd_soc_debugfs_root); |
Jarkko Nikula | 3a45b86 | 2010-11-05 20:35:21 +0200 | [diff] [blame] | 202 | |
Greg Kroah-Hartman | fee531d | 2019-07-31 15:17:15 +0200 | [diff] [blame] | 203 | debugfs_create_u32("dapm_pop_time", 0644, card->debugfs_card_root, |
| 204 | &card->pop_time); |
Kuninori Morimoto | d8ca7a0 | 2019-08-07 10:31:14 +0900 | [diff] [blame] | 205 | |
| 206 | snd_soc_dapm_debugfs_init(&card->dapm, card->debugfs_card_root); |
Jarkko Nikula | a605215 | 2010-11-05 20:35:19 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void soc_cleanup_card_debugfs(struct snd_soc_card *card) |
| 210 | { |
| 211 | debugfs_remove_recursive(card->debugfs_card_root); |
Kuninori Morimoto | 29040d1 | 2019-05-27 16:51:34 +0900 | [diff] [blame] | 212 | card->debugfs_card_root = NULL; |
Jarkko Nikula | a605215 | 2010-11-05 20:35:19 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 215 | static void snd_soc_debugfs_init(void) |
| 216 | { |
| 217 | snd_soc_debugfs_root = debugfs_create_dir("asoc", NULL); |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 218 | |
Greg Kroah-Hartman | fee531d | 2019-07-31 15:17:15 +0200 | [diff] [blame] | 219 | debugfs_create_file("dais", 0444, snd_soc_debugfs_root, NULL, |
| 220 | &dai_list_fops); |
Kuninori Morimoto | db795f9 | 2018-05-08 03:21:00 +0000 | [diff] [blame] | 221 | |
Greg Kroah-Hartman | fee531d | 2019-07-31 15:17:15 +0200 | [diff] [blame] | 222 | debugfs_create_file("components", 0444, snd_soc_debugfs_root, NULL, |
| 223 | &component_list_fops); |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static void snd_soc_debugfs_exit(void) |
| 227 | { |
| 228 | debugfs_remove_recursive(snd_soc_debugfs_root); |
| 229 | } |
| 230 | |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 231 | #else |
| 232 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 233 | static inline void soc_init_component_debugfs( |
| 234 | struct snd_soc_component *component) |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 235 | { |
| 236 | } |
| 237 | |
Lars-Peter Clausen | 81c7cfd | 2014-08-19 15:51:18 +0200 | [diff] [blame] | 238 | static inline void soc_cleanup_component_debugfs( |
| 239 | struct snd_soc_component *component) |
Sebastien Guiriec | 731f1ab | 2012-02-15 15:25:31 +0000 | [diff] [blame] | 240 | { |
| 241 | } |
| 242 | |
Axel Lin | b95fccb | 2010-11-09 17:06:44 +0800 | [diff] [blame] | 243 | static inline void soc_init_card_debugfs(struct snd_soc_card *card) |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | static inline void soc_cleanup_card_debugfs(struct snd_soc_card *card) |
| 248 | { |
| 249 | } |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 250 | |
| 251 | static inline void snd_soc_debugfs_init(void) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | static inline void snd_soc_debugfs_exit(void) |
| 256 | { |
| 257 | } |
| 258 | |
Mark Brown | 2624d5f | 2009-11-03 21:56:13 +0000 | [diff] [blame] | 259 | #endif |
| 260 | |
Kuninori Morimoto | 12b0523 | 2020-01-10 11:35:54 +0900 | [diff] [blame] | 261 | static int snd_soc_rtd_add_component(struct snd_soc_pcm_runtime *rtd, |
| 262 | struct snd_soc_component *component) |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 263 | { |
Kuninori Morimoto | 2b544dd | 2019-10-15 12:59:31 +0900 | [diff] [blame] | 264 | struct snd_soc_component *comp; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 265 | int i; |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 266 | |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 267 | for_each_rtd_components(rtd, i, comp) { |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 268 | /* already connected */ |
Kuninori Morimoto | 2b544dd | 2019-10-15 12:59:31 +0900 | [diff] [blame] | 269 | if (comp == component) |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 273 | /* see for_each_rtd_components */ |
| 274 | rtd->components[rtd->num_components] = component; |
| 275 | rtd->num_components++; |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 280 | struct snd_soc_component *snd_soc_rtdcom_lookup(struct snd_soc_pcm_runtime *rtd, |
| 281 | const char *driver_name) |
| 282 | { |
Kuninori Morimoto | 2b544dd | 2019-10-15 12:59:31 +0900 | [diff] [blame] | 283 | struct snd_soc_component *component; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 284 | int i; |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 285 | |
Kuninori Morimoto | 971da24 | 2018-01-23 00:41:24 +0000 | [diff] [blame] | 286 | if (!driver_name) |
| 287 | return NULL; |
| 288 | |
Kuninori Morimoto | a33c0d1 | 2019-08-20 14:05:02 +0900 | [diff] [blame] | 289 | /* |
| 290 | * NOTE |
| 291 | * |
| 292 | * snd_soc_rtdcom_lookup() will find component from rtd by using |
| 293 | * specified driver name. |
| 294 | * But, if many components which have same driver name are connected |
| 295 | * to 1 rtd, this function will return 1st found component. |
| 296 | */ |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 297 | for_each_rtd_components(rtd, i, component) { |
Kuninori Morimoto | 2b544dd | 2019-10-15 12:59:31 +0900 | [diff] [blame] | 298 | const char *component_name = component->driver->name; |
Kuninori Morimoto | 971da24 | 2018-01-23 00:41:24 +0000 | [diff] [blame] | 299 | |
| 300 | if (!component_name) |
| 301 | continue; |
| 302 | |
| 303 | if ((component_name == driver_name) || |
| 304 | strcmp(component_name, driver_name) == 0) |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 305 | return component; |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return NULL; |
| 309 | } |
Kuninori Morimoto | 031734b | 2018-01-18 01:13:54 +0000 | [diff] [blame] | 310 | EXPORT_SYMBOL_GPL(snd_soc_rtdcom_lookup); |
Kuninori Morimoto | a0ac441 | 2017-08-08 06:17:47 +0000 | [diff] [blame] | 311 | |
Kuninori Morimoto | 18dd66e | 2019-11-06 16:05:05 +0900 | [diff] [blame] | 312 | static struct snd_soc_component |
| 313 | *snd_soc_lookup_component_nolocked(struct device *dev, const char *driver_name) |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 314 | { |
| 315 | struct snd_soc_component *component; |
Kuninori Morimoto | 5bd7e08 | 2019-11-05 15:46:35 +0900 | [diff] [blame] | 316 | struct snd_soc_component *found_component; |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 317 | |
Kuninori Morimoto | 5bd7e08 | 2019-11-05 15:46:35 +0900 | [diff] [blame] | 318 | found_component = NULL; |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 319 | for_each_component(component) { |
Kuninori Morimoto | 5bd7e08 | 2019-11-05 15:46:35 +0900 | [diff] [blame] | 320 | if ((dev == component->dev) && |
| 321 | (!driver_name || |
| 322 | (driver_name == component->driver->name) || |
| 323 | (strcmp(component->driver->name, driver_name) == 0))) { |
| 324 | found_component = component; |
| 325 | break; |
| 326 | } |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 327 | } |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 328 | |
Kuninori Morimoto | 5bd7e08 | 2019-11-05 15:46:35 +0900 | [diff] [blame] | 329 | return found_component; |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 330 | } |
Kuninori Morimoto | 18dd66e | 2019-11-06 16:05:05 +0900 | [diff] [blame] | 331 | |
| 332 | struct snd_soc_component *snd_soc_lookup_component(struct device *dev, |
| 333 | const char *driver_name) |
| 334 | { |
| 335 | struct snd_soc_component *component; |
| 336 | |
| 337 | mutex_lock(&client_mutex); |
| 338 | component = snd_soc_lookup_component_nolocked(dev, driver_name); |
| 339 | mutex_unlock(&client_mutex); |
| 340 | |
| 341 | return component; |
| 342 | } |
Kuninori Morimoto | b813265 | 2019-11-05 15:46:30 +0900 | [diff] [blame] | 343 | EXPORT_SYMBOL_GPL(snd_soc_lookup_component); |
| 344 | |
Kuninori Morimoto | 94def8e | 2019-12-10 09:34:01 +0900 | [diff] [blame] | 345 | struct snd_soc_pcm_runtime |
| 346 | *snd_soc_get_pcm_runtime(struct snd_soc_card *card, |
Kuninori Morimoto | 4468189 | 2019-12-10 09:34:08 +0900 | [diff] [blame] | 347 | struct snd_soc_dai_link *dai_link) |
Kuninori Morimoto | 94def8e | 2019-12-10 09:34:01 +0900 | [diff] [blame] | 348 | { |
| 349 | struct snd_soc_pcm_runtime *rtd; |
| 350 | |
| 351 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 4468189 | 2019-12-10 09:34:08 +0900 | [diff] [blame] | 352 | if (rtd->dai_link == dai_link) |
Kuninori Morimoto | 94def8e | 2019-12-10 09:34:01 +0900 | [diff] [blame] | 353 | return rtd; |
| 354 | } |
Kuninori Morimoto | 4468189 | 2019-12-10 09:34:08 +0900 | [diff] [blame] | 355 | dev_dbg(card->dev, "ASoC: failed to find rtd %s\n", dai_link->name); |
Kuninori Morimoto | 94def8e | 2019-12-10 09:34:01 +0900 | [diff] [blame] | 356 | return NULL; |
| 357 | } |
| 358 | EXPORT_SYMBOL_GPL(snd_soc_get_pcm_runtime); |
| 359 | |
Kuninori Morimoto | 83f94a2 | 2020-01-10 11:36:17 +0900 | [diff] [blame] | 360 | /* |
| 361 | * Power down the audio subsystem pmdown_time msecs after close is called. |
| 362 | * This is to ensure there are no pops or clicks in between any music tracks |
| 363 | * due to DAPM power cycling. |
| 364 | */ |
| 365 | void snd_soc_close_delayed_work(struct snd_soc_pcm_runtime *rtd) |
| 366 | { |
| 367 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 368 | |
| 369 | mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); |
| 370 | |
| 371 | dev_dbg(rtd->dev, |
| 372 | "ASoC: pop wq checking: %s status: %s waiting: %s\n", |
| 373 | codec_dai->driver->playback.stream_name, |
| 374 | codec_dai->playback_active ? "active" : "inactive", |
| 375 | rtd->pop_wait ? "yes" : "no"); |
| 376 | |
| 377 | /* are we waiting on this codec DAI stream */ |
| 378 | if (rtd->pop_wait == 1) { |
| 379 | rtd->pop_wait = 0; |
| 380 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
| 381 | SND_SOC_DAPM_STREAM_STOP); |
| 382 | } |
| 383 | |
| 384 | mutex_unlock(&rtd->card->pcm_mutex); |
| 385 | } |
| 386 | EXPORT_SYMBOL_GPL(snd_soc_close_delayed_work); |
| 387 | |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 388 | static void soc_release_rtd_dev(struct device *dev) |
| 389 | { |
| 390 | /* "dev" means "rtd->dev" */ |
| 391 | kfree(dev); |
| 392 | } |
| 393 | |
Kuninori Morimoto | 1c93a9e | 2019-09-12 13:38:22 +0900 | [diff] [blame] | 394 | static void soc_free_pcm_runtime(struct snd_soc_pcm_runtime *rtd) |
| 395 | { |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 396 | if (!rtd) |
| 397 | return; |
| 398 | |
Kuninori Morimoto | 753ace0 | 2019-09-12 13:38:50 +0900 | [diff] [blame] | 399 | list_del(&rtd->list); |
Kuninori Morimoto | b7c5bc4 | 2019-09-12 13:41:01 +0900 | [diff] [blame] | 400 | |
Curtis Malainey | 9c9b652 | 2019-11-27 17:13:58 -0800 | [diff] [blame] | 401 | if (delayed_work_pending(&rtd->delayed_work)) |
| 402 | flush_delayed_work(&rtd->delayed_work); |
Kuninori Morimoto | 0ced7b0 | 2019-11-18 10:51:11 +0900 | [diff] [blame] | 403 | snd_soc_pcm_component_free(rtd); |
| 404 | |
Kuninori Morimoto | b7c5bc4 | 2019-09-12 13:41:01 +0900 | [diff] [blame] | 405 | /* |
| 406 | * we don't need to call kfree() for rtd->dev |
| 407 | * see |
| 408 | * soc_release_rtd_dev() |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 409 | * |
| 410 | * We don't need rtd->dev NULL check, because |
| 411 | * it is alloced *before* rtd. |
| 412 | * see |
| 413 | * soc_new_pcm_runtime() |
Kuninori Morimoto | b7c5bc4 | 2019-09-12 13:41:01 +0900 | [diff] [blame] | 414 | */ |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 415 | device_unregister(rtd->dev); |
Kuninori Morimoto | 1c93a9e | 2019-09-12 13:38:22 +0900 | [diff] [blame] | 416 | } |
| 417 | |
Curtis Malainey | 4bf2e38 | 2019-12-03 09:30:07 -0800 | [diff] [blame] | 418 | static void close_delayed_work(struct work_struct *work) { |
| 419 | struct snd_soc_pcm_runtime *rtd = |
| 420 | container_of(work, struct snd_soc_pcm_runtime, |
| 421 | delayed_work.work); |
| 422 | |
| 423 | if (rtd->close_delayed_work_func) |
| 424 | rtd->close_delayed_work_func(rtd); |
| 425 | } |
| 426 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 427 | static struct snd_soc_pcm_runtime *soc_new_pcm_runtime( |
| 428 | struct snd_soc_card *card, struct snd_soc_dai_link *dai_link) |
| 429 | { |
| 430 | struct snd_soc_pcm_runtime *rtd; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 431 | struct snd_soc_component *component; |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 432 | struct device *dev; |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 433 | int ret; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 434 | |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 435 | /* |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 436 | * for rtd->dev |
| 437 | */ |
| 438 | dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 439 | if (!dev) |
| 440 | return NULL; |
| 441 | |
| 442 | dev->parent = card->dev; |
| 443 | dev->release = soc_release_rtd_dev; |
| 444 | dev->groups = soc_dev_attr_groups; |
| 445 | |
| 446 | dev_set_name(dev, "%s", dai_link->name); |
| 447 | |
| 448 | ret = device_register(dev); |
| 449 | if (ret < 0) { |
| 450 | put_device(dev); /* soc_release_rtd_dev */ |
| 451 | return NULL; |
| 452 | } |
| 453 | |
| 454 | /* |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 455 | * for rtd |
| 456 | */ |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 457 | rtd = devm_kzalloc(dev, |
| 458 | sizeof(*rtd) + |
| 459 | sizeof(*component) * (dai_link->num_cpus + |
| 460 | dai_link->num_codecs + |
| 461 | dai_link->num_platforms), |
| 462 | GFP_KERNEL); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 463 | if (!rtd) |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 464 | goto free_rtd; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 465 | |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 466 | rtd->dev = dev; |
Takashi Iwai | 07d22a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 467 | INIT_LIST_HEAD(&rtd->list); |
Takashi Iwai | 07d22a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 468 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].be_clients); |
| 469 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].be_clients); |
| 470 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_PLAYBACK].fe_clients); |
| 471 | INIT_LIST_HEAD(&rtd->dpcm[SNDRV_PCM_STREAM_CAPTURE].fe_clients); |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 472 | dev_set_drvdata(dev, rtd); |
Curtis Malainey | 4bf2e38 | 2019-12-03 09:30:07 -0800 | [diff] [blame] | 473 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
Kuninori Morimoto | d918a37 | 2019-09-12 13:42:30 +0900 | [diff] [blame] | 474 | |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 475 | /* |
| 476 | * for rtd->codec_dais |
| 477 | */ |
Kuninori Morimoto | 4dc0e7d | 2019-10-02 14:22:32 +0900 | [diff] [blame] | 478 | rtd->codec_dais = devm_kcalloc(dev, dai_link->num_codecs, |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 479 | sizeof(struct snd_soc_dai *), |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 480 | GFP_KERNEL); |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 481 | if (!rtd->codec_dais) |
| 482 | goto free_rtd; |
| 483 | |
| 484 | /* |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 485 | * rtd remaining settings |
| 486 | */ |
Kuninori Morimoto | 929deb8 | 2019-09-12 13:39:32 +0900 | [diff] [blame] | 487 | rtd->card = card; |
| 488 | rtd->dai_link = dai_link; |
Kuninori Morimoto | 929deb8 | 2019-09-12 13:39:32 +0900 | [diff] [blame] | 489 | |
Kuninori Morimoto | 6634e3d | 2019-08-07 10:30:31 +0900 | [diff] [blame] | 490 | /* see for_each_card_rtds */ |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 491 | list_add_tail(&rtd->list, &card->rtd_list); |
| 492 | rtd->num = card->num_rtd; |
| 493 | card->num_rtd++; |
Kuninori Morimoto | a848125 | 2019-09-12 13:38:34 +0900 | [diff] [blame] | 494 | |
| 495 | return rtd; |
Kuninori Morimoto | 6e86434 | 2019-09-12 13:40:08 +0900 | [diff] [blame] | 496 | |
| 497 | free_rtd: |
| 498 | soc_free_pcm_runtime(rtd); |
| 499 | return NULL; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 500 | } |
| 501 | |
Kuninori Morimoto | 65462e44 | 2019-01-21 09:32:37 +0900 | [diff] [blame] | 502 | static void snd_soc_flush_all_delayed_work(struct snd_soc_card *card) |
| 503 | { |
| 504 | struct snd_soc_pcm_runtime *rtd; |
| 505 | |
| 506 | for_each_card_rtds(card, rtd) |
| 507 | flush_delayed_work(&rtd->delayed_work); |
| 508 | } |
| 509 | |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 510 | #ifdef CONFIG_PM_SLEEP |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 511 | /* powers down audio subsystem for suspend */ |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 512 | int snd_soc_suspend(struct device *dev) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 513 | { |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 514 | struct snd_soc_card *card = dev_get_drvdata(dev); |
Kuninori Morimoto | d9fc406 | 2016-11-30 06:22:36 +0000 | [diff] [blame] | 515 | struct snd_soc_component *component; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 516 | struct snd_soc_pcm_runtime *rtd; |
| 517 | int i; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 518 | |
Lars-Peter Clausen | c5599b8 | 2014-08-19 15:51:30 +0200 | [diff] [blame] | 519 | /* If the card is not initialized yet there is nothing to do */ |
| 520 | if (!card->instantiated) |
Daniel Mack | e3509ff | 2009-06-03 17:44:49 +0200 | [diff] [blame] | 521 | return 0; |
| 522 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 523 | /* |
| 524 | * Due to the resume being scheduled into a workqueue we could |
| 525 | * suspend before that's finished - wait for it to complete. |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 526 | */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 527 | snd_power_wait(card->snd_card, SNDRV_CTL_POWER_D0); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 528 | |
| 529 | /* we're going to block userspace touching us until resume completes */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 530 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D3hot); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 531 | |
Mark Brown | a00f90f | 2010-12-02 16:24:24 +0000 | [diff] [blame] | 532 | /* mute any active DACs */ |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 533 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 534 | struct snd_soc_dai *dai; |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 535 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 536 | if (rtd->dai_link->ignore_suspend) |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 537 | continue; |
| 538 | |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 539 | for_each_rtd_codec_dai(rtd, i, dai) { |
Kuninori Morimoto | 88fdffa | 2019-07-22 10:36:27 +0900 | [diff] [blame] | 540 | if (dai->playback_active) |
| 541 | snd_soc_dai_digital_mute(dai, 1, |
| 542 | SNDRV_PCM_STREAM_PLAYBACK); |
Benoit Cousson | 88bd870 | 2014-07-08 23:19:34 +0200 | [diff] [blame] | 543 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Liam Girdwood | 4ccab3e | 2008-01-10 14:39:01 +0100 | [diff] [blame] | 546 | /* suspend all pcms */ |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 547 | for_each_card_rtds(card, rtd) { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 548 | if (rtd->dai_link->ignore_suspend) |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 549 | continue; |
| 550 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 551 | snd_pcm_suspend_all(rtd->pcm); |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 552 | } |
Liam Girdwood | 4ccab3e | 2008-01-10 14:39:01 +0100 | [diff] [blame] | 553 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 554 | if (card->suspend_pre) |
Mark Brown | 70b2ac1 | 2011-01-26 14:05:25 +0000 | [diff] [blame] | 555 | card->suspend_pre(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 556 | |
Lars-Peter Clausen | 37660b6 | 2015-03-30 21:04:50 +0200 | [diff] [blame] | 557 | /* close any waiting streams */ |
Kuninori Morimoto | 65462e44 | 2019-01-21 09:32:37 +0900 | [diff] [blame] | 558 | snd_soc_flush_all_delayed_work(card); |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 559 | |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 560 | for_each_card_rtds(card, rtd) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 561 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 562 | if (rtd->dai_link->ignore_suspend) |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 563 | continue; |
| 564 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 565 | snd_soc_dapm_stream_event(rtd, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 566 | SNDRV_PCM_STREAM_PLAYBACK, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 567 | SND_SOC_DAPM_STREAM_SUSPEND); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 568 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 569 | snd_soc_dapm_stream_event(rtd, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 570 | SNDRV_PCM_STREAM_CAPTURE, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 571 | SND_SOC_DAPM_STREAM_SUSPEND); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Lars-Peter Clausen | 8be4da2 | 2014-10-25 17:42:01 +0200 | [diff] [blame] | 574 | /* Recheck all endpoints too, their state is affected by suspend */ |
| 575 | dapm_mark_endpoints_dirty(card); |
Mark Brown | e2d32ff | 2012-08-31 17:38:32 -0700 | [diff] [blame] | 576 | snd_soc_dapm_sync(&card->dapm); |
| 577 | |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 578 | /* suspend all COMPONENTs */ |
Kuninori Morimoto | 1272063 | 2020-01-10 11:36:02 +0900 | [diff] [blame] | 579 | for_each_card_rtds(card, rtd) { |
| 580 | |
| 581 | if (rtd->dai_link->ignore_suspend) |
| 582 | continue; |
| 583 | |
| 584 | for_each_rtd_components(rtd, i, component) { |
| 585 | struct snd_soc_dapm_context *dapm = |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 586 | snd_soc_component_get_dapm(component); |
Kuninori Morimoto | d9fc406 | 2016-11-30 06:22:36 +0000 | [diff] [blame] | 587 | |
Kuninori Morimoto | 1272063 | 2020-01-10 11:36:02 +0900 | [diff] [blame] | 588 | /* |
| 589 | * ignore if component was already suspended |
| 590 | */ |
| 591 | if (snd_soc_component_is_suspended(component)) |
| 592 | continue; |
| 593 | |
| 594 | /* |
| 595 | * If there are paths active then the COMPONENT will be |
| 596 | * held with bias _ON and should not be suspended. |
| 597 | */ |
Lars-Peter Clausen | 4890140 | 2015-07-06 15:38:11 +0200 | [diff] [blame] | 598 | switch (snd_soc_dapm_get_bias_level(dapm)) { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 599 | case SND_SOC_BIAS_STANDBY: |
Mark Brown | 125a25d | 2012-01-31 15:49:10 +0000 | [diff] [blame] | 600 | /* |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 601 | * If the COMPONENT is capable of idle |
Mark Brown | 125a25d | 2012-01-31 15:49:10 +0000 | [diff] [blame] | 602 | * bias off then being in STANDBY |
| 603 | * means it's doing something, |
| 604 | * otherwise fall through. |
| 605 | */ |
Lars-Peter Clausen | 4890140 | 2015-07-06 15:38:11 +0200 | [diff] [blame] | 606 | if (dapm->idle_bias_off) { |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 607 | dev_dbg(component->dev, |
Michał Mirosław | 10e8aa9 | 2013-05-04 22:21:38 +0200 | [diff] [blame] | 608 | "ASoC: idle_bias_off CODEC on over suspend\n"); |
Mark Brown | 125a25d | 2012-01-31 15:49:10 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
Gustavo A. R. Silva | 1a12d5d | 2018-08-03 11:34:30 -0500 | [diff] [blame] | 611 | /* fall through */ |
Lars-Peter Clausen | a809329 | 2014-09-04 19:44:07 +0200 | [diff] [blame] | 612 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 613 | case SND_SOC_BIAS_OFF: |
Kuninori Morimoto | 66c5157 | 2019-07-26 13:50:34 +0900 | [diff] [blame] | 614 | snd_soc_component_suspend(component); |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 615 | if (component->regmap) |
| 616 | regcache_mark_dirty(component->regmap); |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 617 | /* deactivate pins to sleep state */ |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 618 | pinctrl_pm_select_sleep_state(component->dev); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 619 | break; |
| 620 | default: |
Kuninori Morimoto | 9178feb | 2016-11-30 06:23:13 +0000 | [diff] [blame] | 621 | dev_dbg(component->dev, |
| 622 | "ASoC: COMPONENT is on over suspend\n"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 628 | if (card->suspend_post) |
Mark Brown | 70b2ac1 | 2011-01-26 14:05:25 +0000 | [diff] [blame] | 629 | card->suspend_post(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 630 | |
| 631 | return 0; |
| 632 | } |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 633 | EXPORT_SYMBOL_GPL(snd_soc_suspend); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 634 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 635 | /* |
| 636 | * deferred resume work, so resume can complete before we finished |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 637 | * setting our codec back up, which can be very slow on I2C |
| 638 | */ |
| 639 | static void soc_resume_deferred(struct work_struct *work) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 640 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 641 | struct snd_soc_card *card = |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 642 | container_of(work, struct snd_soc_card, |
| 643 | deferred_resume_work); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 644 | struct snd_soc_pcm_runtime *rtd; |
Kuninori Morimoto | d9fc406 | 2016-11-30 06:22:36 +0000 | [diff] [blame] | 645 | struct snd_soc_component *component; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 646 | int i; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 647 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 648 | /* |
| 649 | * our power state is still SNDRV_CTL_POWER_D3hot from suspend time, |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 650 | * so userspace apps are blocked from touching us |
| 651 | */ |
| 652 | |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 653 | dev_dbg(card->dev, "ASoC: starting resume work\n"); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 654 | |
Mark Brown | 9949788b | 2010-05-07 20:24:05 +0100 | [diff] [blame] | 655 | /* Bring us up into D2 so that DAPM starts enabling things */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 656 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D2); |
Mark Brown | 9949788b | 2010-05-07 20:24:05 +0100 | [diff] [blame] | 657 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 658 | if (card->resume_pre) |
Mark Brown | 70b2ac1 | 2011-01-26 14:05:25 +0000 | [diff] [blame] | 659 | card->resume_pre(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 660 | |
Kuninori Morimoto | f70f18f7 | 2018-09-18 01:29:55 +0000 | [diff] [blame] | 661 | for_each_card_components(card, component) { |
Kuninori Morimoto | e40fadb | 2019-07-26 13:51:13 +0900 | [diff] [blame] | 662 | if (snd_soc_component_is_suspended(component)) |
Kuninori Morimoto | 9a840cb | 2019-07-26 13:51:08 +0900 | [diff] [blame] | 663 | snd_soc_component_resume(component); |
Mark Brown | 1547aba | 2010-05-07 21:11:40 +0100 | [diff] [blame] | 664 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 665 | |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 666 | for_each_card_rtds(card, rtd) { |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 667 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 668 | if (rtd->dai_link->ignore_suspend) |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 669 | continue; |
| 670 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 671 | snd_soc_dapm_stream_event(rtd, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 672 | SNDRV_PCM_STREAM_PLAYBACK, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 673 | SND_SOC_DAPM_STREAM_RESUME); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 674 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 675 | snd_soc_dapm_stream_event(rtd, |
Liam Girdwood | d9b0951 | 2012-03-07 16:32:59 +0000 | [diff] [blame] | 676 | SNDRV_PCM_STREAM_CAPTURE, |
Mark Brown | 7bd3a6f | 2012-02-16 15:03:27 -0800 | [diff] [blame] | 677 | SND_SOC_DAPM_STREAM_RESUME); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 678 | } |
| 679 | |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 680 | /* unmute any active DACs */ |
Kuninori Morimoto | bcb1fd1 | 2018-09-18 01:29:35 +0000 | [diff] [blame] | 681 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 682 | struct snd_soc_dai *dai; |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 683 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 684 | if (rtd->dai_link->ignore_suspend) |
Mark Brown | 3efab7d | 2010-05-09 13:25:43 +0100 | [diff] [blame] | 685 | continue; |
| 686 | |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 687 | for_each_rtd_codec_dai(rtd, i, dai) { |
Kuninori Morimoto | 88fdffa | 2019-07-22 10:36:27 +0900 | [diff] [blame] | 688 | if (dai->playback_active) |
| 689 | snd_soc_dai_digital_mute(dai, 0, |
| 690 | SNDRV_PCM_STREAM_PLAYBACK); |
Benoit Cousson | 88bd870 | 2014-07-08 23:19:34 +0200 | [diff] [blame] | 691 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 692 | } |
| 693 | |
Mark Brown | 8750654 | 2008-11-18 20:50:34 +0000 | [diff] [blame] | 694 | if (card->resume_post) |
Mark Brown | 70b2ac1 | 2011-01-26 14:05:25 +0000 | [diff] [blame] | 695 | card->resume_post(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 696 | |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 697 | dev_dbg(card->dev, "ASoC: resume work completed\n"); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 698 | |
Lars-Peter Clausen | 8be4da2 | 2014-10-25 17:42:01 +0200 | [diff] [blame] | 699 | /* Recheck all endpoints too, their state is affected by suspend */ |
| 700 | dapm_mark_endpoints_dirty(card); |
Mark Brown | e2d32ff | 2012-08-31 17:38:32 -0700 | [diff] [blame] | 701 | snd_soc_dapm_sync(&card->dapm); |
Jeeja KP | 1a7aaa5 | 2015-11-23 21:22:31 +0530 | [diff] [blame] | 702 | |
| 703 | /* userspace can access us now we are back as we were before */ |
| 704 | snd_power_change_state(card->snd_card, SNDRV_CTL_POWER_D0); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /* powers up audio subsystem after a suspend */ |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 708 | int snd_soc_resume(struct device *dev) |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 709 | { |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 710 | struct snd_soc_card *card = dev_get_drvdata(dev); |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 711 | struct snd_soc_component *component; |
Peter Ujfalusi | b9dd94a | 2010-02-22 13:27:13 +0200 | [diff] [blame] | 712 | |
Lars-Peter Clausen | c5599b8 | 2014-08-19 15:51:30 +0200 | [diff] [blame] | 713 | /* If the card is not initialized yet there is nothing to do */ |
| 714 | if (!card->instantiated) |
Eric Miao | 5ff1ddf | 2011-11-23 22:37:00 +0800 | [diff] [blame] | 715 | return 0; |
| 716 | |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 717 | /* activate pins from sleep state */ |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 718 | for_each_card_components(card, component) |
| 719 | if (component->active) |
| 720 | pinctrl_pm_select_default_state(component->dev); |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 721 | |
Kuninori Morimoto | 250a15c | 2020-01-20 10:05:07 +0900 | [diff] [blame] | 722 | dev_dbg(dev, "ASoC: Scheduling resume work\n"); |
| 723 | if (!schedule_work(&card->deferred_resume_work)) |
| 724 | dev_err(dev, "ASoC: resume work item may be lost\n"); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 725 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 726 | return 0; |
| 727 | } |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 728 | EXPORT_SYMBOL_GPL(snd_soc_resume); |
Kuninori Morimoto | b3da425 | 2019-08-07 10:31:24 +0900 | [diff] [blame] | 729 | |
| 730 | static void soc_resume_init(struct snd_soc_card *card) |
| 731 | { |
| 732 | /* deferred resume work */ |
| 733 | INIT_WORK(&card->deferred_resume_work, soc_resume_deferred); |
| 734 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 735 | #else |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 736 | #define snd_soc_suspend NULL |
| 737 | #define snd_soc_resume NULL |
Kuninori Morimoto | b3da425 | 2019-08-07 10:31:24 +0900 | [diff] [blame] | 738 | static inline void soc_resume_init(struct snd_soc_card *card) |
| 739 | { |
| 740 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 741 | #endif |
| 742 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 743 | static const struct snd_soc_dai_ops null_dai_ops = { |
Barry Song | 02a06d3 | 2009-10-16 18:13:38 +0800 | [diff] [blame] | 744 | }; |
| 745 | |
Kuninori Morimoto | c083444 | 2019-05-13 16:06:59 +0900 | [diff] [blame] | 746 | static struct device_node |
| 747 | *soc_component_to_node(struct snd_soc_component *component) |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 748 | { |
Kuninori Morimoto | c083444 | 2019-05-13 16:06:59 +0900 | [diff] [blame] | 749 | struct device_node *of_node; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 750 | |
Kuninori Morimoto | c083444 | 2019-05-13 16:06:59 +0900 | [diff] [blame] | 751 | of_node = component->dev->of_node; |
| 752 | if (!of_node && component->dev->parent) |
| 753 | of_node = component->dev->parent->of_node; |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 754 | |
Kuninori Morimoto | c083444 | 2019-05-13 16:06:59 +0900 | [diff] [blame] | 755 | return of_node; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Kuninori Morimoto | be6ac0a | 2018-09-11 06:51:45 +0000 | [diff] [blame] | 758 | static int snd_soc_is_matching_component( |
| 759 | const struct snd_soc_dai_link_component *dlc, |
| 760 | struct snd_soc_component *component) |
| 761 | { |
| 762 | struct device_node *component_of_node; |
| 763 | |
Kuninori Morimoto | 7d7db5d | 2019-06-20 09:49:17 +0900 | [diff] [blame] | 764 | if (!dlc) |
| 765 | return 0; |
| 766 | |
| 767 | component_of_node = soc_component_to_node(component); |
Kuninori Morimoto | be6ac0a | 2018-09-11 06:51:45 +0000 | [diff] [blame] | 768 | |
| 769 | if (dlc->of_node && component_of_node != dlc->of_node) |
| 770 | return 0; |
| 771 | if (dlc->name && strcmp(component->name, dlc->name)) |
| 772 | return 0; |
| 773 | |
| 774 | return 1; |
| 775 | } |
| 776 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 777 | static struct snd_soc_component *soc_find_component( |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 778 | const struct snd_soc_dai_link_component *dlc) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 779 | { |
| 780 | struct snd_soc_component *component; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 781 | |
| 782 | lockdep_assert_held(&client_mutex); |
| 783 | |
Kuninori Morimoto | e330326 | 2019-06-26 10:40:59 +0900 | [diff] [blame] | 784 | /* |
| 785 | * NOTE |
| 786 | * |
| 787 | * It returns *1st* found component, but some driver |
| 788 | * has few components by same of_node/name |
| 789 | * ex) |
| 790 | * CPU component and generic DMAEngine component |
| 791 | */ |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 792 | for_each_component(component) |
| 793 | if (snd_soc_is_matching_component(dlc, component)) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 794 | return component; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 795 | |
| 796 | return NULL; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Mengdong Lin | fbb88b5 | 2016-04-22 12:25:33 +0800 | [diff] [blame] | 799 | /** |
| 800 | * snd_soc_find_dai - Find a registered DAI |
| 801 | * |
Jeffy Chen | 4958471 | 2017-08-22 15:57:21 +0800 | [diff] [blame] | 802 | * @dlc: name of the DAI or the DAI driver and optional component info to match |
Mengdong Lin | fbb88b5 | 2016-04-22 12:25:33 +0800 | [diff] [blame] | 803 | * |
Stephen Boyd | ad61dd3 | 2017-05-08 15:57:50 -0700 | [diff] [blame] | 804 | * This function will search all registered components and their DAIs to |
Mengdong Lin | fbb88b5 | 2016-04-22 12:25:33 +0800 | [diff] [blame] | 805 | * find the DAI of the same name. The component's of_node and name |
| 806 | * should also match if being specified. |
| 807 | * |
| 808 | * Return: pointer of DAI, or NULL if not found. |
| 809 | */ |
Mengdong Lin | 305e902 | 2016-04-19 13:12:25 +0800 | [diff] [blame] | 810 | struct snd_soc_dai *snd_soc_find_dai( |
Lars-Peter Clausen | 14621c7 | 2014-08-19 15:51:27 +0200 | [diff] [blame] | 811 | const struct snd_soc_dai_link_component *dlc) |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 812 | { |
Lars-Peter Clausen | 14621c7 | 2014-08-19 15:51:27 +0200 | [diff] [blame] | 813 | struct snd_soc_component *component; |
| 814 | struct snd_soc_dai *dai; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 815 | |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 816 | lockdep_assert_held(&client_mutex); |
| 817 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 818 | /* Find CPU DAI from registered DAIs */ |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 819 | for_each_component(component) { |
Kuninori Morimoto | be6ac0a | 2018-09-11 06:51:45 +0000 | [diff] [blame] | 820 | if (!snd_soc_is_matching_component(dlc, component)) |
Lars-Peter Clausen | 14621c7 | 2014-08-19 15:51:27 +0200 | [diff] [blame] | 821 | continue; |
Kuninori Morimoto | 15a0c64 | 2018-09-21 05:23:17 +0000 | [diff] [blame] | 822 | for_each_component_dais(component, dai) { |
Jeffy Chen | 4958471 | 2017-08-22 15:57:21 +0800 | [diff] [blame] | 823 | if (dlc->dai_name && strcmp(dai->name, dlc->dai_name) |
Jeffy Chen | 6a6dafd | 2017-08-24 12:40:17 +0800 | [diff] [blame] | 824 | && (!dai->driver->name |
| 825 | || strcmp(dai->driver->name, dlc->dai_name))) |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 826 | continue; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 827 | |
Lars-Peter Clausen | 14621c7 | 2014-08-19 15:51:27 +0200 | [diff] [blame] | 828 | return dai; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 829 | } |
| 830 | } |
| 831 | |
| 832 | return NULL; |
| 833 | } |
Mengdong Lin | 305e902 | 2016-04-19 13:12:25 +0800 | [diff] [blame] | 834 | EXPORT_SYMBOL_GPL(snd_soc_find_dai); |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 835 | |
Kuninori Morimoto | bfce78a | 2019-11-05 15:45:50 +0900 | [diff] [blame] | 836 | static int soc_dai_link_sanity_check(struct snd_soc_card *card, |
| 837 | struct snd_soc_dai_link *link) |
Kuninori Morimoto | 3679490 | 2019-11-05 15:45:41 +0900 | [diff] [blame] | 838 | { |
| 839 | int i; |
| 840 | struct snd_soc_dai_link_component *codec, *platform; |
| 841 | |
| 842 | for_each_link_codecs(link, i, codec) { |
| 843 | /* |
| 844 | * Codec must be specified by 1 of name or OF node, |
| 845 | * not both or neither. |
| 846 | */ |
| 847 | if (!!codec->name == !!codec->of_node) { |
| 848 | dev_err(card->dev, "ASoC: Neither/both codec name/of_node are set for %s\n", |
| 849 | link->name); |
| 850 | return -EINVAL; |
| 851 | } |
| 852 | |
| 853 | /* Codec DAI name must be specified */ |
| 854 | if (!codec->dai_name) { |
| 855 | dev_err(card->dev, "ASoC: codec_dai_name not set for %s\n", |
| 856 | link->name); |
| 857 | return -EINVAL; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Defer card registration if codec component is not added to |
| 862 | * component list. |
| 863 | */ |
| 864 | if (!soc_find_component(codec)) |
| 865 | return -EPROBE_DEFER; |
| 866 | } |
| 867 | |
| 868 | for_each_link_platforms(link, i, platform) { |
| 869 | /* |
| 870 | * Platform may be specified by either name or OF node, but it |
| 871 | * can be left unspecified, then no components will be inserted |
| 872 | * in the rtdcom list |
| 873 | */ |
| 874 | if (!!platform->name == !!platform->of_node) { |
| 875 | dev_err(card->dev, |
| 876 | "ASoC: Neither/both platform name/of_node are set for %s\n", |
| 877 | link->name); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Defer card registration if platform component is not added to |
| 883 | * component list. |
| 884 | */ |
| 885 | if (!soc_find_component(platform)) |
| 886 | return -EPROBE_DEFER; |
| 887 | } |
| 888 | |
| 889 | /* FIXME */ |
| 890 | if (link->num_cpus > 1) { |
| 891 | dev_err(card->dev, |
| 892 | "ASoC: multi cpu is not yet supported %s\n", |
| 893 | link->name); |
| 894 | return -EINVAL; |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * CPU device may be specified by either name or OF node, but |
| 899 | * can be left unspecified, and will be matched based on DAI |
| 900 | * name alone.. |
| 901 | */ |
| 902 | if (link->cpus->name && link->cpus->of_node) { |
| 903 | dev_err(card->dev, |
| 904 | "ASoC: Neither/both cpu name/of_node are set for %s\n", |
| 905 | link->name); |
| 906 | return -EINVAL; |
| 907 | } |
| 908 | |
| 909 | /* |
Kuninori Morimoto | cd3c5ad | 2019-11-05 15:46:00 +0900 | [diff] [blame] | 910 | * Defer card registration if cpu dai component is not added to |
Kuninori Morimoto | 3679490 | 2019-11-05 15:45:41 +0900 | [diff] [blame] | 911 | * component list. |
| 912 | */ |
| 913 | if ((link->cpus->of_node || link->cpus->name) && |
| 914 | !soc_find_component(link->cpus)) |
| 915 | return -EPROBE_DEFER; |
| 916 | |
| 917 | /* |
| 918 | * At least one of CPU DAI name or CPU device name/node must be |
| 919 | * specified |
| 920 | */ |
| 921 | if (!link->cpus->dai_name && |
| 922 | !(link->cpus->name || link->cpus->of_node)) { |
| 923 | dev_err(card->dev, |
| 924 | "ASoC: Neither cpu_dai_name nor cpu_name/of_node are set for %s\n", |
| 925 | link->name); |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
Kuninori Morimoto | da704f2 | 2019-11-06 10:07:38 +0900 | [diff] [blame] | 932 | /** |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 933 | * snd_soc_remove_pcm_runtime - Remove a pcm_runtime from card |
| 934 | * @card: The ASoC card to which the pcm_runtime has |
| 935 | * @rtd: The pcm_runtime to remove |
Kuninori Morimoto | da704f2 | 2019-11-06 10:07:38 +0900 | [diff] [blame] | 936 | * |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 937 | * This function removes a pcm_runtime from the ASoC card. |
Kuninori Morimoto | da704f2 | 2019-11-06 10:07:38 +0900 | [diff] [blame] | 938 | */ |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 939 | void snd_soc_remove_pcm_runtime(struct snd_soc_card *card, |
| 940 | struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | bc7a909 | 2019-11-05 15:46:25 +0900 | [diff] [blame] | 941 | { |
Kuninori Morimoto | da704f2 | 2019-11-06 10:07:38 +0900 | [diff] [blame] | 942 | lockdep_assert_held(&client_mutex); |
| 943 | |
| 944 | /* |
| 945 | * Notify the machine driver for extra destruction |
| 946 | */ |
| 947 | if (card->remove_dai_link) |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 948 | card->remove_dai_link(card, rtd->dai_link); |
Kuninori Morimoto | da704f2 | 2019-11-06 10:07:38 +0900 | [diff] [blame] | 949 | |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 950 | soc_free_pcm_runtime(rtd); |
Kuninori Morimoto | bc7a909 | 2019-11-05 15:46:25 +0900 | [diff] [blame] | 951 | } |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 952 | EXPORT_SYMBOL_GPL(snd_soc_remove_pcm_runtime); |
Kuninori Morimoto | bc7a909 | 2019-11-05 15:46:25 +0900 | [diff] [blame] | 953 | |
Kuninori Morimoto | 63dc47d | 2019-11-06 10:07:31 +0900 | [diff] [blame] | 954 | /** |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 955 | * snd_soc_add_pcm_runtime - Add a pcm_runtime dynamically via dai_link |
| 956 | * @card: The ASoC card to which the pcm_runtime is added |
| 957 | * @dai_link: The DAI link to find pcm_runtime |
Kuninori Morimoto | 63dc47d | 2019-11-06 10:07:31 +0900 | [diff] [blame] | 958 | * |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 959 | * This function adds a pcm_runtime ASoC card by using dai_link. |
Kuninori Morimoto | 63dc47d | 2019-11-06 10:07:31 +0900 | [diff] [blame] | 960 | * |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 961 | * Note: Topology can use this API to add pcm_runtime when probing the |
Kuninori Morimoto | 63dc47d | 2019-11-06 10:07:31 +0900 | [diff] [blame] | 962 | * topology component. And machine drivers can still define static |
| 963 | * DAI links in dai_link array. |
| 964 | */ |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 965 | int snd_soc_add_pcm_runtime(struct snd_soc_card *card, |
| 966 | struct snd_soc_dai_link *dai_link) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 967 | { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 968 | struct snd_soc_pcm_runtime *rtd; |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 969 | struct snd_soc_dai_link_component *codec, *platform; |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 970 | struct snd_soc_component *component; |
Kuninori Morimoto | bfce78a | 2019-11-05 15:45:50 +0900 | [diff] [blame] | 971 | int i, ret; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 972 | |
Kuninori Morimoto | 63dc47d | 2019-11-06 10:07:31 +0900 | [diff] [blame] | 973 | lockdep_assert_held(&client_mutex); |
| 974 | |
| 975 | /* |
| 976 | * Notify the machine driver for extra initialization |
| 977 | */ |
| 978 | if (card->add_dai_link) |
| 979 | card->add_dai_link(card, dai_link); |
| 980 | |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 981 | if (dai_link->ignore) |
| 982 | return 0; |
| 983 | |
Mengdong Lin | 6f2f1ff | 2015-11-23 11:03:52 -0500 | [diff] [blame] | 984 | dev_dbg(card->dev, "ASoC: binding %s\n", dai_link->name); |
Mark Brown | 6308419 | 2008-12-02 15:08:03 +0000 | [diff] [blame] | 985 | |
Kuninori Morimoto | bfce78a | 2019-11-05 15:45:50 +0900 | [diff] [blame] | 986 | ret = soc_dai_link_sanity_check(card, dai_link); |
| 987 | if (ret < 0) |
| 988 | return ret; |
| 989 | |
Sudip Mukherjee | 513cb311 | 2016-02-22 14:14:31 +0530 | [diff] [blame] | 990 | rtd = soc_new_pcm_runtime(card, dai_link); |
| 991 | if (!rtd) |
| 992 | return -ENOMEM; |
| 993 | |
Kuninori Morimoto | 08a5841 | 2019-06-06 13:07:22 +0900 | [diff] [blame] | 994 | /* FIXME: we need multi CPU support in the future */ |
| 995 | rtd->cpu_dai = snd_soc_find_dai(dai_link->cpus); |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 996 | if (!rtd->cpu_dai) { |
Martin Hundebøll | 6b49087 | 2018-02-01 11:09:41 +0100 | [diff] [blame] | 997 | dev_info(card->dev, "ASoC: CPU DAI %s not registered\n", |
Kuninori Morimoto | 08a5841 | 2019-06-06 13:07:22 +0900 | [diff] [blame] | 998 | dai_link->cpus->dai_name); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 999 | goto _err_defer; |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 1000 | } |
Kuninori Morimoto | 12b0523 | 2020-01-10 11:35:54 +0900 | [diff] [blame] | 1001 | snd_soc_rtd_add_component(rtd, rtd->cpu_dai->component); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 1002 | |
Benoit Cousson | 88bd870 | 2014-07-08 23:19:34 +0200 | [diff] [blame] | 1003 | /* Find CODEC from registered CODECs */ |
Kuninori Morimoto | e2b30ed | 2019-05-13 16:06:44 +0900 | [diff] [blame] | 1004 | rtd->num_codecs = dai_link->num_codecs; |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 1005 | for_each_link_codecs(dai_link, i, codec) { |
| 1006 | rtd->codec_dais[i] = snd_soc_find_dai(codec); |
Kuninori Morimoto | 0a2cfcd | 2019-05-13 16:06:30 +0900 | [diff] [blame] | 1007 | if (!rtd->codec_dais[i]) { |
Stefan Agner | 7c7e2d6 | 2019-01-18 10:55:04 +0100 | [diff] [blame] | 1008 | dev_info(card->dev, "ASoC: CODEC DAI %s not registered\n", |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 1009 | codec->dai_name); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1010 | goto _err_defer; |
Benoit Cousson | 88bd870 | 2014-07-08 23:19:34 +0200 | [diff] [blame] | 1011 | } |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 1012 | |
Kuninori Morimoto | 12b0523 | 2020-01-10 11:35:54 +0900 | [diff] [blame] | 1013 | snd_soc_rtd_add_component(rtd, rtd->codec_dais[i]->component); |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 1014 | } |
Mark Brown | 848dd8b | 2011-04-27 18:16:32 +0100 | [diff] [blame] | 1015 | |
Benoit Cousson | 88bd870 | 2014-07-08 23:19:34 +0200 | [diff] [blame] | 1016 | /* Single codec links expect codec and codec_dai in runtime data */ |
Kuninori Morimoto | 0a2cfcd | 2019-05-13 16:06:30 +0900 | [diff] [blame] | 1017 | rtd->codec_dai = rtd->codec_dais[0]; |
Misael Lopez Cruz | 12023a9 | 2014-03-21 16:27:25 +0100 | [diff] [blame] | 1018 | |
Kuninori Morimoto | e2b30ed | 2019-05-13 16:06:44 +0900 | [diff] [blame] | 1019 | /* Find PLATFORM from registered PLATFORMs */ |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 1020 | for_each_link_platforms(dai_link, i, platform) { |
| 1021 | for_each_component(component) { |
| 1022 | if (!snd_soc_is_matching_component(platform, component)) |
| 1023 | continue; |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 1024 | |
Kuninori Morimoto | 12b0523 | 2020-01-10 11:35:54 +0900 | [diff] [blame] | 1025 | snd_soc_rtd_add_component(rtd, component); |
Jerome Brunet | 3461473 | 2019-06-27 14:13:50 +0200 | [diff] [blame] | 1026 | } |
Kuninori Morimoto | 90be711 | 2017-08-08 06:18:10 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 1029 | return 0; |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1030 | |
| 1031 | _err_defer: |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 1032 | snd_soc_remove_pcm_runtime(card, rtd); |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1033 | return -EPROBE_DEFER; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1034 | } |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 1035 | EXPORT_SYMBOL_GPL(snd_soc_add_pcm_runtime); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1036 | |
Kuninori Morimoto | bfa0dd8 | 2019-12-10 09:34:32 +0900 | [diff] [blame] | 1037 | static int soc_dai_pcm_new(struct snd_soc_dai **dais, int num_dais, |
| 1038 | struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | 9e9c70a | 2019-12-10 09:34:27 +0900 | [diff] [blame] | 1039 | { |
| 1040 | int i, ret = 0; |
| 1041 | |
| 1042 | for (i = 0; i < num_dais; ++i) { |
| 1043 | struct snd_soc_dai_driver *drv = dais[i]->driver; |
| 1044 | |
| 1045 | if (drv->pcm_new) |
| 1046 | ret = drv->pcm_new(rtd, dais[i]); |
| 1047 | if (ret < 0) { |
| 1048 | dev_err(dais[i]->dev, |
| 1049 | "ASoC: Failed to bind %s with pcm device\n", |
| 1050 | dais[i]->name); |
| 1051 | return ret; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | return 0; |
| 1056 | } |
| 1057 | |
Kuninori Morimoto | eaffeef | 2019-12-10 09:34:44 +0900 | [diff] [blame] | 1058 | static int soc_init_pcm_runtime(struct snd_soc_card *card, |
| 1059 | struct snd_soc_pcm_runtime *rtd) |
Kuninori Morimoto | 46496ac | 2019-12-10 09:34:36 +0900 | [diff] [blame] | 1060 | { |
| 1061 | struct snd_soc_dai_link *dai_link = rtd->dai_link; |
| 1062 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | 46496ac | 2019-12-10 09:34:36 +0900 | [diff] [blame] | 1063 | struct snd_soc_component *component; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1064 | int ret, num, i; |
Kuninori Morimoto | 46496ac | 2019-12-10 09:34:36 +0900 | [diff] [blame] | 1065 | |
| 1066 | /* set default power off timeout */ |
| 1067 | rtd->pmdown_time = pmdown_time; |
| 1068 | |
| 1069 | /* do machine specific initialization */ |
| 1070 | if (dai_link->init) { |
| 1071 | ret = dai_link->init(rtd); |
| 1072 | if (ret < 0) { |
| 1073 | dev_err(card->dev, "ASoC: failed to init %s: %d\n", |
| 1074 | dai_link->name, ret); |
| 1075 | return ret; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | if (dai_link->dai_fmt) { |
| 1080 | ret = snd_soc_runtime_set_dai_fmt(rtd, dai_link->dai_fmt); |
| 1081 | if (ret) |
| 1082 | return ret; |
| 1083 | } |
| 1084 | |
| 1085 | /* add DPCM sysfs entries */ |
| 1086 | soc_dpcm_debugfs_add(rtd); |
| 1087 | |
| 1088 | num = rtd->num; |
| 1089 | |
| 1090 | /* |
| 1091 | * most drivers will register their PCMs using DAI link ordering but |
| 1092 | * topology based drivers can use the DAI link id field to set PCM |
| 1093 | * device number and then use rtd + a base offset of the BEs. |
| 1094 | */ |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1095 | for_each_rtd_components(rtd, i, component) { |
Kuninori Morimoto | 46496ac | 2019-12-10 09:34:36 +0900 | [diff] [blame] | 1096 | if (!component->driver->use_dai_pcm_id) |
| 1097 | continue; |
| 1098 | |
| 1099 | if (rtd->dai_link->no_pcm) |
| 1100 | num += component->driver->be_pcm_base; |
| 1101 | else |
| 1102 | num = rtd->dai_link->id; |
| 1103 | } |
| 1104 | |
| 1105 | /* create compress_device if possible */ |
| 1106 | ret = snd_soc_dai_compress_new(cpu_dai, rtd, num); |
| 1107 | if (ret != -ENOTSUPP) { |
| 1108 | if (ret < 0) |
| 1109 | dev_err(card->dev, "ASoC: can't create compress %s\n", |
| 1110 | dai_link->stream_name); |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | /* create the pcm */ |
| 1115 | ret = soc_new_pcm(rtd, num); |
| 1116 | if (ret < 0) { |
| 1117 | dev_err(card->dev, "ASoC: can't create pcm %s :%d\n", |
| 1118 | dai_link->stream_name, ret); |
| 1119 | return ret; |
| 1120 | } |
| 1121 | ret = soc_dai_pcm_new(&cpu_dai, 1, rtd); |
| 1122 | if (ret < 0) |
| 1123 | return ret; |
| 1124 | ret = soc_dai_pcm_new(rtd->codec_dais, |
| 1125 | rtd->num_codecs, rtd); |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1129 | static void soc_set_name_prefix(struct snd_soc_card *card, |
| 1130 | struct snd_soc_component *component) |
| 1131 | { |
Kuninori Morimoto | aec3ff9 | 2019-12-10 09:34:52 +0900 | [diff] [blame] | 1132 | struct device_node *of_node = soc_component_to_node(component); |
Kuninori Morimoto | 29d9fc7 | 2019-12-11 13:31:39 +0900 | [diff] [blame] | 1133 | const char *str; |
| 1134 | int ret, i; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1135 | |
Kuninori Morimoto | 4702f99 | 2019-12-10 09:34:48 +0900 | [diff] [blame] | 1136 | for (i = 0; i < card->num_configs; i++) { |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1137 | struct snd_soc_codec_conf *map = &card->codec_conf[i]; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1138 | |
Kuninori Morimoto | c13493a | 2019-12-13 09:54:36 +0900 | [diff] [blame] | 1139 | if (snd_soc_is_matching_component(&map->dlc, component)) { |
| 1140 | component->name_prefix = map->name_prefix; |
| 1141 | return; |
| 1142 | } |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * If there is no configuration table or no match in the table, |
| 1147 | * check if a prefix is provided in the node |
| 1148 | */ |
Kuninori Morimoto | 29d9fc7 | 2019-12-11 13:31:39 +0900 | [diff] [blame] | 1149 | ret = of_property_read_string(of_node, "sound-name-prefix", &str); |
| 1150 | if (ret < 0) |
| 1151 | return; |
| 1152 | |
| 1153 | component->name_prefix = str; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1154 | } |
| 1155 | |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1156 | static void soc_remove_component(struct snd_soc_component *component, |
| 1157 | int probed) |
Kuninori Morimoto | 22d1423 | 2019-01-21 09:32:55 +0900 | [diff] [blame] | 1158 | { |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1159 | |
| 1160 | if (!component->card) |
| 1161 | return; |
| 1162 | |
| 1163 | if (probed) |
| 1164 | snd_soc_component_remove(component); |
| 1165 | |
Kuninori Morimoto | 04f770d | 2019-09-04 09:14:35 +0900 | [diff] [blame] | 1166 | /* For framework level robustness */ |
Amadeusz Sławiński | 3bb936f5 | 2019-06-05 15:45:53 +0200 | [diff] [blame] | 1167 | snd_soc_component_set_jack(component, NULL, NULL); |
Kuninori Morimoto | 04f770d | 2019-09-04 09:14:35 +0900 | [diff] [blame] | 1168 | |
Bard liao | 7a5d981 | 2019-09-18 21:31:31 +0800 | [diff] [blame] | 1169 | list_del_init(&component->card_list); |
Kuninori Morimoto | 22d1423 | 2019-01-21 09:32:55 +0900 | [diff] [blame] | 1170 | snd_soc_dapm_free(snd_soc_component_get_dapm(component)); |
| 1171 | soc_cleanup_component_debugfs(component); |
| 1172 | component->card = NULL; |
Pierre-Louis Bossart | 0e36f36 | 2019-08-07 21:51:31 -0500 | [diff] [blame] | 1173 | snd_soc_component_module_put_when_remove(component); |
Kuninori Morimoto | 22d1423 | 2019-01-21 09:32:55 +0900 | [diff] [blame] | 1174 | } |
| 1175 | |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1176 | static int soc_probe_component(struct snd_soc_card *card, |
| 1177 | struct snd_soc_component *component) |
| 1178 | { |
| 1179 | struct snd_soc_dapm_context *dapm = |
| 1180 | snd_soc_component_get_dapm(component); |
| 1181 | struct snd_soc_dai *dai; |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1182 | int probed = 0; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1183 | int ret; |
| 1184 | |
| 1185 | if (!strcmp(component->name, "snd-soc-dummy")) |
| 1186 | return 0; |
| 1187 | |
| 1188 | if (component->card) { |
| 1189 | if (component->card != card) { |
| 1190 | dev_err(component->dev, |
| 1191 | "Trying to bind component to card \"%s\" but is already bound to card \"%s\"\n", |
| 1192 | card->name, component->card->name); |
| 1193 | return -ENODEV; |
| 1194 | } |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | ret = snd_soc_component_module_get_when_probe(component); |
| 1199 | if (ret < 0) |
| 1200 | return ret; |
| 1201 | |
| 1202 | component->card = card; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1203 | soc_set_name_prefix(card, component); |
| 1204 | |
| 1205 | soc_init_component_debugfs(component); |
| 1206 | |
Kuninori Morimoto | 95c267d | 2019-08-23 09:58:52 +0900 | [diff] [blame] | 1207 | snd_soc_dapm_init(dapm, card, component); |
Kuninori Morimoto | b614bea | 2019-08-23 09:58:47 +0900 | [diff] [blame] | 1208 | |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1209 | ret = snd_soc_dapm_new_controls(dapm, |
| 1210 | component->driver->dapm_widgets, |
| 1211 | component->driver->num_dapm_widgets); |
| 1212 | |
| 1213 | if (ret != 0) { |
| 1214 | dev_err(component->dev, |
| 1215 | "Failed to create new controls %d\n", ret); |
| 1216 | goto err_probe; |
| 1217 | } |
| 1218 | |
| 1219 | for_each_component_dais(component, dai) { |
| 1220 | ret = snd_soc_dapm_new_dai_widgets(dapm, dai); |
| 1221 | if (ret != 0) { |
| 1222 | dev_err(component->dev, |
| 1223 | "Failed to create DAI widgets %d\n", ret); |
| 1224 | goto err_probe; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | ret = snd_soc_component_probe(component); |
| 1229 | if (ret < 0) { |
| 1230 | dev_err(component->dev, |
| 1231 | "ASoC: failed to probe component %d\n", ret); |
| 1232 | goto err_probe; |
| 1233 | } |
| 1234 | WARN(dapm->idle_bias_off && |
| 1235 | dapm->bias_level != SND_SOC_BIAS_OFF, |
| 1236 | "codec %s can not start from non-off bias with idle_bias_off==1\n", |
| 1237 | component->name); |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1238 | probed = 1; |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1239 | |
| 1240 | /* machine specific init */ |
| 1241 | if (component->init) { |
| 1242 | ret = component->init(component); |
| 1243 | if (ret < 0) { |
| 1244 | dev_err(component->dev, |
| 1245 | "Failed to do machine specific init %d\n", ret); |
| 1246 | goto err_probe; |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | ret = snd_soc_add_component_controls(component, |
| 1251 | component->driver->controls, |
| 1252 | component->driver->num_controls); |
| 1253 | if (ret < 0) |
| 1254 | goto err_probe; |
| 1255 | |
| 1256 | ret = snd_soc_dapm_add_routes(dapm, |
| 1257 | component->driver->dapm_routes, |
| 1258 | component->driver->num_dapm_routes); |
| 1259 | if (ret < 0) |
| 1260 | goto err_probe; |
| 1261 | |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1262 | /* see for_each_card_components */ |
| 1263 | list_add(&component->card_list, &card->component_dev_list); |
| 1264 | |
| 1265 | err_probe: |
| 1266 | if (ret < 0) |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1267 | soc_remove_component(component, probed); |
Kuninori Morimoto | ffd60fb | 2019-08-23 09:58:42 +0900 | [diff] [blame] | 1268 | |
| 1269 | return ret; |
| 1270 | } |
| 1271 | |
Lars-Peter Clausen | e60cd14 | 2014-08-19 15:51:26 +0200 | [diff] [blame] | 1272 | static void soc_remove_dai(struct snd_soc_dai *dai, int order) |
Jarkko Nikula | 589c356 | 2010-12-06 16:27:07 +0200 | [diff] [blame] | 1273 | { |
| 1274 | int err; |
| 1275 | |
Guennadi Liakhovetski | 52abe6c | 2019-02-01 11:05:13 -0600 | [diff] [blame] | 1276 | if (!dai || !dai->probed || !dai->driver || |
Kuninori Morimoto | 2eda3cb | 2018-09-11 06:54:26 +0000 | [diff] [blame] | 1277 | dai->driver->remove_order != order) |
| 1278 | return; |
| 1279 | |
Kuninori Morimoto | dcdab58 | 2019-07-22 10:35:05 +0900 | [diff] [blame] | 1280 | err = snd_soc_dai_remove(dai); |
| 1281 | if (err < 0) |
| 1282 | dev_err(dai->dev, |
| 1283 | "ASoC: failed to remove %s: %d\n", |
| 1284 | dai->name, err); |
| 1285 | |
Kuninori Morimoto | 2eda3cb | 2018-09-11 06:54:26 +0000 | [diff] [blame] | 1286 | dai->probed = 0; |
Misael Lopez Cruz | b0aa88a | 2014-03-21 16:27:26 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
Kuninori Morimoto | a7d44f7 | 2019-09-04 09:15:06 +0900 | [diff] [blame] | 1289 | static int soc_probe_dai(struct snd_soc_dai *dai, int order) |
| 1290 | { |
| 1291 | int ret; |
| 1292 | |
| 1293 | if (dai->probed || |
| 1294 | dai->driver->probe_order != order) |
| 1295 | return 0; |
| 1296 | |
| 1297 | ret = snd_soc_dai_probe(dai); |
| 1298 | if (ret < 0) { |
| 1299 | dev_err(dai->dev, "ASoC: failed to probe DAI %s: %d\n", |
| 1300 | dai->name, ret); |
| 1301 | return ret; |
| 1302 | } |
| 1303 | |
| 1304 | dai->probed = 1; |
| 1305 | |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
Kuninori Morimoto | 4ca47d2 | 2019-09-04 09:14:57 +0900 | [diff] [blame] | 1309 | static void soc_remove_link_dais(struct snd_soc_card *card) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1310 | { |
Lars-Peter Clausen | e60cd14 | 2014-08-19 15:51:26 +0200 | [diff] [blame] | 1311 | int i; |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 1312 | struct snd_soc_dai *codec_dai; |
Kuninori Morimoto | 4ca47d2 | 2019-09-04 09:14:57 +0900 | [diff] [blame] | 1313 | struct snd_soc_pcm_runtime *rtd; |
| 1314 | int order; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1315 | |
Kuninori Morimoto | 4ca47d2 | 2019-09-04 09:14:57 +0900 | [diff] [blame] | 1316 | for_each_comp_order(order) { |
| 1317 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 4ca47d2 | 2019-09-04 09:14:57 +0900 | [diff] [blame] | 1318 | /* remove the CODEC DAI */ |
| 1319 | for_each_rtd_codec_dai(rtd, i, codec_dai) |
| 1320 | soc_remove_dai(codec_dai, order); |
| 1321 | |
| 1322 | soc_remove_dai(rtd->cpu_dai, order); |
| 1323 | } |
| 1324 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Kuninori Morimoto | bc7c16c | 2019-09-04 09:15:23 +0900 | [diff] [blame] | 1327 | static int soc_probe_link_dais(struct snd_soc_card *card) |
| 1328 | { |
| 1329 | struct snd_soc_dai *codec_dai; |
| 1330 | struct snd_soc_pcm_runtime *rtd; |
| 1331 | int i, order, ret; |
| 1332 | |
| 1333 | for_each_comp_order(order) { |
| 1334 | for_each_card_rtds(card, rtd) { |
| 1335 | |
| 1336 | dev_dbg(card->dev, |
| 1337 | "ASoC: probe %s dai link %d late %d\n", |
| 1338 | card->name, rtd->num, order); |
| 1339 | |
| 1340 | ret = soc_probe_dai(rtd->cpu_dai, order); |
| 1341 | if (ret) |
| 1342 | return ret; |
| 1343 | |
| 1344 | /* probe the CODEC DAI */ |
| 1345 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
| 1346 | ret = soc_probe_dai(codec_dai, order); |
| 1347 | if (ret) |
| 1348 | return ret; |
| 1349 | } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
| 1355 | |
Kuninori Morimoto | b006c0c | 2019-09-04 09:14:51 +0900 | [diff] [blame] | 1356 | static void soc_remove_link_components(struct snd_soc_card *card) |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1357 | { |
Lars-Peter Clausen | 61aca564 | 2014-08-19 15:51:21 +0200 | [diff] [blame] | 1358 | struct snd_soc_component *component; |
Kuninori Morimoto | b006c0c | 2019-09-04 09:14:51 +0900 | [diff] [blame] | 1359 | struct snd_soc_pcm_runtime *rtd; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1360 | int i, order; |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1361 | |
Kuninori Morimoto | b006c0c | 2019-09-04 09:14:51 +0900 | [diff] [blame] | 1362 | for_each_comp_order(order) { |
| 1363 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1364 | for_each_rtd_components(rtd, i, component) { |
Kuninori Morimoto | b006c0c | 2019-09-04 09:14:51 +0900 | [diff] [blame] | 1365 | if (component->driver->remove_order != order) |
| 1366 | continue; |
| 1367 | |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1368 | soc_remove_component(component, 1); |
Kuninori Morimoto | b006c0c | 2019-09-04 09:14:51 +0900 | [diff] [blame] | 1369 | } |
| 1370 | } |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1371 | } |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1372 | } |
| 1373 | |
Kuninori Morimoto | 62f07a6 | 2019-09-04 09:14:46 +0900 | [diff] [blame] | 1374 | static int soc_probe_link_components(struct snd_soc_card *card) |
Kuninori Morimoto | 6fb0355 | 2019-08-23 09:58:58 +0900 | [diff] [blame] | 1375 | { |
| 1376 | struct snd_soc_component *component; |
Kuninori Morimoto | 62f07a6 | 2019-09-04 09:14:46 +0900 | [diff] [blame] | 1377 | struct snd_soc_pcm_runtime *rtd; |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1378 | int i, ret, order; |
Kuninori Morimoto | 6fb0355 | 2019-08-23 09:58:58 +0900 | [diff] [blame] | 1379 | |
Kuninori Morimoto | 62f07a6 | 2019-09-04 09:14:46 +0900 | [diff] [blame] | 1380 | for_each_comp_order(order) { |
| 1381 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | 613fb50 | 2020-01-10 11:35:21 +0900 | [diff] [blame] | 1382 | for_each_rtd_components(rtd, i, component) { |
Kuninori Morimoto | 62f07a6 | 2019-09-04 09:14:46 +0900 | [diff] [blame] | 1383 | if (component->driver->probe_order != order) |
| 1384 | continue; |
| 1385 | |
| 1386 | ret = soc_probe_component(card, component); |
| 1387 | if (ret < 0) |
| 1388 | return ret; |
| 1389 | } |
Kuninori Morimoto | 6fb0355 | 2019-08-23 09:58:58 +0900 | [diff] [blame] | 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
Kuninori Morimoto | e8fbd25 | 2019-09-04 09:15:40 +0900 | [diff] [blame] | 1396 | static void soc_unbind_aux_dev(struct snd_soc_card *card) |
Kuninori Morimoto | 4893a2e | 2019-09-04 09:15:35 +0900 | [diff] [blame] | 1397 | { |
Kuninori Morimoto | e8fbd25 | 2019-09-04 09:15:40 +0900 | [diff] [blame] | 1398 | struct snd_soc_component *component, *_component; |
| 1399 | |
| 1400 | for_each_card_auxs_safe(card, component, _component) { |
| 1401 | component->init = NULL; |
| 1402 | list_del(&component->card_aux_list); |
| 1403 | } |
Kuninori Morimoto | 4893a2e | 2019-09-04 09:15:35 +0900 | [diff] [blame] | 1404 | } |
| 1405 | |
Kuninori Morimoto | bee886f | 2019-09-04 09:15:28 +0900 | [diff] [blame] | 1406 | static int soc_bind_aux_dev(struct snd_soc_card *card) |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 1407 | { |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1408 | struct snd_soc_component *component; |
Kuninori Morimoto | bee886f | 2019-09-04 09:15:28 +0900 | [diff] [blame] | 1409 | struct snd_soc_aux_dev *aux; |
| 1410 | int i; |
Sebastian Reichel | 3ca041e | 2014-04-28 16:07:22 +0200 | [diff] [blame] | 1411 | |
Kuninori Morimoto | bee886f | 2019-09-04 09:15:28 +0900 | [diff] [blame] | 1412 | for_each_card_pre_auxs(card, i, aux) { |
| 1413 | /* codecs, usually analog devices */ |
| 1414 | component = soc_find_component(&aux->dlc); |
| 1415 | if (!component) |
| 1416 | return -EPROBE_DEFER; |
Lars-Peter Clausen | 44c69bb | 2014-07-01 22:13:47 +0200 | [diff] [blame] | 1417 | |
Kuninori Morimoto | bee886f | 2019-09-04 09:15:28 +0900 | [diff] [blame] | 1418 | component->init = aux->init; |
| 1419 | /* see for_each_card_auxs */ |
| 1420 | list_add(&component->card_aux_list, &card->aux_comp_list); |
| 1421 | } |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1422 | return 0; |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | static int soc_probe_aux_devices(struct snd_soc_card *card) |
| 1426 | { |
Kuninori Morimoto | 74bd3f9 | 2019-11-06 10:08:06 +0900 | [diff] [blame] | 1427 | struct snd_soc_component *component; |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1428 | int order; |
| 1429 | int ret; |
| 1430 | |
Kuninori Morimoto | 1a1035a | 2018-09-18 01:30:41 +0000 | [diff] [blame] | 1431 | for_each_comp_order(order) { |
Kuninori Morimoto | 74bd3f9 | 2019-11-06 10:08:06 +0900 | [diff] [blame] | 1432 | for_each_card_auxs(card, component) { |
| 1433 | if (component->driver->probe_order != order) |
| 1434 | continue; |
| 1435 | |
| 1436 | ret = soc_probe_component(card, component); |
| 1437 | if (ret < 0) |
| 1438 | return ret; |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1439 | } |
| 1440 | } |
Lars-Peter Clausen | 65d9361 | 2014-08-19 15:51:22 +0200 | [diff] [blame] | 1441 | |
Lars-Peter Clausen | 44c69bb | 2014-07-01 22:13:47 +0200 | [diff] [blame] | 1442 | return 0; |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1445 | static void soc_remove_aux_devices(struct snd_soc_card *card) |
Jarkko Nikula | 2eea392 | 2010-11-25 17:47:38 +0200 | [diff] [blame] | 1446 | { |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1447 | struct snd_soc_component *comp, *_comp; |
| 1448 | int order; |
Jarkko Nikula | 2eea392 | 2010-11-25 17:47:38 +0200 | [diff] [blame] | 1449 | |
Kuninori Morimoto | 1a1035a | 2018-09-18 01:30:41 +0000 | [diff] [blame] | 1450 | for_each_comp_order(order) { |
Kuninori Morimoto | c2b71c7 | 2019-08-08 14:54:44 +0900 | [diff] [blame] | 1451 | for_each_card_auxs_safe(card, comp, _comp) { |
Kuninori Morimoto | e8fbd25 | 2019-09-04 09:15:40 +0900 | [diff] [blame] | 1452 | if (comp->driver->remove_order == order) |
Kuninori Morimoto | c6619b7 | 2019-11-06 10:07:46 +0900 | [diff] [blame] | 1453 | soc_remove_component(comp, 1); |
Lars-Peter Clausen | 5f3484a | 2014-07-01 22:13:48 +0200 | [diff] [blame] | 1454 | } |
| 1455 | } |
Jarkko Nikula | 2eea392 | 2010-11-25 17:47:38 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1458 | /** |
| 1459 | * snd_soc_runtime_set_dai_fmt() - Change DAI link format for a ASoC runtime |
| 1460 | * @rtd: The runtime for which the DAI link format should be changed |
| 1461 | * @dai_fmt: The new DAI link format |
| 1462 | * |
| 1463 | * This function updates the DAI link format for all DAIs connected to the DAI |
| 1464 | * link for the specified runtime. |
| 1465 | * |
| 1466 | * Note: For setups with a static format set the dai_fmt field in the |
| 1467 | * corresponding snd_dai_link struct instead of using this function. |
| 1468 | * |
| 1469 | * Returns 0 on success, otherwise a negative error code. |
| 1470 | */ |
| 1471 | int snd_soc_runtime_set_dai_fmt(struct snd_soc_pcm_runtime *rtd, |
| 1472 | unsigned int dai_fmt) |
| 1473 | { |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1474 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 1475 | struct snd_soc_dai *codec_dai; |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1476 | unsigned int i; |
| 1477 | int ret; |
| 1478 | |
Kuninori Morimoto | 0b7990e | 2018-09-03 02:12:56 +0000 | [diff] [blame] | 1479 | for_each_rtd_codec_dai(rtd, i, codec_dai) { |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1480 | ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt); |
| 1481 | if (ret != 0 && ret != -ENOTSUPP) { |
| 1482 | dev_warn(codec_dai->dev, |
| 1483 | "ASoC: Failed to set DAI format: %d\n", ret); |
| 1484 | return ret; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1488 | /* |
| 1489 | * Flip the polarity for the "CPU" end of a CODEC<->CODEC link |
| 1490 | * the component which has non_legacy_dai_naming is Codec |
| 1491 | */ |
Kuninori Morimoto | 999f7f5 | 2018-05-08 03:20:24 +0000 | [diff] [blame] | 1492 | if (cpu_dai->component->driver->non_legacy_dai_naming) { |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1493 | unsigned int inv_dai_fmt; |
| 1494 | |
| 1495 | inv_dai_fmt = dai_fmt & ~SND_SOC_DAIFMT_MASTER_MASK; |
| 1496 | switch (dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 1497 | case SND_SOC_DAIFMT_CBM_CFM: |
| 1498 | inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFS; |
| 1499 | break; |
| 1500 | case SND_SOC_DAIFMT_CBM_CFS: |
| 1501 | inv_dai_fmt |= SND_SOC_DAIFMT_CBS_CFM; |
| 1502 | break; |
| 1503 | case SND_SOC_DAIFMT_CBS_CFM: |
| 1504 | inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFS; |
| 1505 | break; |
| 1506 | case SND_SOC_DAIFMT_CBS_CFS: |
| 1507 | inv_dai_fmt |= SND_SOC_DAIFMT_CBM_CFM; |
| 1508 | break; |
| 1509 | } |
| 1510 | |
| 1511 | dai_fmt = inv_dai_fmt; |
| 1512 | } |
| 1513 | |
| 1514 | ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt); |
| 1515 | if (ret != 0 && ret != -ENOTSUPP) { |
| 1516 | dev_warn(cpu_dai->dev, |
| 1517 | "ASoC: Failed to set DAI format: %d\n", ret); |
| 1518 | return ret; |
| 1519 | } |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
Lars-Peter Clausen | ddaca25 | 2015-01-08 12:23:05 +0100 | [diff] [blame] | 1523 | EXPORT_SYMBOL_GPL(snd_soc_runtime_set_dai_fmt); |
Lars-Peter Clausen | ce64c8b | 2015-01-06 15:17:20 +0100 | [diff] [blame] | 1524 | |
Takashi Iwai | 1f5a453 | 2017-04-24 08:54:41 +0200 | [diff] [blame] | 1525 | #ifdef CONFIG_DMI |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1526 | /* |
Kuninori Morimoto | 8a6a6a3 | 2019-12-11 13:32:05 +0900 | [diff] [blame] | 1527 | * If a DMI filed contain strings in this blacklist (e.g. |
| 1528 | * "Type2 - Board Manufacturer" or "Type1 - TBD by OEM"), it will be taken |
| 1529 | * as invalid and dropped when setting the card long name from DMI info. |
| 1530 | */ |
| 1531 | static const char * const dmi_blacklist[] = { |
| 1532 | "To be filled by OEM", |
| 1533 | "TBD by OEM", |
| 1534 | "Default String", |
| 1535 | "Board Manufacturer", |
| 1536 | "Board Vendor Name", |
| 1537 | "Board Product Name", |
| 1538 | NULL, /* terminator */ |
| 1539 | }; |
| 1540 | |
| 1541 | /* |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1542 | * Trim special characters, and replace '-' with '_' since '-' is used to |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1543 | * separate different DMI fields in the card long name. Only number and |
| 1544 | * alphabet characters and a few separator characters are kept. |
| 1545 | */ |
| 1546 | static void cleanup_dmi_name(char *name) |
| 1547 | { |
| 1548 | int i, j = 0; |
| 1549 | |
| 1550 | for (i = 0; name[i]; i++) { |
| 1551 | if (isalnum(name[i]) || (name[i] == '.') |
| 1552 | || (name[i] == '_')) |
| 1553 | name[j++] = name[i]; |
| 1554 | else if (name[i] == '-') |
| 1555 | name[j++] = '_'; |
| 1556 | } |
| 1557 | |
| 1558 | name[j] = '\0'; |
| 1559 | } |
| 1560 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1561 | /* |
| 1562 | * Check if a DMI field is valid, i.e. not containing any string |
Mengdong Lin | 98faf43 | 2017-06-28 15:01:39 +0800 | [diff] [blame] | 1563 | * in the black list. |
| 1564 | */ |
| 1565 | static int is_dmi_valid(const char *field) |
| 1566 | { |
| 1567 | int i = 0; |
| 1568 | |
| 1569 | while (dmi_blacklist[i]) { |
| 1570 | if (strstr(field, dmi_blacklist[i])) |
| 1571 | return 0; |
| 1572 | i++; |
Wu Fengguang | 46b5a4d | 2017-06-30 00:27:13 +0800 | [diff] [blame] | 1573 | } |
Mengdong Lin | 98faf43 | 2017-06-28 15:01:39 +0800 | [diff] [blame] | 1574 | |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * Append a string to card->dmi_longname with character cleanups. |
| 1580 | */ |
| 1581 | static void append_dmi_string(struct snd_soc_card *card, const char *str) |
| 1582 | { |
| 1583 | char *dst = card->dmi_longname; |
| 1584 | size_t dst_len = sizeof(card->dmi_longname); |
| 1585 | size_t len; |
| 1586 | |
| 1587 | len = strlen(dst); |
| 1588 | snprintf(dst + len, dst_len - len, "-%s", str); |
| 1589 | |
| 1590 | len++; /* skip the separator "-" */ |
| 1591 | if (len < dst_len) |
| 1592 | cleanup_dmi_name(dst + len); |
| 1593 | } |
| 1594 | |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1595 | /** |
| 1596 | * snd_soc_set_dmi_name() - Register DMI names to card |
| 1597 | * @card: The card to register DMI names |
| 1598 | * @flavour: The flavour "differentiator" for the card amongst its peers. |
| 1599 | * |
| 1600 | * An Intel machine driver may be used by many different devices but are |
| 1601 | * difficult for userspace to differentiate, since machine drivers ususally |
| 1602 | * use their own name as the card short name and leave the card long name |
| 1603 | * blank. To differentiate such devices and fix bugs due to lack of |
| 1604 | * device-specific configurations, this function allows DMI info to be used |
| 1605 | * as the sound card long name, in the format of |
| 1606 | * "vendor-product-version-board" |
| 1607 | * (Character '-' is used to separate different DMI fields here). |
| 1608 | * This will help the user space to load the device-specific Use Case Manager |
| 1609 | * (UCM) configurations for the card. |
| 1610 | * |
| 1611 | * Possible card long names may be: |
| 1612 | * DellInc.-XPS139343-01-0310JH |
| 1613 | * ASUSTeKCOMPUTERINC.-T100TA-1.0-T100TA |
| 1614 | * Circuitco-MinnowboardMaxD0PLATFORM-D0-MinnowBoardMAX |
| 1615 | * |
| 1616 | * This function also supports flavoring the card longname to provide |
| 1617 | * the extra differentiation, like "vendor-product-version-board-flavor". |
| 1618 | * |
| 1619 | * We only keep number and alphabet characters and a few separator characters |
| 1620 | * in the card long name since UCM in the user space uses the card long names |
| 1621 | * as card configuration directory names and AudoConf cannot support special |
| 1622 | * charactors like SPACE. |
| 1623 | * |
| 1624 | * Returns 0 on success, otherwise a negative error code. |
| 1625 | */ |
| 1626 | int snd_soc_set_dmi_name(struct snd_soc_card *card, const char *flavour) |
| 1627 | { |
| 1628 | const char *vendor, *product, *product_version, *board; |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1629 | |
| 1630 | if (card->long_name) |
| 1631 | return 0; /* long name already set by driver or from DMI */ |
| 1632 | |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1633 | /* make up dmi long name as: vendor-product-version-board */ |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1634 | vendor = dmi_get_system_info(DMI_BOARD_VENDOR); |
Mengdong Lin | 98faf43 | 2017-06-28 15:01:39 +0800 | [diff] [blame] | 1635 | if (!vendor || !is_dmi_valid(vendor)) { |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1636 | dev_warn(card->dev, "ASoC: no DMI vendor name!\n"); |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1640 | snprintf(card->dmi_longname, sizeof(card->dmi_longname), "%s", vendor); |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1641 | cleanup_dmi_name(card->dmi_longname); |
| 1642 | |
| 1643 | product = dmi_get_system_info(DMI_PRODUCT_NAME); |
Mengdong Lin | 98faf43 | 2017-06-28 15:01:39 +0800 | [diff] [blame] | 1644 | if (product && is_dmi_valid(product)) { |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1645 | append_dmi_string(card, product); |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1646 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1647 | /* |
| 1648 | * some vendors like Lenovo may only put a self-explanatory |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1649 | * name in the product version field |
| 1650 | */ |
| 1651 | product_version = dmi_get_system_info(DMI_PRODUCT_VERSION); |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1652 | if (product_version && is_dmi_valid(product_version)) |
| 1653 | append_dmi_string(card, product_version); |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | board = dmi_get_system_info(DMI_BOARD_NAME); |
Mengdong Lin | 98faf43 | 2017-06-28 15:01:39 +0800 | [diff] [blame] | 1657 | if (board && is_dmi_valid(board)) { |
Jaroslav Kysela | 39870b0 | 2019-11-20 18:44:35 +0100 | [diff] [blame] | 1658 | if (!product || strcasecmp(board, product)) |
| 1659 | append_dmi_string(card, board); |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1660 | } else if (!product) { |
| 1661 | /* fall back to using legacy name */ |
| 1662 | dev_warn(card->dev, "ASoC: no DMI board/product name!\n"); |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | /* Add flavour to dmi long name */ |
Jaroslav Kysela | 4e01e5d | 2019-11-20 18:44:34 +0100 | [diff] [blame] | 1667 | if (flavour) |
| 1668 | append_dmi_string(card, flavour); |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1669 | |
| 1670 | /* set the card long name */ |
| 1671 | card->long_name = card->dmi_longname; |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | EXPORT_SYMBOL_GPL(snd_soc_set_dmi_name); |
Takashi Iwai | 1f5a453 | 2017-04-24 08:54:41 +0200 | [diff] [blame] | 1676 | #endif /* CONFIG_DMI */ |
Liam Girdwood | 345233d | 2017-01-14 16:13:02 +0800 | [diff] [blame] | 1677 | |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1678 | static void soc_check_tplg_fes(struct snd_soc_card *card) |
| 1679 | { |
| 1680 | struct snd_soc_component *component; |
| 1681 | const struct snd_soc_component_driver *comp_drv; |
| 1682 | struct snd_soc_dai_link *dai_link; |
| 1683 | int i; |
| 1684 | |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 1685 | for_each_component(component) { |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1686 | |
Daniel Baluta | 49f9c4f | 2019-09-25 21:33:58 +0300 | [diff] [blame] | 1687 | /* does this component override BEs ? */ |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1688 | if (!component->driver->ignore_machine) |
| 1689 | continue; |
| 1690 | |
| 1691 | /* for this machine ? */ |
Pierre-Louis Bossart | e194098b | 2019-03-01 19:08:51 -0600 | [diff] [blame] | 1692 | if (!strcmp(component->driver->ignore_machine, |
| 1693 | card->dev->driver->name)) |
| 1694 | goto match; |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1695 | if (strcmp(component->driver->ignore_machine, |
Pierre-Louis Bossart | e194098b | 2019-03-01 19:08:51 -0600 | [diff] [blame] | 1696 | dev_name(card->dev))) |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1697 | continue; |
Pierre-Louis Bossart | e194098b | 2019-03-01 19:08:51 -0600 | [diff] [blame] | 1698 | match: |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1699 | /* machine matches, so override the rtd data */ |
Kuninori Morimoto | 7fe072b | 2018-09-18 01:28:49 +0000 | [diff] [blame] | 1700 | for_each_card_prelinks(card, i, dai_link) { |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1701 | |
| 1702 | /* ignore this FE */ |
| 1703 | if (dai_link->dynamic) { |
| 1704 | dai_link->ignore = true; |
| 1705 | continue; |
| 1706 | } |
| 1707 | |
Daniel Baluta | 49f9c4f | 2019-09-25 21:33:58 +0300 | [diff] [blame] | 1708 | dev_info(card->dev, "info: override BE DAI link %s\n", |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1709 | card->dai_link[i].name); |
| 1710 | |
| 1711 | /* override platform component */ |
Kuninori Morimoto | adb76b5 | 2019-06-06 13:22:19 +0900 | [diff] [blame] | 1712 | if (!dai_link->platforms) { |
Kuninori Morimoto | daecf46 | 2018-08-31 03:10:08 +0000 | [diff] [blame] | 1713 | dev_err(card->dev, "init platform error"); |
| 1714 | continue; |
| 1715 | } |
Kuninori Morimoto | 910fdca | 2019-01-21 09:32:32 +0900 | [diff] [blame] | 1716 | dai_link->platforms->name = component->name; |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1717 | |
| 1718 | /* convert non BE into BE */ |
| 1719 | dai_link->no_pcm = 1; |
Daniel Baluta | 218fe9b | 2019-12-04 17:13:33 +0200 | [diff] [blame] | 1720 | dai_link->dpcm_playback = 1; |
| 1721 | dai_link->dpcm_capture = 1; |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1722 | |
| 1723 | /* override any BE fixups */ |
| 1724 | dai_link->be_hw_params_fixup = |
| 1725 | component->driver->be_hw_params_fixup; |
| 1726 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1727 | /* |
| 1728 | * most BE links don't set stream name, so set it to |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1729 | * dai link name if it's NULL to help bind widgets. |
| 1730 | */ |
| 1731 | if (!dai_link->stream_name) |
| 1732 | dai_link->stream_name = dai_link->name; |
| 1733 | } |
| 1734 | |
| 1735 | /* Inform userspace we are using alternate topology */ |
| 1736 | if (component->driver->topology_name_prefix) { |
| 1737 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 1738 | /* topology shortname created? */ |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1739 | if (!card->topology_shortname_created) { |
| 1740 | comp_drv = component->driver; |
| 1741 | |
| 1742 | snprintf(card->topology_shortname, 32, "%s-%s", |
| 1743 | comp_drv->topology_name_prefix, |
| 1744 | card->name); |
| 1745 | card->topology_shortname_created = true; |
| 1746 | } |
| 1747 | |
| 1748 | /* use topology shortname */ |
| 1749 | card->name = card->topology_shortname; |
| 1750 | } |
| 1751 | } |
| 1752 | } |
| 1753 | |
Kuninori Morimoto | 0f23f71 | 2019-10-02 14:22:49 +0900 | [diff] [blame] | 1754 | #define soc_setup_card_name(name, name1, name2, norm) \ |
| 1755 | __soc_setup_card_name(name, sizeof(name), name1, name2, norm) |
| 1756 | static void __soc_setup_card_name(char *name, int len, |
| 1757 | const char *name1, const char *name2, |
| 1758 | int normalization) |
| 1759 | { |
| 1760 | int i; |
| 1761 | |
| 1762 | snprintf(name, len, "%s", name1 ? name1 : name2); |
| 1763 | |
| 1764 | if (!normalization) |
| 1765 | return; |
| 1766 | |
| 1767 | /* |
| 1768 | * Name normalization |
| 1769 | * |
| 1770 | * The driver name is somewhat special, as it's used as a key for |
| 1771 | * searches in the user-space. |
| 1772 | * |
| 1773 | * ex) |
| 1774 | * "abcd??efg" -> "abcd__efg" |
| 1775 | */ |
| 1776 | for (i = 0; i < len; i++) { |
| 1777 | switch (name[i]) { |
| 1778 | case '_': |
| 1779 | case '-': |
| 1780 | case '\0': |
| 1781 | break; |
| 1782 | default: |
| 1783 | if (!isalnum(name[i])) |
| 1784 | name[i] = '_'; |
| 1785 | break; |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1790 | static void soc_cleanup_card_resources(struct snd_soc_card *card, |
| 1791 | int card_probed) |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1792 | { |
Kuninori Morimoto | cc73390 | 2019-12-10 09:33:40 +0900 | [diff] [blame] | 1793 | struct snd_soc_pcm_runtime *rtd, *n; |
Kuninori Morimoto | 7ce6088 | 2019-10-02 14:22:40 +0900 | [diff] [blame] | 1794 | |
Kuninori Morimoto | 0ced7b0 | 2019-11-18 10:51:11 +0900 | [diff] [blame] | 1795 | if (card->snd_card) |
| 1796 | snd_card_disconnect_sync(card->snd_card); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1797 | |
Kuninori Morimoto | 2a6f089 | 2019-11-13 10:16:29 +0900 | [diff] [blame] | 1798 | snd_soc_dapm_shutdown(card); |
| 1799 | |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1800 | /* remove and free each DAI */ |
Kuninori Morimoto | 7ce6088 | 2019-10-02 14:22:40 +0900 | [diff] [blame] | 1801 | soc_remove_link_dais(card); |
Kuninori Morimoto | 0ced7b0 | 2019-11-18 10:51:11 +0900 | [diff] [blame] | 1802 | soc_remove_link_components(card); |
Kuninori Morimoto | 7ce6088 | 2019-10-02 14:22:40 +0900 | [diff] [blame] | 1803 | |
Kuninori Morimoto | cc73390 | 2019-12-10 09:33:40 +0900 | [diff] [blame] | 1804 | for_each_card_rtds_safe(card, rtd, n) |
Kuninori Morimoto | 50cd9b5 | 2019-12-10 09:34:23 +0900 | [diff] [blame] | 1805 | snd_soc_remove_pcm_runtime(card, rtd); |
Kuninori Morimoto | 7ce6088 | 2019-10-02 14:22:40 +0900 | [diff] [blame] | 1806 | |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1807 | /* remove auxiliary devices */ |
| 1808 | soc_remove_aux_devices(card); |
Kuninori Morimoto | e8fbd25 | 2019-09-04 09:15:40 +0900 | [diff] [blame] | 1809 | soc_unbind_aux_dev(card); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1810 | |
| 1811 | snd_soc_dapm_free(&card->dapm); |
| 1812 | soc_cleanup_card_debugfs(card); |
| 1813 | |
| 1814 | /* remove the card */ |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1815 | if (card_probed && card->remove) |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1816 | card->remove(card); |
Kuninori Morimoto | 0ced7b0 | 2019-11-18 10:51:11 +0900 | [diff] [blame] | 1817 | |
| 1818 | if (card->snd_card) { |
| 1819 | snd_card_free(card->snd_card); |
| 1820 | card->snd_card = NULL; |
| 1821 | } |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1822 | } |
| 1823 | |
Kuninori Morimoto | 2cc1afc | 2019-11-13 10:16:34 +0900 | [diff] [blame] | 1824 | static void snd_soc_unbind_card(struct snd_soc_card *card, bool unregister) |
| 1825 | { |
| 1826 | if (card->instantiated) { |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1827 | int card_probed = 1; |
| 1828 | |
Kuninori Morimoto | 2cc1afc | 2019-11-13 10:16:34 +0900 | [diff] [blame] | 1829 | card->instantiated = false; |
| 1830 | snd_soc_flush_all_delayed_work(card); |
| 1831 | |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1832 | soc_cleanup_card_resources(card, card_probed); |
Kuninori Morimoto | 2cc1afc | 2019-11-13 10:16:34 +0900 | [diff] [blame] | 1833 | if (!unregister) |
| 1834 | list_add(&card->list, &unbind_card_list); |
| 1835 | } else { |
| 1836 | if (unregister) |
| 1837 | list_del(&card->list); |
| 1838 | } |
| 1839 | } |
| 1840 | |
Kuninori Morimoto | ed90c01 | 2019-11-06 10:07:56 +0900 | [diff] [blame] | 1841 | static int snd_soc_bind_card(struct snd_soc_card *card) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1842 | { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 1843 | struct snd_soc_pcm_runtime *rtd; |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 1844 | struct snd_soc_component *component; |
Mengdong Lin | 61b0088 | 2015-12-02 14:11:48 +0800 | [diff] [blame] | 1845 | struct snd_soc_dai_link *dai_link; |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1846 | int ret, i, card_probed = 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1847 | |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 1848 | mutex_lock(&client_mutex); |
Liam Girdwood | 01b9d99 | 2012-03-07 10:38:25 +0000 | [diff] [blame] | 1849 | mutex_lock_nested(&card->mutex, SND_SOC_CARD_CLASS_INIT); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1850 | |
Kuninori Morimoto | 95c267d | 2019-08-23 09:58:52 +0900 | [diff] [blame] | 1851 | snd_soc_dapm_init(&card->dapm, card, NULL); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1852 | |
Liam Girdwood | a655de8 | 2018-07-02 16:59:54 +0100 | [diff] [blame] | 1853 | /* check whether any platform is ignore machine FE and using topology */ |
| 1854 | soc_check_tplg_fes(card); |
| 1855 | |
Lars-Peter Clausen | 44c69bb | 2014-07-01 22:13:47 +0200 | [diff] [blame] | 1856 | /* bind aux_devs too */ |
Kuninori Morimoto | bee886f | 2019-09-04 09:15:28 +0900 | [diff] [blame] | 1857 | ret = soc_bind_aux_dev(card); |
| 1858 | if (ret < 0) |
| 1859 | goto probe_end; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1860 | |
Mengdong Lin | f8f8036 | 2015-12-02 14:11:22 +0800 | [diff] [blame] | 1861 | /* add predefined DAI links to the list */ |
Kuninori Morimoto | d814598 | 2019-10-02 14:23:07 +0900 | [diff] [blame] | 1862 | card->num_rtd = 0; |
Kuninori Morimoto | 5b99a0a | 2019-08-07 10:30:36 +0900 | [diff] [blame] | 1863 | for_each_card_prelinks(card, i, dai_link) { |
Kuninori Morimoto | 0c04800 | 2019-12-10 09:34:19 +0900 | [diff] [blame] | 1864 | ret = snd_soc_add_pcm_runtime(card, dai_link); |
Kuninori Morimoto | 5b99a0a | 2019-08-07 10:30:36 +0900 | [diff] [blame] | 1865 | if (ret < 0) |
| 1866 | goto probe_end; |
| 1867 | } |
Mengdong Lin | f8f8036 | 2015-12-02 14:11:22 +0800 | [diff] [blame] | 1868 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1869 | /* card bind complete so register a sound card */ |
Takashi Iwai | 102b5a8 | 2014-01-29 14:42:55 +0100 | [diff] [blame] | 1870 | ret = snd_card_new(card->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1871 | card->owner, 0, &card->snd_card); |
| 1872 | if (ret < 0) { |
Michał Mirosław | 10e8aa9 | 2013-05-04 22:21:38 +0200 | [diff] [blame] | 1873 | dev_err(card->dev, |
| 1874 | "ASoC: can't create sound card for card %s: %d\n", |
| 1875 | card->name, ret); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1876 | goto probe_end; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1877 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1878 | |
Lars-Peter Clausen | 0757d83 | 2015-04-09 10:52:36 +0200 | [diff] [blame] | 1879 | soc_init_card_debugfs(card); |
| 1880 | |
Kuninori Morimoto | b3da425 | 2019-08-07 10:31:24 +0900 | [diff] [blame] | 1881 | soc_resume_init(card); |
Andy Green | 6ed2597 | 2008-06-13 16:24:05 +0100 | [diff] [blame] | 1882 | |
Kuninori Morimoto | b8ba3b5 | 2019-08-07 10:30:53 +0900 | [diff] [blame] | 1883 | ret = snd_soc_dapm_new_controls(&card->dapm, card->dapm_widgets, |
| 1884 | card->num_dapm_widgets); |
| 1885 | if (ret < 0) |
| 1886 | goto probe_end; |
Mark Brown | 9a841eb | 2011-04-12 17:51:37 -0700 | [diff] [blame] | 1887 | |
Kuninori Morimoto | b8ba3b5 | 2019-08-07 10:30:53 +0900 | [diff] [blame] | 1888 | ret = snd_soc_dapm_new_controls(&card->dapm, card->of_dapm_widgets, |
| 1889 | card->num_of_dapm_widgets); |
| 1890 | if (ret < 0) |
| 1891 | goto probe_end; |
Nicolin Chen | f23e860 | 2015-02-14 17:22:49 -0800 | [diff] [blame] | 1892 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1893 | /* initialise the sound card only once */ |
| 1894 | if (card->probe) { |
Mark Brown | e7361ec | 2011-01-26 14:17:20 +0000 | [diff] [blame] | 1895 | ret = card->probe(card); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1896 | if (ret < 0) |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1897 | goto probe_end; |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1898 | card_probed = 1; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1901 | /* probe all components used by DAI links on this card */ |
Kuninori Morimoto | 62f07a6 | 2019-09-04 09:14:46 +0900 | [diff] [blame] | 1902 | ret = soc_probe_link_components(card); |
| 1903 | if (ret < 0) { |
| 1904 | dev_err(card->dev, |
| 1905 | "ASoC: failed to instantiate card %d\n", ret); |
| 1906 | goto probe_end; |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1907 | } |
| 1908 | |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1909 | /* probe auxiliary components */ |
| 1910 | ret = soc_probe_aux_devices(card); |
Kuninori Morimoto | 74bd3f9 | 2019-11-06 10:08:06 +0900 | [diff] [blame] | 1911 | if (ret < 0) { |
| 1912 | dev_err(card->dev, |
| 1913 | "ASoC: failed to probe aux component %d\n", ret); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1914 | goto probe_end; |
Kuninori Morimoto | 74bd3f9 | 2019-11-06 10:08:06 +0900 | [diff] [blame] | 1915 | } |
Mengdong Lin | f2ed6b0 | 2016-01-06 13:29:31 +0800 | [diff] [blame] | 1916 | |
Stephen Warren | 62ae68f | 2012-06-08 12:34:23 -0600 | [diff] [blame] | 1917 | /* probe all DAI links on this card */ |
Kuninori Morimoto | c7e7377 | 2019-09-04 09:15:17 +0900 | [diff] [blame] | 1918 | ret = soc_probe_link_dais(card); |
| 1919 | if (ret < 0) { |
| 1920 | dev_err(card->dev, |
| 1921 | "ASoC: failed to instantiate card %d\n", ret); |
| 1922 | goto probe_end; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Kuninori Morimoto | 626c2e5 | 2019-12-10 09:34:40 +0900 | [diff] [blame] | 1925 | for_each_card_rtds(card, rtd) { |
Kuninori Morimoto | eaffeef | 2019-12-10 09:34:44 +0900 | [diff] [blame] | 1926 | ret = soc_init_pcm_runtime(card, rtd); |
Kuninori Morimoto | 626c2e5 | 2019-12-10 09:34:40 +0900 | [diff] [blame] | 1927 | if (ret < 0) |
| 1928 | goto probe_end; |
| 1929 | } |
Kuninori Morimoto | c4b4698 | 2019-09-04 09:15:12 +0900 | [diff] [blame] | 1930 | |
Mark Brown | 888df39 | 2012-02-16 19:37:51 -0800 | [diff] [blame] | 1931 | snd_soc_dapm_link_dai_widgets(card); |
Liam Girdwood | b893ea5 | 2014-01-08 10:40:19 +0000 | [diff] [blame] | 1932 | snd_soc_dapm_connect_dai_link_widgets(card); |
Mark Brown | 888df39 | 2012-02-16 19:37:51 -0800 | [diff] [blame] | 1933 | |
Kuninori Morimoto | 9b98c7c | 2019-08-07 10:31:08 +0900 | [diff] [blame] | 1934 | ret = snd_soc_add_card_controls(card, card->controls, |
| 1935 | card->num_controls); |
| 1936 | if (ret < 0) |
| 1937 | goto probe_end; |
Mark Brown | b7af1da | 2011-04-07 19:18:44 +0900 | [diff] [blame] | 1938 | |
Kuninori Morimoto | daa480b | 2019-08-07 10:31:03 +0900 | [diff] [blame] | 1939 | ret = snd_soc_dapm_add_routes(&card->dapm, card->dapm_routes, |
| 1940 | card->num_dapm_routes); |
| 1941 | if (ret < 0) |
| 1942 | goto probe_end; |
Mark Brown | b8ad29d | 2011-03-02 18:35:51 +0000 | [diff] [blame] | 1943 | |
Kuninori Morimoto | daa480b | 2019-08-07 10:31:03 +0900 | [diff] [blame] | 1944 | ret = snd_soc_dapm_add_routes(&card->dapm, card->of_dapm_routes, |
| 1945 | card->num_of_dapm_routes); |
| 1946 | if (ret < 0) |
| 1947 | goto probe_end; |
Mark Brown | 75d9ac4 | 2011-09-27 16:41:01 +0100 | [diff] [blame] | 1948 | |
Takashi Iwai | 861886d | 2017-04-24 08:54:42 +0200 | [diff] [blame] | 1949 | /* try to set some sane longname if DMI is available */ |
| 1950 | snd_soc_set_dmi_name(card, NULL); |
| 1951 | |
Kuninori Morimoto | 0f23f71 | 2019-10-02 14:22:49 +0900 | [diff] [blame] | 1952 | soc_setup_card_name(card->snd_card->shortname, |
| 1953 | card->name, NULL, 0); |
| 1954 | soc_setup_card_name(card->snd_card->longname, |
| 1955 | card->long_name, card->name, 0); |
| 1956 | soc_setup_card_name(card->snd_card->driver, |
| 1957 | card->driver_name, card->name, 1); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1958 | |
Jaroslav Kysela | dc73d73 | 2019-11-19 18:49:32 +0100 | [diff] [blame] | 1959 | if (card->components) { |
| 1960 | /* the current implementation of snd_component_add() accepts */ |
| 1961 | /* multiple components in the string separated by space, */ |
| 1962 | /* but the string collision (identical string) check might */ |
| 1963 | /* not work correctly */ |
| 1964 | ret = snd_component_add(card->snd_card, card->components); |
| 1965 | if (ret < 0) { |
| 1966 | dev_err(card->dev, "ASoC: %s snd_component_add() failed: %d\n", |
| 1967 | card->name, ret); |
| 1968 | goto probe_end; |
| 1969 | } |
| 1970 | } |
| 1971 | |
Mark Brown | 28e9ad9 | 2011-03-02 18:36:34 +0000 | [diff] [blame] | 1972 | if (card->late_probe) { |
| 1973 | ret = card->late_probe(card); |
| 1974 | if (ret < 0) { |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 1975 | dev_err(card->dev, "ASoC: %s late_probe() failed: %d\n", |
Mark Brown | 28e9ad9 | 2011-03-02 18:36:34 +0000 | [diff] [blame] | 1976 | card->name, ret); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1977 | goto probe_end; |
Mark Brown | 28e9ad9 | 2011-03-02 18:36:34 +0000 | [diff] [blame] | 1978 | } |
| 1979 | } |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 1980 | card_probed = 1; |
Mark Brown | 28e9ad9 | 2011-03-02 18:36:34 +0000 | [diff] [blame] | 1981 | |
Lars-Peter Clausen | 824ef82 | 2013-08-27 15:51:01 +0200 | [diff] [blame] | 1982 | snd_soc_dapm_new_widgets(card); |
Lars-Peter Clausen | 8c193b8 | 2013-08-27 15:51:00 +0200 | [diff] [blame] | 1983 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1984 | ret = snd_card_register(card->snd_card); |
| 1985 | if (ret < 0) { |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 1986 | dev_err(card->dev, "ASoC: failed to register soundcard %d\n", |
| 1987 | ret); |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 1988 | goto probe_end; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 1991 | card->instantiated = 1; |
Tzung-Bi Shih | 882eab6 | 2018-11-14 17:06:13 +0800 | [diff] [blame] | 1992 | dapm_mark_endpoints_dirty(card); |
Mark Brown | 4f4c007 | 2011-10-07 14:29:19 +0100 | [diff] [blame] | 1993 | snd_soc_dapm_sync(&card->dapm); |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 1994 | |
Kuninori Morimoto | ed90c01 | 2019-11-06 10:07:56 +0900 | [diff] [blame] | 1995 | /* deactivate pins to sleep state */ |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 1996 | for_each_card_components(card, component) |
| 1997 | if (!component->active) |
| 1998 | pinctrl_pm_select_sleep_state(component->dev); |
Kuninori Morimoto | ed90c01 | 2019-11-06 10:07:56 +0900 | [diff] [blame] | 1999 | |
Kuninori Morimoto | 53e947a | 2019-01-21 09:32:42 +0900 | [diff] [blame] | 2000 | probe_end: |
| 2001 | if (ret < 0) |
Kuninori Morimoto | ce21401 | 2019-11-13 10:16:48 +0900 | [diff] [blame] | 2002 | soc_cleanup_card_resources(card, card_probed); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2003 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2004 | mutex_unlock(&card->mutex); |
Lars-Peter Clausen | 34e81ab | 2015-03-07 19:34:03 +0100 | [diff] [blame] | 2005 | mutex_unlock(&client_mutex); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2006 | |
Mark Brown | b19e6e7 | 2012-03-14 21:18:39 +0000 | [diff] [blame] | 2007 | return ret; |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /* probes a new socdev */ |
| 2011 | static int soc_probe(struct platform_device *pdev) |
| 2012 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2013 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2014 | |
Vinod Koul | 70a7ca3 | 2011-01-14 19:22:48 +0530 | [diff] [blame] | 2015 | /* |
| 2016 | * no card, so machine driver should be registering card |
| 2017 | * we should not be here in that case so ret error |
| 2018 | */ |
| 2019 | if (!card) |
| 2020 | return -EINVAL; |
| 2021 | |
Mark Brown | fe4085e | 2012-03-02 13:07:41 +0000 | [diff] [blame] | 2022 | dev_warn(&pdev->dev, |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 2023 | "ASoC: machine %s should use snd_soc_register_card()\n", |
Mark Brown | fe4085e | 2012-03-02 13:07:41 +0000 | [diff] [blame] | 2024 | card->name); |
| 2025 | |
Mark Brown | 435c5e2 | 2008-12-04 15:32:53 +0000 | [diff] [blame] | 2026 | /* Bodge while we unpick instantiation */ |
| 2027 | card->dev = &pdev->dev; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2028 | |
Mark Brown | 28d528c | 2012-08-09 18:45:23 +0100 | [diff] [blame] | 2029 | return snd_soc_register_card(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | /* removes a socdev */ |
| 2033 | static int soc_remove(struct platform_device *pdev) |
| 2034 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2035 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2036 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2037 | snd_soc_unregister_card(card); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
| 2040 | |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2041 | int snd_soc_poweroff(struct device *dev) |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2042 | { |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2043 | struct snd_soc_card *card = dev_get_drvdata(dev); |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 2044 | struct snd_soc_component *component; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2045 | |
| 2046 | if (!card->instantiated) |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 2047 | return 0; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2048 | |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * Flush out pmdown_time work - we actually do want to run it |
| 2051 | * now, we're shutting down so no imminent restart. |
| 2052 | */ |
Kuninori Morimoto | 65462e44 | 2019-01-21 09:32:37 +0900 | [diff] [blame] | 2053 | snd_soc_flush_all_delayed_work(card); |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2054 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2055 | snd_soc_dapm_shutdown(card); |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 2056 | |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 2057 | /* deactivate pins to sleep state */ |
Kuninori Morimoto | 76c39e8 | 2020-01-10 11:36:13 +0900 | [diff] [blame] | 2058 | for_each_card_components(card, component) |
| 2059 | pinctrl_pm_select_sleep_state(component->dev); |
Nicolin Chen | 988e8cc | 2013-11-04 14:57:31 +0800 | [diff] [blame] | 2060 | |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 2061 | return 0; |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2062 | } |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2063 | EXPORT_SYMBOL_GPL(snd_soc_poweroff); |
Mark Brown | 5173747 | 2009-06-22 13:16:51 +0100 | [diff] [blame] | 2064 | |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2065 | const struct dev_pm_ops snd_soc_pm_ops = { |
Viresh Kumar | b1dd589 | 2012-02-24 16:25:49 +0530 | [diff] [blame] | 2066 | .suspend = snd_soc_suspend, |
| 2067 | .resume = snd_soc_resume, |
| 2068 | .freeze = snd_soc_suspend, |
| 2069 | .thaw = snd_soc_resume, |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2070 | .poweroff = snd_soc_poweroff, |
Viresh Kumar | b1dd589 | 2012-02-24 16:25:49 +0530 | [diff] [blame] | 2071 | .restore = snd_soc_resume, |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 2072 | }; |
Stephen Warren | deb2607 | 2011-04-05 19:35:30 -0600 | [diff] [blame] | 2073 | EXPORT_SYMBOL_GPL(snd_soc_pm_ops); |
Mark Brown | 416356f | 2009-06-30 19:05:15 +0100 | [diff] [blame] | 2074 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2075 | /* ASoC platform driver */ |
| 2076 | static struct platform_driver soc_driver = { |
| 2077 | .driver = { |
| 2078 | .name = "soc-audio", |
Mark Brown | 6f8ab4a | 2011-01-26 14:59:27 +0000 | [diff] [blame] | 2079 | .pm = &snd_soc_pm_ops, |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2080 | }, |
| 2081 | .probe = soc_probe, |
| 2082 | .remove = soc_remove, |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2083 | }; |
| 2084 | |
Mark Brown | 096e49d | 2009-07-05 15:12:22 +0100 | [diff] [blame] | 2085 | /** |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2086 | * snd_soc_cnew - create new control |
| 2087 | * @_template: control template |
| 2088 | * @data: control private data |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2089 | * @long_name: control long name |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2090 | * @prefix: control name prefix |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2091 | * |
| 2092 | * Create a new mixer control from a template control. |
| 2093 | * |
| 2094 | * Returns 0 for success, else error. |
| 2095 | */ |
| 2096 | struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template, |
Mark Brown | 3056557 | 2012-02-16 17:07:42 -0800 | [diff] [blame] | 2097 | void *data, const char *long_name, |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2098 | const char *prefix) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2099 | { |
| 2100 | struct snd_kcontrol_new template; |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2101 | struct snd_kcontrol *kcontrol; |
| 2102 | char *name = NULL; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2103 | |
| 2104 | memcpy(&template, _template, sizeof(template)); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2105 | template.index = 0; |
| 2106 | |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2107 | if (!long_name) |
| 2108 | long_name = template.name; |
| 2109 | |
| 2110 | if (prefix) { |
Lars-Peter Clausen | 2b58107 | 2013-05-14 11:05:32 +0200 | [diff] [blame] | 2111 | name = kasprintf(GFP_KERNEL, "%s %s", prefix, long_name); |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2112 | if (!name) |
| 2113 | return NULL; |
| 2114 | |
Mark Brown | efb7ac3 | 2011-03-08 17:23:24 +0000 | [diff] [blame] | 2115 | template.name = name; |
| 2116 | } else { |
| 2117 | template.name = long_name; |
| 2118 | } |
| 2119 | |
| 2120 | kcontrol = snd_ctl_new1(&template, data); |
| 2121 | |
| 2122 | kfree(name); |
| 2123 | |
| 2124 | return kcontrol; |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2125 | } |
| 2126 | EXPORT_SYMBOL_GPL(snd_soc_cnew); |
| 2127 | |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 2128 | static int snd_soc_add_controls(struct snd_card *card, struct device *dev, |
| 2129 | const struct snd_kcontrol_new *controls, int num_controls, |
| 2130 | const char *prefix, void *data) |
| 2131 | { |
| 2132 | int err, i; |
| 2133 | |
| 2134 | for (i = 0; i < num_controls; i++) { |
| 2135 | const struct snd_kcontrol_new *control = &controls[i]; |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2136 | |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 2137 | err = snd_ctl_add(card, snd_soc_cnew(control, data, |
| 2138 | control->name, prefix)); |
| 2139 | if (err < 0) { |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 2140 | dev_err(dev, "ASoC: Failed to add %s: %d\n", |
| 2141 | control->name, err); |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 2142 | return err; |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
Dimitris Papastamos | 4fefd69 | 2013-07-29 13:51:58 +0100 | [diff] [blame] | 2149 | struct snd_kcontrol *snd_soc_card_get_kcontrol(struct snd_soc_card *soc_card, |
| 2150 | const char *name) |
| 2151 | { |
| 2152 | struct snd_card *card = soc_card->snd_card; |
| 2153 | struct snd_kcontrol *kctl; |
| 2154 | |
| 2155 | if (unlikely(!name)) |
| 2156 | return NULL; |
| 2157 | |
| 2158 | list_for_each_entry(kctl, &card->controls, list) |
| 2159 | if (!strncmp(kctl->id.name, name, sizeof(kctl->id.name))) |
| 2160 | return kctl; |
| 2161 | return NULL; |
| 2162 | } |
| 2163 | EXPORT_SYMBOL_GPL(snd_soc_card_get_kcontrol); |
| 2164 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 2165 | /** |
Lars-Peter Clausen | 0f2780a | 2014-07-17 22:01:08 +0200 | [diff] [blame] | 2166 | * snd_soc_add_component_controls - Add an array of controls to a component. |
| 2167 | * |
| 2168 | * @component: Component to add controls to |
| 2169 | * @controls: Array of controls to add |
| 2170 | * @num_controls: Number of elements in the array |
| 2171 | * |
| 2172 | * Return: 0 for success, else error. |
| 2173 | */ |
| 2174 | int snd_soc_add_component_controls(struct snd_soc_component *component, |
| 2175 | const struct snd_kcontrol_new *controls, unsigned int num_controls) |
| 2176 | { |
| 2177 | struct snd_card *card = component->card->snd_card; |
| 2178 | |
| 2179 | return snd_soc_add_controls(card, component->dev, controls, |
| 2180 | num_controls, component->name_prefix, component); |
| 2181 | } |
| 2182 | EXPORT_SYMBOL_GPL(snd_soc_add_component_controls); |
| 2183 | |
| 2184 | /** |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 2185 | * snd_soc_add_card_controls - add an array of controls to a SoC card. |
| 2186 | * Convenience function to add a list of controls. |
| 2187 | * |
| 2188 | * @soc_card: SoC card to add controls to |
| 2189 | * @controls: array of controls to add |
| 2190 | * @num_controls: number of elements in the array |
| 2191 | * |
| 2192 | * Return 0 for success, else error. |
| 2193 | */ |
| 2194 | int snd_soc_add_card_controls(struct snd_soc_card *soc_card, |
| 2195 | const struct snd_kcontrol_new *controls, int num_controls) |
| 2196 | { |
| 2197 | struct snd_card *card = soc_card->snd_card; |
| 2198 | |
| 2199 | return snd_soc_add_controls(card, soc_card->dev, controls, num_controls, |
| 2200 | NULL, soc_card); |
| 2201 | } |
| 2202 | EXPORT_SYMBOL_GPL(snd_soc_add_card_controls); |
| 2203 | |
| 2204 | /** |
| 2205 | * snd_soc_add_dai_controls - add an array of controls to a DAI. |
| 2206 | * Convienience function to add a list of controls. |
| 2207 | * |
| 2208 | * @dai: DAI to add controls to |
| 2209 | * @controls: array of controls to add |
| 2210 | * @num_controls: number of elements in the array |
| 2211 | * |
| 2212 | * Return 0 for success, else error. |
| 2213 | */ |
| 2214 | int snd_soc_add_dai_controls(struct snd_soc_dai *dai, |
| 2215 | const struct snd_kcontrol_new *controls, int num_controls) |
| 2216 | { |
Lars-Peter Clausen | 313665b | 2014-11-04 11:30:58 +0100 | [diff] [blame] | 2217 | struct snd_card *card = dai->component->card->snd_card; |
Liam Girdwood | 022658b | 2012-02-03 17:43:09 +0000 | [diff] [blame] | 2218 | |
| 2219 | return snd_soc_add_controls(card, dai->dev, controls, num_controls, |
| 2220 | NULL, dai); |
| 2221 | } |
| 2222 | EXPORT_SYMBOL_GPL(snd_soc_add_dai_controls); |
| 2223 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2224 | /** |
| 2225 | * snd_soc_register_card - Register a card with the ASoC core |
| 2226 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2227 | * @card: Card to register |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2228 | * |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2229 | */ |
Vinod Koul | 70a7ca3 | 2011-01-14 19:22:48 +0530 | [diff] [blame] | 2230 | int snd_soc_register_card(struct snd_soc_card *card) |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2231 | { |
| 2232 | if (!card->name || !card->dev) |
| 2233 | return -EINVAL; |
| 2234 | |
Mark Brown | ed77cc1 | 2011-05-03 18:25:34 +0100 | [diff] [blame] | 2235 | dev_set_drvdata(card->dev, card); |
| 2236 | |
Kuninori Morimoto | b03bfae | 2019-08-20 14:04:54 +0900 | [diff] [blame] | 2237 | INIT_LIST_HEAD(&card->widgets); |
| 2238 | INIT_LIST_HEAD(&card->paths); |
| 2239 | INIT_LIST_HEAD(&card->dapm_list); |
| 2240 | INIT_LIST_HEAD(&card->aux_comp_list); |
| 2241 | INIT_LIST_HEAD(&card->component_dev_list); |
| 2242 | INIT_LIST_HEAD(&card->list); |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame] | 2243 | INIT_LIST_HEAD(&card->rtd_list); |
Mark Brown | db432b4 | 2011-10-03 21:06:40 +0100 | [diff] [blame] | 2244 | INIT_LIST_HEAD(&card->dapm_dirty); |
Liam Girdwood | 8a97823 | 2015-05-29 19:06:14 +0100 | [diff] [blame] | 2245 | INIT_LIST_HEAD(&card->dobj_list); |
Kuninori Morimoto | b03bfae | 2019-08-20 14:04:54 +0900 | [diff] [blame] | 2246 | |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2247 | card->instantiated = 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2248 | mutex_init(&card->mutex); |
Liam Girdwood | a73fb2d | 2012-03-07 10:38:26 +0000 | [diff] [blame] | 2249 | mutex_init(&card->dapm_mutex); |
Peter Ujfalusi | 72b745e | 2019-08-13 13:45:32 +0300 | [diff] [blame] | 2250 | mutex_init(&card->pcm_mutex); |
KaiChieh Chuang | a976486 | 2019-03-08 13:05:53 +0800 | [diff] [blame] | 2251 | spin_lock_init(&card->dpcm_lock); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2252 | |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 2253 | return snd_soc_bind_card(card); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2254 | } |
Vinod Koul | 70a7ca3 | 2011-01-14 19:22:48 +0530 | [diff] [blame] | 2255 | EXPORT_SYMBOL_GPL(snd_soc_register_card); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2256 | |
| 2257 | /** |
| 2258 | * snd_soc_unregister_card - Unregister a card with the ASoC core |
| 2259 | * |
Mark Brown | ac11a2b | 2009-01-01 12:18:17 +0000 | [diff] [blame] | 2260 | * @card: Card to unregister |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2261 | * |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2262 | */ |
Vinod Koul | 70a7ca3 | 2011-01-14 19:22:48 +0530 | [diff] [blame] | 2263 | int snd_soc_unregister_card(struct snd_soc_card *card) |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2264 | { |
Kuninori Morimoto | b545542 | 2019-06-19 10:07:19 +0900 | [diff] [blame] | 2265 | mutex_lock(&client_mutex); |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 2266 | snd_soc_unbind_card(card, true); |
Kuninori Morimoto | b545542 | 2019-06-19 10:07:19 +0900 | [diff] [blame] | 2267 | mutex_unlock(&client_mutex); |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 2268 | dev_dbg(card->dev, "ASoC: Unregistered card '%s'\n", card->name); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2269 | |
| 2270 | return 0; |
| 2271 | } |
Vinod Koul | 70a7ca3 | 2011-01-14 19:22:48 +0530 | [diff] [blame] | 2272 | EXPORT_SYMBOL_GPL(snd_soc_unregister_card); |
Mark Brown | c5af3a2 | 2008-11-28 13:29:45 +0000 | [diff] [blame] | 2273 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2274 | /* |
| 2275 | * Simplify DAI link configuration by removing ".-1" from device names |
| 2276 | * and sanitizing names. |
| 2277 | */ |
Dimitris Papastamos | 0b9a214 | 2010-12-06 15:49:20 +0000 | [diff] [blame] | 2278 | static char *fmt_single_name(struct device *dev, int *id) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2279 | { |
| 2280 | char *found, name[NAME_SIZE]; |
| 2281 | int id1, id2; |
| 2282 | |
| 2283 | if (dev_name(dev) == NULL) |
| 2284 | return NULL; |
| 2285 | |
Dimitris Papastamos | 58818a7 | 2010-12-06 15:42:17 +0000 | [diff] [blame] | 2286 | strlcpy(name, dev_name(dev), NAME_SIZE); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2287 | |
| 2288 | /* are we a "%s.%d" name (platform and SPI components) */ |
| 2289 | found = strstr(name, dev->driver->name); |
| 2290 | if (found) { |
| 2291 | /* get ID */ |
| 2292 | if (sscanf(&found[strlen(dev->driver->name)], ".%d", id) == 1) { |
| 2293 | |
| 2294 | /* discard ID from name if ID == -1 */ |
| 2295 | if (*id == -1) |
| 2296 | found[strlen(dev->driver->name)] = '\0'; |
| 2297 | } |
| 2298 | |
| 2299 | } else { |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2300 | /* I2C component devices are named "bus-addr" */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2301 | if (sscanf(name, "%x-%x", &id1, &id2) == 2) { |
| 2302 | char tmp[NAME_SIZE]; |
| 2303 | |
| 2304 | /* create unique ID number from I2C addr and bus */ |
Jarkko Nikula | 0589944 | 2010-10-19 11:10:45 +0300 | [diff] [blame] | 2305 | *id = ((id1 & 0xffff) << 16) + id2; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2306 | |
| 2307 | /* sanitize component name for DAI link creation */ |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2308 | snprintf(tmp, NAME_SIZE, "%s.%s", dev->driver->name, |
| 2309 | name); |
Dimitris Papastamos | 58818a7 | 2010-12-06 15:42:17 +0000 | [diff] [blame] | 2310 | strlcpy(name, tmp, NAME_SIZE); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2311 | } else |
| 2312 | *id = 0; |
| 2313 | } |
| 2314 | |
Kuninori Morimoto | 5001449 | 2019-10-02 14:22:57 +0900 | [diff] [blame] | 2315 | return devm_kstrdup(dev, name, GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | /* |
| 2319 | * Simplify DAI link naming for single devices with multiple DAIs by removing |
| 2320 | * any ".-1" and using the DAI name (instead of device name). |
| 2321 | */ |
| 2322 | static inline char *fmt_multiple_name(struct device *dev, |
| 2323 | struct snd_soc_dai_driver *dai_drv) |
| 2324 | { |
| 2325 | if (dai_drv->name == NULL) { |
Michał Mirosław | 10e8aa9 | 2013-05-04 22:21:38 +0200 | [diff] [blame] | 2326 | dev_err(dev, |
| 2327 | "ASoC: error - multiple DAI %s registered with no name\n", |
| 2328 | dev_name(dev)); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2329 | return NULL; |
| 2330 | } |
| 2331 | |
Kuninori Morimoto | 5001449 | 2019-10-02 14:22:57 +0900 | [diff] [blame] | 2332 | return devm_kstrdup(dev, dai_drv->name, GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Kuninori Morimoto | ffdbca0 | 2019-11-06 10:07:23 +0900 | [diff] [blame] | 2335 | void snd_soc_unregister_dai(struct snd_soc_dai *dai) |
Kuninori Morimoto | e11381f | 2019-11-05 15:47:04 +0900 | [diff] [blame] | 2336 | { |
| 2337 | dev_dbg(dai->dev, "ASoC: Unregistered DAI '%s'\n", dai->name); |
| 2338 | list_del(&dai->list); |
| 2339 | } |
Kuninori Morimoto | ffdbca0 | 2019-11-06 10:07:23 +0900 | [diff] [blame] | 2340 | EXPORT_SYMBOL_GPL(snd_soc_unregister_dai); |
Kuninori Morimoto | e11381f | 2019-11-05 15:47:04 +0900 | [diff] [blame] | 2341 | |
Kuninori Morimoto | 7ca2438 | 2019-11-06 10:07:17 +0900 | [diff] [blame] | 2342 | /** |
| 2343 | * snd_soc_register_dai - Register a DAI dynamically & create its widgets |
| 2344 | * |
| 2345 | * @component: The component the DAIs are registered for |
| 2346 | * @dai_drv: DAI driver to use for the DAI |
Randy Dunlap | bc9a665 | 2019-12-08 20:37:04 -0800 | [diff] [blame] | 2347 | * @legacy_dai_naming: if %true, use legacy single-name format; |
| 2348 | * if %false, use multiple-name format; |
Kuninori Morimoto | 7ca2438 | 2019-11-06 10:07:17 +0900 | [diff] [blame] | 2349 | * |
| 2350 | * Topology can use this API to register DAIs when probing a component. |
| 2351 | * These DAIs's widgets will be freed in the card cleanup and the DAIs |
| 2352 | * will be freed in the component cleanup. |
| 2353 | */ |
| 2354 | struct snd_soc_dai *snd_soc_register_dai(struct snd_soc_component *component, |
| 2355 | struct snd_soc_dai_driver *dai_drv, |
| 2356 | bool legacy_dai_naming) |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2357 | { |
| 2358 | struct device *dev = component->dev; |
| 2359 | struct snd_soc_dai *dai; |
| 2360 | |
| 2361 | dev_dbg(dev, "ASoC: dynamically register DAI %s\n", dev_name(dev)); |
| 2362 | |
Kuninori Morimoto | 7ca2438 | 2019-11-06 10:07:17 +0900 | [diff] [blame] | 2363 | lockdep_assert_held(&client_mutex); |
| 2364 | |
Kuninori Morimoto | 5001449 | 2019-10-02 14:22:57 +0900 | [diff] [blame] | 2365 | dai = devm_kzalloc(dev, sizeof(*dai), GFP_KERNEL); |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2366 | if (dai == NULL) |
| 2367 | return NULL; |
| 2368 | |
| 2369 | /* |
| 2370 | * Back in the old days when we still had component-less DAIs, |
| 2371 | * instead of having a static name, component-less DAIs would |
| 2372 | * inherit the name of the parent device so it is possible to |
| 2373 | * register multiple instances of the DAI. We still need to keep |
| 2374 | * the same naming style even though those DAIs are not |
| 2375 | * component-less anymore. |
| 2376 | */ |
| 2377 | if (legacy_dai_naming && |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2378 | (dai_drv->id == 0 || dai_drv->name == NULL)) { |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2379 | dai->name = fmt_single_name(dev, &dai->id); |
| 2380 | } else { |
| 2381 | dai->name = fmt_multiple_name(dev, dai_drv); |
| 2382 | if (dai_drv->id) |
| 2383 | dai->id = dai_drv->id; |
| 2384 | else |
| 2385 | dai->id = component->num_dai; |
| 2386 | } |
Kuninori Morimoto | 5001449 | 2019-10-02 14:22:57 +0900 | [diff] [blame] | 2387 | if (!dai->name) |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2388 | return NULL; |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2389 | |
| 2390 | dai->component = component; |
| 2391 | dai->dev = dev; |
| 2392 | dai->driver = dai_drv; |
| 2393 | if (!dai->driver->ops) |
| 2394 | dai->driver->ops = &null_dai_ops; |
| 2395 | |
Kuninori Morimoto | 15a0c64 | 2018-09-21 05:23:17 +0000 | [diff] [blame] | 2396 | /* see for_each_component_dais */ |
Kuninori Morimoto | 58bf417 | 2017-12-20 01:48:29 +0000 | [diff] [blame] | 2397 | list_add_tail(&dai->list, &component->dai_list); |
Mengdong Lin | 5e4fb37 | 2015-12-31 16:40:20 +0800 | [diff] [blame] | 2398 | component->num_dai++; |
| 2399 | |
| 2400 | dev_dbg(dev, "ASoC: Registered DAI '%s'\n", dai->name); |
| 2401 | return dai; |
| 2402 | } |
Kuninori Morimoto | e11381f | 2019-11-05 15:47:04 +0900 | [diff] [blame] | 2403 | |
Mark Brown | 9115171 | 2008-11-30 23:31:24 +0000 | [diff] [blame] | 2404 | /** |
Kuninori Morimoto | 3f6674a | 2019-11-05 15:47:00 +0900 | [diff] [blame] | 2405 | * snd_soc_unregister_dai - Unregister DAIs from the ASoC core |
| 2406 | * |
| 2407 | * @component: The component for which the DAIs should be unregistered |
| 2408 | */ |
| 2409 | static void snd_soc_unregister_dais(struct snd_soc_component *component) |
| 2410 | { |
| 2411 | struct snd_soc_dai *dai, *_dai; |
| 2412 | |
Kuninori Morimoto | e11381f | 2019-11-05 15:47:04 +0900 | [diff] [blame] | 2413 | for_each_component_dais_safe(component, dai, _dai) |
| 2414 | snd_soc_unregister_dai(dai); |
Kuninori Morimoto | 3f6674a | 2019-11-05 15:47:00 +0900 | [diff] [blame] | 2415 | } |
| 2416 | |
| 2417 | /** |
Kuninori Morimoto | daf7737 | 2019-11-05 15:46:55 +0900 | [diff] [blame] | 2418 | * snd_soc_register_dais - Register a DAI with the ASoC core |
| 2419 | * |
| 2420 | * @component: The component the DAIs are registered for |
| 2421 | * @dai_drv: DAI driver to use for the DAIs |
| 2422 | * @count: Number of DAIs |
| 2423 | */ |
| 2424 | static int snd_soc_register_dais(struct snd_soc_component *component, |
| 2425 | struct snd_soc_dai_driver *dai_drv, |
| 2426 | size_t count) |
| 2427 | { |
Kuninori Morimoto | daf7737 | 2019-11-05 15:46:55 +0900 | [diff] [blame] | 2428 | struct snd_soc_dai *dai; |
| 2429 | unsigned int i; |
| 2430 | int ret; |
| 2431 | |
Kuninori Morimoto | daf7737 | 2019-11-05 15:46:55 +0900 | [diff] [blame] | 2432 | for (i = 0; i < count; i++) { |
Kuninori Morimoto | 71cb85f | 2019-11-05 15:47:18 +0900 | [diff] [blame] | 2433 | dai = snd_soc_register_dai(component, dai_drv + i, count == 1 && |
Kuninori Morimoto | daf7737 | 2019-11-05 15:46:55 +0900 | [diff] [blame] | 2434 | !component->driver->non_legacy_dai_naming); |
| 2435 | if (dai == NULL) { |
| 2436 | ret = -ENOMEM; |
| 2437 | goto err; |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | return 0; |
| 2442 | |
| 2443 | err: |
| 2444 | snd_soc_unregister_dais(component); |
| 2445 | |
| 2446 | return ret; |
| 2447 | } |
| 2448 | |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2449 | static int snd_soc_component_initialize(struct snd_soc_component *component, |
| 2450 | const struct snd_soc_component_driver *driver, struct device *dev) |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2451 | { |
Kuninori Morimoto | 8d92bb5 | 2019-08-20 14:05:16 +0900 | [diff] [blame] | 2452 | INIT_LIST_HEAD(&component->dai_list); |
Kuninori Morimoto | 495efdb | 2019-08-20 14:05:20 +0900 | [diff] [blame] | 2453 | INIT_LIST_HEAD(&component->dobj_list); |
| 2454 | INIT_LIST_HEAD(&component->card_list); |
Kuninori Morimoto | 8d92bb5 | 2019-08-20 14:05:16 +0900 | [diff] [blame] | 2455 | mutex_init(&component->io_mutex); |
| 2456 | |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2457 | component->name = fmt_single_name(dev, &component->id); |
| 2458 | if (!component->name) { |
| 2459 | dev_err(dev, "ASoC: Failed to allocate name\n"); |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2460 | return -ENOMEM; |
| 2461 | } |
| 2462 | |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2463 | component->dev = dev; |
| 2464 | component->driver = driver; |
Lars-Peter Clausen | e2c330b | 2014-04-22 13:23:13 +0200 | [diff] [blame] | 2465 | |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2466 | return 0; |
| 2467 | } |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2468 | |
Lars-Peter Clausen | 20feb88 | 2014-11-18 19:45:52 +0100 | [diff] [blame] | 2469 | static void snd_soc_component_setup_regmap(struct snd_soc_component *component) |
Lars-Peter Clausen | 886f569 | 2014-08-19 15:51:28 +0200 | [diff] [blame] | 2470 | { |
Lars-Peter Clausen | 20feb88 | 2014-11-18 19:45:52 +0100 | [diff] [blame] | 2471 | int val_bytes = regmap_get_val_bytes(component->regmap); |
| 2472 | |
| 2473 | /* Errors are legitimate for non-integer byte multiples */ |
| 2474 | if (val_bytes > 0) |
| 2475 | component->val_bytes = val_bytes; |
Lars-Peter Clausen | 886f569 | 2014-08-19 15:51:28 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
Lars-Peter Clausen | e874bf5 | 2014-11-25 21:41:03 +0100 | [diff] [blame] | 2478 | #ifdef CONFIG_REGMAP |
| 2479 | |
Lars-Peter Clausen | 20feb88 | 2014-11-18 19:45:52 +0100 | [diff] [blame] | 2480 | /** |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2481 | * snd_soc_component_init_regmap() - Initialize regmap instance for the |
| 2482 | * component |
Lars-Peter Clausen | 20feb88 | 2014-11-18 19:45:52 +0100 | [diff] [blame] | 2483 | * @component: The component for which to initialize the regmap instance |
| 2484 | * @regmap: The regmap instance that should be used by the component |
| 2485 | * |
| 2486 | * This function allows deferred assignment of the regmap instance that is |
| 2487 | * associated with the component. Only use this if the regmap instance is not |
| 2488 | * yet ready when the component is registered. The function must also be called |
| 2489 | * before the first IO attempt of the component. |
| 2490 | */ |
| 2491 | void snd_soc_component_init_regmap(struct snd_soc_component *component, |
| 2492 | struct regmap *regmap) |
| 2493 | { |
| 2494 | component->regmap = regmap; |
| 2495 | snd_soc_component_setup_regmap(component); |
| 2496 | } |
| 2497 | EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap); |
| 2498 | |
| 2499 | /** |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 2500 | * snd_soc_component_exit_regmap() - De-initialize regmap instance for the |
| 2501 | * component |
Lars-Peter Clausen | 20feb88 | 2014-11-18 19:45:52 +0100 | [diff] [blame] | 2502 | * @component: The component for which to de-initialize the regmap instance |
| 2503 | * |
| 2504 | * Calls regmap_exit() on the regmap instance associated to the component and |
| 2505 | * removes the regmap instance from the component. |
| 2506 | * |
| 2507 | * This function should only be used if snd_soc_component_init_regmap() was used |
| 2508 | * to initialize the regmap instance. |
| 2509 | */ |
| 2510 | void snd_soc_component_exit_regmap(struct snd_soc_component *component) |
| 2511 | { |
| 2512 | regmap_exit(component->regmap); |
| 2513 | component->regmap = NULL; |
| 2514 | } |
| 2515 | EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap); |
| 2516 | |
Lars-Peter Clausen | e874bf5 | 2014-11-25 21:41:03 +0100 | [diff] [blame] | 2517 | #endif |
| 2518 | |
Kuninori Morimoto | 273d778 | 2017-10-11 01:38:29 +0000 | [diff] [blame] | 2519 | #define ENDIANNESS_MAP(name) \ |
| 2520 | (SNDRV_PCM_FMTBIT_##name##LE | SNDRV_PCM_FMTBIT_##name##BE) |
| 2521 | static u64 endianness_format_map[] = { |
| 2522 | ENDIANNESS_MAP(S16_), |
| 2523 | ENDIANNESS_MAP(U16_), |
| 2524 | ENDIANNESS_MAP(S24_), |
| 2525 | ENDIANNESS_MAP(U24_), |
| 2526 | ENDIANNESS_MAP(S32_), |
| 2527 | ENDIANNESS_MAP(U32_), |
| 2528 | ENDIANNESS_MAP(S24_3), |
| 2529 | ENDIANNESS_MAP(U24_3), |
| 2530 | ENDIANNESS_MAP(S20_3), |
| 2531 | ENDIANNESS_MAP(U20_3), |
| 2532 | ENDIANNESS_MAP(S18_3), |
| 2533 | ENDIANNESS_MAP(U18_3), |
| 2534 | ENDIANNESS_MAP(FLOAT_), |
| 2535 | ENDIANNESS_MAP(FLOAT64_), |
| 2536 | ENDIANNESS_MAP(IEC958_SUBFRAME_), |
| 2537 | }; |
| 2538 | |
| 2539 | /* |
| 2540 | * Fix up the DAI formats for endianness: codecs don't actually see |
| 2541 | * the endianness of the data but we're using the CPU format |
| 2542 | * definitions which do need to include endianness so we ensure that |
| 2543 | * codec DAIs always have both big and little endian variants set. |
| 2544 | */ |
| 2545 | static void convert_endianness_formats(struct snd_soc_pcm_stream *stream) |
| 2546 | { |
| 2547 | int i; |
| 2548 | |
| 2549 | for (i = 0; i < ARRAY_SIZE(endianness_format_map); i++) |
| 2550 | if (stream->formats & endianness_format_map[i]) |
| 2551 | stream->formats |= endianness_format_map[i]; |
| 2552 | } |
| 2553 | |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 2554 | static void snd_soc_try_rebind_card(void) |
| 2555 | { |
| 2556 | struct snd_soc_card *card, *c; |
| 2557 | |
Kuninori Morimoto | b245d27 | 2019-08-07 10:31:19 +0900 | [diff] [blame] | 2558 | list_for_each_entry_safe(card, c, &unbind_card_list, list) |
| 2559 | if (!snd_soc_bind_card(card)) |
| 2560 | list_del(&card->list); |
Srinivas Kandagatla | e894efe | 2018-09-12 10:15:00 +0100 | [diff] [blame] | 2561 | } |
| 2562 | |
Kuninori Morimoto | 486c797 | 2019-11-05 15:46:39 +0900 | [diff] [blame] | 2563 | static void snd_soc_del_component_unlocked(struct snd_soc_component *component) |
| 2564 | { |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2565 | struct snd_soc_card *card = component->card; |
| 2566 | |
Kuninori Morimoto | 486c797 | 2019-11-05 15:46:39 +0900 | [diff] [blame] | 2567 | snd_soc_unregister_dais(component); |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2568 | |
| 2569 | if (card) |
| 2570 | snd_soc_unbind_card(card, false); |
| 2571 | |
| 2572 | list_del(&component->list); |
Kuninori Morimoto | 486c797 | 2019-11-05 15:46:39 +0900 | [diff] [blame] | 2573 | } |
| 2574 | |
Kuninori Morimoto | e0dac41 | 2017-10-02 05:10:17 +0000 | [diff] [blame] | 2575 | int snd_soc_add_component(struct device *dev, |
| 2576 | struct snd_soc_component *component, |
| 2577 | const struct snd_soc_component_driver *component_driver, |
| 2578 | struct snd_soc_dai_driver *dai_drv, |
| 2579 | int num_dai) |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2580 | { |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2581 | int ret; |
Kuninori Morimoto | 273d778 | 2017-10-11 01:38:29 +0000 | [diff] [blame] | 2582 | int i; |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2583 | |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2584 | mutex_lock(&client_mutex); |
| 2585 | |
Kuninori Morimoto | cf9e829 | 2017-08-07 02:06:23 +0000 | [diff] [blame] | 2586 | ret = snd_soc_component_initialize(component, component_driver, dev); |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2587 | if (ret) |
| 2588 | goto err_free; |
| 2589 | |
Kuninori Morimoto | 273d778 | 2017-10-11 01:38:29 +0000 | [diff] [blame] | 2590 | if (component_driver->endianness) { |
| 2591 | for (i = 0; i < num_dai; i++) { |
| 2592 | convert_endianness_formats(&dai_drv[i].playback); |
| 2593 | convert_endianness_formats(&dai_drv[i].capture); |
| 2594 | } |
| 2595 | } |
| 2596 | |
Kuninori Morimoto | 0e7b25c | 2018-05-08 03:23:01 +0000 | [diff] [blame] | 2597 | ret = snd_soc_register_dais(component, dai_drv, num_dai); |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2598 | if (ret < 0) { |
Masanari Iida | f42cf8d | 2015-02-24 23:11:26 +0900 | [diff] [blame] | 2599 | dev_err(dev, "ASoC: Failed to register DAIs: %d\n", ret); |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2600 | goto err_cleanup; |
| 2601 | } |
| 2602 | |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2603 | if (!component->driver->write && !component->driver->read) { |
| 2604 | if (!component->regmap) |
| 2605 | component->regmap = dev_get_regmap(component->dev, |
| 2606 | NULL); |
| 2607 | if (component->regmap) |
| 2608 | snd_soc_component_setup_regmap(component); |
| 2609 | } |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2610 | |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2611 | /* see for_each_component */ |
| 2612 | list_add(&component->list, &component_list); |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2613 | |
| 2614 | err_cleanup: |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2615 | if (ret < 0) |
| 2616 | snd_soc_del_component_unlocked(component); |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2617 | err_free: |
Kuninori Morimoto | b18768f | 2019-11-05 15:46:45 +0900 | [diff] [blame] | 2618 | mutex_unlock(&client_mutex); |
| 2619 | |
| 2620 | if (ret == 0) |
| 2621 | snd_soc_try_rebind_card(); |
| 2622 | |
Lars-Peter Clausen | bb13109 | 2014-06-16 18:13:03 +0200 | [diff] [blame] | 2623 | return ret; |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2624 | } |
Kuninori Morimoto | e0dac41 | 2017-10-02 05:10:17 +0000 | [diff] [blame] | 2625 | EXPORT_SYMBOL_GPL(snd_soc_add_component); |
| 2626 | |
| 2627 | int snd_soc_register_component(struct device *dev, |
| 2628 | const struct snd_soc_component_driver *component_driver, |
| 2629 | struct snd_soc_dai_driver *dai_drv, |
| 2630 | int num_dai) |
| 2631 | { |
| 2632 | struct snd_soc_component *component; |
| 2633 | |
Kuninori Morimoto | 7ecbd6a | 2018-03-19 07:27:17 +0000 | [diff] [blame] | 2634 | component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL); |
Kuninori Morimoto | 08e61d0 | 2017-10-02 05:10:33 +0000 | [diff] [blame] | 2635 | if (!component) |
Kuninori Morimoto | e0dac41 | 2017-10-02 05:10:17 +0000 | [diff] [blame] | 2636 | return -ENOMEM; |
Kuninori Morimoto | e0dac41 | 2017-10-02 05:10:17 +0000 | [diff] [blame] | 2637 | |
| 2638 | return snd_soc_add_component(dev, component, component_driver, |
| 2639 | dai_drv, num_dai); |
| 2640 | } |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2641 | EXPORT_SYMBOL_GPL(snd_soc_register_component); |
| 2642 | |
| 2643 | /** |
Kuninori Morimoto | 2eccea8 | 2017-08-07 02:06:55 +0000 | [diff] [blame] | 2644 | * snd_soc_unregister_component - Unregister all related component |
| 2645 | * from the ASoC core |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2646 | * |
Jonathan Corbet | 628536e | 2015-08-25 01:14:48 -0600 | [diff] [blame] | 2647 | * @dev: The device to unregister |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2648 | */ |
Kuninori Morimoto | 2eccea8 | 2017-08-07 02:06:55 +0000 | [diff] [blame] | 2649 | void snd_soc_unregister_component(struct device *dev) |
| 2650 | { |
Kuninori Morimoto | ac6a4dd | 2019-11-05 15:46:51 +0900 | [diff] [blame] | 2651 | struct snd_soc_component *component; |
| 2652 | |
| 2653 | mutex_lock(&client_mutex); |
| 2654 | while (1) { |
Kuninori Morimoto | 18dd66e | 2019-11-06 16:05:05 +0900 | [diff] [blame] | 2655 | component = snd_soc_lookup_component_nolocked(dev, NULL); |
Kuninori Morimoto | ac6a4dd | 2019-11-05 15:46:51 +0900 | [diff] [blame] | 2656 | if (!component) |
| 2657 | break; |
| 2658 | |
| 2659 | snd_soc_del_component_unlocked(component); |
| 2660 | } |
| 2661 | mutex_unlock(&client_mutex); |
Kuninori Morimoto | d191bd8 | 2013-09-04 19:39:03 -0700 | [diff] [blame] | 2662 | } |
| 2663 | EXPORT_SYMBOL_GPL(snd_soc_unregister_component); |
| 2664 | |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2665 | /* Retrieve a card's name from device tree */ |
Kuninori Morimoto | b07609ce | 2017-01-27 06:37:51 +0000 | [diff] [blame] | 2666 | int snd_soc_of_parse_card_name(struct snd_soc_card *card, |
| 2667 | const char *propname) |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2668 | { |
Kuninori Morimoto | b07609ce | 2017-01-27 06:37:51 +0000 | [diff] [blame] | 2669 | struct device_node *np; |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2670 | int ret; |
| 2671 | |
Tushar Behera | 7e07e7c | 2014-07-04 14:23:00 +0530 | [diff] [blame] | 2672 | if (!card->dev) { |
| 2673 | pr_err("card->dev is not set before calling %s\n", __func__); |
| 2674 | return -EINVAL; |
| 2675 | } |
| 2676 | |
Kuninori Morimoto | b07609ce | 2017-01-27 06:37:51 +0000 | [diff] [blame] | 2677 | np = card->dev->of_node; |
Tushar Behera | 7e07e7c | 2014-07-04 14:23:00 +0530 | [diff] [blame] | 2678 | |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2679 | ret = of_property_read_string_index(np, propname, 0, &card->name); |
| 2680 | /* |
| 2681 | * EINVAL means the property does not exist. This is fine providing |
| 2682 | * card->name was previously set, which is checked later in |
| 2683 | * snd_soc_register_card. |
| 2684 | */ |
| 2685 | if (ret < 0 && ret != -EINVAL) { |
| 2686 | dev_err(card->dev, |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 2687 | "ASoC: Property '%s' could not be read: %d\n", |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2688 | propname, ret); |
| 2689 | return ret; |
| 2690 | } |
| 2691 | |
| 2692 | return 0; |
| 2693 | } |
Kuninori Morimoto | b07609ce | 2017-01-27 06:37:51 +0000 | [diff] [blame] | 2694 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_card_name); |
Stephen Warren | bec4fa0 | 2011-12-12 15:55:34 -0700 | [diff] [blame] | 2695 | |
Xiubo Li | 9a6d486 | 2014-02-08 15:59:52 +0800 | [diff] [blame] | 2696 | static const struct snd_soc_dapm_widget simple_widgets[] = { |
| 2697 | SND_SOC_DAPM_MIC("Microphone", NULL), |
| 2698 | SND_SOC_DAPM_LINE("Line", NULL), |
| 2699 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 2700 | SND_SOC_DAPM_SPK("Speaker", NULL), |
| 2701 | }; |
| 2702 | |
Kuninori Morimoto | 21efde5 | 2017-01-27 06:37:34 +0000 | [diff] [blame] | 2703 | int snd_soc_of_parse_audio_simple_widgets(struct snd_soc_card *card, |
Xiubo Li | 9a6d486 | 2014-02-08 15:59:52 +0800 | [diff] [blame] | 2704 | const char *propname) |
| 2705 | { |
Kuninori Morimoto | 21efde5 | 2017-01-27 06:37:34 +0000 | [diff] [blame] | 2706 | struct device_node *np = card->dev->of_node; |
Xiubo Li | 9a6d486 | 2014-02-08 15:59:52 +0800 | [diff] [blame] | 2707 | struct snd_soc_dapm_widget *widgets; |
| 2708 | const char *template, *wname; |
| 2709 | int i, j, num_widgets, ret; |
| 2710 | |
| 2711 | num_widgets = of_property_count_strings(np, propname); |
| 2712 | if (num_widgets < 0) { |
| 2713 | dev_err(card->dev, |
| 2714 | "ASoC: Property '%s' does not exist\n", propname); |
| 2715 | return -EINVAL; |
| 2716 | } |
| 2717 | if (num_widgets & 1) { |
| 2718 | dev_err(card->dev, |
| 2719 | "ASoC: Property '%s' length is not even\n", propname); |
| 2720 | return -EINVAL; |
| 2721 | } |
| 2722 | |
| 2723 | num_widgets /= 2; |
| 2724 | if (!num_widgets) { |
| 2725 | dev_err(card->dev, "ASoC: Property '%s's length is zero\n", |
| 2726 | propname); |
| 2727 | return -EINVAL; |
| 2728 | } |
| 2729 | |
| 2730 | widgets = devm_kcalloc(card->dev, num_widgets, sizeof(*widgets), |
| 2731 | GFP_KERNEL); |
| 2732 | if (!widgets) { |
| 2733 | dev_err(card->dev, |
| 2734 | "ASoC: Could not allocate memory for widgets\n"); |
| 2735 | return -ENOMEM; |
| 2736 | } |
| 2737 | |
| 2738 | for (i = 0; i < num_widgets; i++) { |
| 2739 | ret = of_property_read_string_index(np, propname, |
| 2740 | 2 * i, &template); |
| 2741 | if (ret) { |
| 2742 | dev_err(card->dev, |
| 2743 | "ASoC: Property '%s' index %d read error:%d\n", |
| 2744 | propname, 2 * i, ret); |
| 2745 | return -EINVAL; |
| 2746 | } |
| 2747 | |
| 2748 | for (j = 0; j < ARRAY_SIZE(simple_widgets); j++) { |
| 2749 | if (!strncmp(template, simple_widgets[j].name, |
| 2750 | strlen(simple_widgets[j].name))) { |
| 2751 | widgets[i] = simple_widgets[j]; |
| 2752 | break; |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | if (j >= ARRAY_SIZE(simple_widgets)) { |
| 2757 | dev_err(card->dev, |
| 2758 | "ASoC: DAPM widget '%s' is not supported\n", |
| 2759 | template); |
| 2760 | return -EINVAL; |
| 2761 | } |
| 2762 | |
| 2763 | ret = of_property_read_string_index(np, propname, |
| 2764 | (2 * i) + 1, |
| 2765 | &wname); |
| 2766 | if (ret) { |
| 2767 | dev_err(card->dev, |
| 2768 | "ASoC: Property '%s' index %d read error:%d\n", |
| 2769 | propname, (2 * i) + 1, ret); |
| 2770 | return -EINVAL; |
| 2771 | } |
| 2772 | |
| 2773 | widgets[i].name = wname; |
| 2774 | } |
| 2775 | |
Nicolin Chen | f23e860 | 2015-02-14 17:22:49 -0800 | [diff] [blame] | 2776 | card->of_dapm_widgets = widgets; |
| 2777 | card->num_of_dapm_widgets = num_widgets; |
Xiubo Li | 9a6d486 | 2014-02-08 15:59:52 +0800 | [diff] [blame] | 2778 | |
| 2779 | return 0; |
| 2780 | } |
Kuninori Morimoto | 21efde5 | 2017-01-27 06:37:34 +0000 | [diff] [blame] | 2781 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_simple_widgets); |
Xiubo Li | 9a6d486 | 2014-02-08 15:59:52 +0800 | [diff] [blame] | 2782 | |
Jerome Brunet | cbdfab3 | 2018-07-17 17:43:02 +0200 | [diff] [blame] | 2783 | int snd_soc_of_get_slot_mask(struct device_node *np, |
| 2784 | const char *prop_name, |
| 2785 | unsigned int *mask) |
Jyri Sarha | 6131084 | 2015-09-09 21:27:43 +0300 | [diff] [blame] | 2786 | { |
| 2787 | u32 val; |
Jyri Sarha | 6c84e591 | 2015-09-17 13:13:38 +0300 | [diff] [blame] | 2788 | const __be32 *of_slot_mask = of_get_property(np, prop_name, &val); |
Jyri Sarha | 6131084 | 2015-09-09 21:27:43 +0300 | [diff] [blame] | 2789 | int i; |
| 2790 | |
| 2791 | if (!of_slot_mask) |
| 2792 | return 0; |
| 2793 | val /= sizeof(u32); |
| 2794 | for (i = 0; i < val; i++) |
| 2795 | if (be32_to_cpup(&of_slot_mask[i])) |
| 2796 | *mask |= (1 << i); |
| 2797 | |
| 2798 | return val; |
| 2799 | } |
Jerome Brunet | cbdfab3 | 2018-07-17 17:43:02 +0200 | [diff] [blame] | 2800 | EXPORT_SYMBOL_GPL(snd_soc_of_get_slot_mask); |
Jyri Sarha | 6131084 | 2015-09-09 21:27:43 +0300 | [diff] [blame] | 2801 | |
Xiubo Li | 89c6785 | 2014-02-14 09:34:35 +0800 | [diff] [blame] | 2802 | int snd_soc_of_parse_tdm_slot(struct device_node *np, |
Jyri Sarha | 6131084 | 2015-09-09 21:27:43 +0300 | [diff] [blame] | 2803 | unsigned int *tx_mask, |
| 2804 | unsigned int *rx_mask, |
Xiubo Li | 89c6785 | 2014-02-14 09:34:35 +0800 | [diff] [blame] | 2805 | unsigned int *slots, |
| 2806 | unsigned int *slot_width) |
| 2807 | { |
| 2808 | u32 val; |
| 2809 | int ret; |
| 2810 | |
Jyri Sarha | 6131084 | 2015-09-09 21:27:43 +0300 | [diff] [blame] | 2811 | if (tx_mask) |
| 2812 | snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask", tx_mask); |
| 2813 | if (rx_mask) |
| 2814 | snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask", rx_mask); |
| 2815 | |
Xiubo Li | 89c6785 | 2014-02-14 09:34:35 +0800 | [diff] [blame] | 2816 | if (of_property_read_bool(np, "dai-tdm-slot-num")) { |
| 2817 | ret = of_property_read_u32(np, "dai-tdm-slot-num", &val); |
| 2818 | if (ret) |
| 2819 | return ret; |
| 2820 | |
| 2821 | if (slots) |
| 2822 | *slots = val; |
| 2823 | } |
| 2824 | |
| 2825 | if (of_property_read_bool(np, "dai-tdm-slot-width")) { |
| 2826 | ret = of_property_read_u32(np, "dai-tdm-slot-width", &val); |
| 2827 | if (ret) |
| 2828 | return ret; |
| 2829 | |
| 2830 | if (slot_width) |
| 2831 | *slot_width = val; |
| 2832 | } |
| 2833 | |
| 2834 | return 0; |
| 2835 | } |
| 2836 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_tdm_slot); |
| 2837 | |
Kuninori Morimoto | 3b71035 | 2018-11-22 00:55:09 +0000 | [diff] [blame] | 2838 | void snd_soc_of_parse_node_prefix(struct device_node *np, |
| 2839 | struct snd_soc_codec_conf *codec_conf, |
| 2840 | struct device_node *of_node, |
| 2841 | const char *propname) |
Kuninori Morimoto | 5e3cdaa | 2015-07-15 07:07:42 +0000 | [diff] [blame] | 2842 | { |
Kuninori Morimoto | 5e3cdaa | 2015-07-15 07:07:42 +0000 | [diff] [blame] | 2843 | const char *str; |
| 2844 | int ret; |
| 2845 | |
| 2846 | ret = of_property_read_string(np, propname, &str); |
| 2847 | if (ret < 0) { |
| 2848 | /* no prefix is not error */ |
| 2849 | return; |
| 2850 | } |
| 2851 | |
Kuninori Morimoto | c13493a | 2019-12-13 09:54:36 +0900 | [diff] [blame] | 2852 | codec_conf->dlc.of_node = of_node; |
Kuninori Morimoto | 5e3cdaa | 2015-07-15 07:07:42 +0000 | [diff] [blame] | 2853 | codec_conf->name_prefix = str; |
| 2854 | } |
Kuninori Morimoto | 3b71035 | 2018-11-22 00:55:09 +0000 | [diff] [blame] | 2855 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_node_prefix); |
Kuninori Morimoto | 5e3cdaa | 2015-07-15 07:07:42 +0000 | [diff] [blame] | 2856 | |
Kuninori Morimoto | 2bc644a | 2017-01-27 06:36:50 +0000 | [diff] [blame] | 2857 | int snd_soc_of_parse_audio_routing(struct snd_soc_card *card, |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2858 | const char *propname) |
| 2859 | { |
Kuninori Morimoto | 2bc644a | 2017-01-27 06:36:50 +0000 | [diff] [blame] | 2860 | struct device_node *np = card->dev->of_node; |
Mark Brown | e3b1e6a | 2014-12-18 11:46:38 +0000 | [diff] [blame] | 2861 | int num_routes; |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2862 | struct snd_soc_dapm_route *routes; |
| 2863 | int i, ret; |
| 2864 | |
| 2865 | num_routes = of_property_count_strings(np, propname); |
Richard Zhao | c34ce32 | 2012-04-24 15:24:43 +0800 | [diff] [blame] | 2866 | if (num_routes < 0 || num_routes & 1) { |
Michał Mirosław | 10e8aa9 | 2013-05-04 22:21:38 +0200 | [diff] [blame] | 2867 | dev_err(card->dev, |
| 2868 | "ASoC: Property '%s' does not exist or its length is not even\n", |
| 2869 | propname); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2870 | return -EINVAL; |
| 2871 | } |
| 2872 | num_routes /= 2; |
| 2873 | if (!num_routes) { |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 2874 | dev_err(card->dev, "ASoC: Property '%s's length is zero\n", |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2875 | propname); |
| 2876 | return -EINVAL; |
| 2877 | } |
| 2878 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 2879 | routes = devm_kcalloc(card->dev, num_routes, sizeof(*routes), |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2880 | GFP_KERNEL); |
| 2881 | if (!routes) { |
| 2882 | dev_err(card->dev, |
Liam Girdwood | f110bfc | 2012-11-19 14:47:09 +0000 | [diff] [blame] | 2883 | "ASoC: Could not allocate DAPM route table\n"); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2884 | return -EINVAL; |
| 2885 | } |
| 2886 | |
| 2887 | for (i = 0; i < num_routes; i++) { |
| 2888 | ret = of_property_read_string_index(np, propname, |
Mark Brown | e3b1e6a | 2014-12-18 11:46:38 +0000 | [diff] [blame] | 2889 | 2 * i, &routes[i].sink); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2890 | if (ret) { |
Mark Brown | c871bd0 | 2012-12-10 16:19:52 +0900 | [diff] [blame] | 2891 | dev_err(card->dev, |
| 2892 | "ASoC: Property '%s' index %d could not be read: %d\n", |
| 2893 | propname, 2 * i, ret); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2894 | return -EINVAL; |
| 2895 | } |
| 2896 | ret = of_property_read_string_index(np, propname, |
Mark Brown | e3b1e6a | 2014-12-18 11:46:38 +0000 | [diff] [blame] | 2897 | (2 * i) + 1, &routes[i].source); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2898 | if (ret) { |
| 2899 | dev_err(card->dev, |
Mark Brown | c871bd0 | 2012-12-10 16:19:52 +0900 | [diff] [blame] | 2900 | "ASoC: Property '%s' index %d could not be read: %d\n", |
| 2901 | propname, (2 * i) + 1, ret); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2902 | return -EINVAL; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Nicolin Chen | f23e860 | 2015-02-14 17:22:49 -0800 | [diff] [blame] | 2906 | card->num_of_dapm_routes = num_routes; |
| 2907 | card->of_dapm_routes = routes; |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2908 | |
| 2909 | return 0; |
| 2910 | } |
Kuninori Morimoto | 2bc644a | 2017-01-27 06:36:50 +0000 | [diff] [blame] | 2911 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_audio_routing); |
Stephen Warren | a4a54dd | 2011-12-12 15:55:35 -0700 | [diff] [blame] | 2912 | |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2913 | unsigned int snd_soc_of_parse_daifmt(struct device_node *np, |
Jyri Sarha | 389cb83 | 2014-03-24 12:15:24 +0200 | [diff] [blame] | 2914 | const char *prefix, |
| 2915 | struct device_node **bitclkmaster, |
| 2916 | struct device_node **framemaster) |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2917 | { |
| 2918 | int ret, i; |
| 2919 | char prop[128]; |
| 2920 | unsigned int format = 0; |
| 2921 | int bit, frame; |
| 2922 | const char *str; |
| 2923 | struct { |
| 2924 | char *name; |
| 2925 | unsigned int val; |
| 2926 | } of_fmt_table[] = { |
| 2927 | { "i2s", SND_SOC_DAIFMT_I2S }, |
| 2928 | { "right_j", SND_SOC_DAIFMT_RIGHT_J }, |
| 2929 | { "left_j", SND_SOC_DAIFMT_LEFT_J }, |
| 2930 | { "dsp_a", SND_SOC_DAIFMT_DSP_A }, |
| 2931 | { "dsp_b", SND_SOC_DAIFMT_DSP_B }, |
| 2932 | { "ac97", SND_SOC_DAIFMT_AC97 }, |
| 2933 | { "pdm", SND_SOC_DAIFMT_PDM}, |
| 2934 | { "msb", SND_SOC_DAIFMT_MSB }, |
| 2935 | { "lsb", SND_SOC_DAIFMT_LSB }, |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2936 | }; |
| 2937 | |
| 2938 | if (!prefix) |
| 2939 | prefix = ""; |
| 2940 | |
| 2941 | /* |
Kuninori Morimoto | 5711c97 | 2017-04-20 01:33:24 +0000 | [diff] [blame] | 2942 | * check "dai-format = xxx" |
| 2943 | * or "[prefix]format = xxx" |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2944 | * SND_SOC_DAIFMT_FORMAT_MASK area |
| 2945 | */ |
Kuninori Morimoto | 5711c97 | 2017-04-20 01:33:24 +0000 | [diff] [blame] | 2946 | ret = of_property_read_string(np, "dai-format", &str); |
| 2947 | if (ret < 0) { |
| 2948 | snprintf(prop, sizeof(prop), "%sformat", prefix); |
| 2949 | ret = of_property_read_string(np, prop, &str); |
| 2950 | } |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2951 | if (ret == 0) { |
| 2952 | for (i = 0; i < ARRAY_SIZE(of_fmt_table); i++) { |
| 2953 | if (strcmp(str, of_fmt_table[i].name) == 0) { |
| 2954 | format |= of_fmt_table[i].val; |
| 2955 | break; |
| 2956 | } |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | /* |
Kuninori Morimoto | 8c2d6a9 | 2013-01-29 21:03:36 -0800 | [diff] [blame] | 2961 | * check "[prefix]continuous-clock" |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2962 | * SND_SOC_DAIFMT_CLOCK_MASK area |
| 2963 | */ |
Kuninori Morimoto | 8c2d6a9 | 2013-01-29 21:03:36 -0800 | [diff] [blame] | 2964 | snprintf(prop, sizeof(prop), "%scontinuous-clock", prefix); |
Julia Lawall | 5193029 | 2016-08-05 10:56:51 +0200 | [diff] [blame] | 2965 | if (of_property_read_bool(np, prop)) |
Kuninori Morimoto | 8c2d6a9 | 2013-01-29 21:03:36 -0800 | [diff] [blame] | 2966 | format |= SND_SOC_DAIFMT_CONT; |
| 2967 | else |
| 2968 | format |= SND_SOC_DAIFMT_GATED; |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 2969 | |
| 2970 | /* |
| 2971 | * check "[prefix]bitclock-inversion" |
| 2972 | * check "[prefix]frame-inversion" |
| 2973 | * SND_SOC_DAIFMT_INV_MASK area |
| 2974 | */ |
| 2975 | snprintf(prop, sizeof(prop), "%sbitclock-inversion", prefix); |
| 2976 | bit = !!of_get_property(np, prop, NULL); |
| 2977 | |
| 2978 | snprintf(prop, sizeof(prop), "%sframe-inversion", prefix); |
| 2979 | frame = !!of_get_property(np, prop, NULL); |
| 2980 | |
| 2981 | switch ((bit << 4) + frame) { |
| 2982 | case 0x11: |
| 2983 | format |= SND_SOC_DAIFMT_IB_IF; |
| 2984 | break; |
| 2985 | case 0x10: |
| 2986 | format |= SND_SOC_DAIFMT_IB_NF; |
| 2987 | break; |
| 2988 | case 0x01: |
| 2989 | format |= SND_SOC_DAIFMT_NB_IF; |
| 2990 | break; |
| 2991 | default: |
| 2992 | /* SND_SOC_DAIFMT_NB_NF is default */ |
| 2993 | break; |
| 2994 | } |
| 2995 | |
| 2996 | /* |
| 2997 | * check "[prefix]bitclock-master" |
| 2998 | * check "[prefix]frame-master" |
| 2999 | * SND_SOC_DAIFMT_MASTER_MASK area |
| 3000 | */ |
| 3001 | snprintf(prop, sizeof(prop), "%sbitclock-master", prefix); |
| 3002 | bit = !!of_get_property(np, prop, NULL); |
Jyri Sarha | 389cb83 | 2014-03-24 12:15:24 +0200 | [diff] [blame] | 3003 | if (bit && bitclkmaster) |
| 3004 | *bitclkmaster = of_parse_phandle(np, prop, 0); |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 3005 | |
| 3006 | snprintf(prop, sizeof(prop), "%sframe-master", prefix); |
| 3007 | frame = !!of_get_property(np, prop, NULL); |
Jyri Sarha | 389cb83 | 2014-03-24 12:15:24 +0200 | [diff] [blame] | 3008 | if (frame && framemaster) |
| 3009 | *framemaster = of_parse_phandle(np, prop, 0); |
Kuninori Morimoto | a7930ed | 2013-01-14 18:36:04 -0800 | [diff] [blame] | 3010 | |
| 3011 | switch ((bit << 4) + frame) { |
| 3012 | case 0x11: |
| 3013 | format |= SND_SOC_DAIFMT_CBM_CFM; |
| 3014 | break; |
| 3015 | case 0x10: |
| 3016 | format |= SND_SOC_DAIFMT_CBM_CFS; |
| 3017 | break; |
| 3018 | case 0x01: |
| 3019 | format |= SND_SOC_DAIFMT_CBS_CFM; |
| 3020 | break; |
| 3021 | default: |
| 3022 | format |= SND_SOC_DAIFMT_CBS_CFS; |
| 3023 | break; |
| 3024 | } |
| 3025 | |
| 3026 | return format; |
| 3027 | } |
| 3028 | EXPORT_SYMBOL_GPL(snd_soc_of_parse_daifmt); |
| 3029 | |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 3030 | int snd_soc_get_dai_id(struct device_node *ep) |
| 3031 | { |
Kuninori Morimoto | 09d4cc0 | 2019-05-13 16:07:20 +0900 | [diff] [blame] | 3032 | struct snd_soc_component *component; |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 3033 | struct snd_soc_dai_link_component dlc; |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 3034 | int ret; |
| 3035 | |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 3036 | dlc.of_node = of_graph_get_port_parent(ep); |
| 3037 | dlc.name = NULL; |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 3038 | /* |
| 3039 | * For example HDMI case, HDMI has video/sound port, |
| 3040 | * but ALSA SoC needs sound port number only. |
| 3041 | * Thus counting HDMI DT port/endpoint doesn't work. |
| 3042 | * Then, it should have .of_xlate_dai_id |
| 3043 | */ |
| 3044 | ret = -ENOTSUPP; |
| 3045 | mutex_lock(&client_mutex); |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 3046 | component = soc_find_component(&dlc); |
Kuninori Morimoto | 2c7b170 | 2019-07-26 13:51:26 +0900 | [diff] [blame] | 3047 | if (component) |
| 3048 | ret = snd_soc_component_of_xlate_dai_id(component, ep); |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 3049 | mutex_unlock(&client_mutex); |
| 3050 | |
Kuninori Morimoto | c1e230f | 2019-06-20 09:49:27 +0900 | [diff] [blame] | 3051 | of_node_put(dlc.of_node); |
Tony Lindgren | c0a480d | 2017-07-28 01:23:15 -0700 | [diff] [blame] | 3052 | |
Kuninori Morimoto | a180e8b | 2017-05-18 01:39:25 +0000 | [diff] [blame] | 3053 | return ret; |
| 3054 | } |
| 3055 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_id); |
| 3056 | |
Kuninori Morimoto | 1ad8ec5 | 2016-11-11 01:19:28 +0000 | [diff] [blame] | 3057 | int snd_soc_get_dai_name(struct of_phandle_args *args, |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3058 | const char **dai_name) |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3059 | { |
| 3060 | struct snd_soc_component *pos; |
Jyri Sarha | 3e0aa8d | 2015-05-26 21:59:05 +0300 | [diff] [blame] | 3061 | struct device_node *component_of_node; |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3062 | int ret = -EPROBE_DEFER; |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3063 | |
| 3064 | mutex_lock(&client_mutex); |
Kuninori Morimoto | 368dee9 | 2018-09-21 05:23:01 +0000 | [diff] [blame] | 3065 | for_each_component(pos) { |
Kuninori Morimoto | c083444 | 2019-05-13 16:06:59 +0900 | [diff] [blame] | 3066 | component_of_node = soc_component_to_node(pos); |
Jyri Sarha | 3e0aa8d | 2015-05-26 21:59:05 +0300 | [diff] [blame] | 3067 | |
| 3068 | if (component_of_node != args->np) |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3069 | continue; |
| 3070 | |
Kuninori Morimoto | a2a3417 | 2019-07-26 13:51:31 +0900 | [diff] [blame] | 3071 | ret = snd_soc_component_of_xlate_dai_name(pos, args, dai_name); |
| 3072 | if (ret == -ENOTSUPP) { |
Kuninori Morimoto | 58bf417 | 2017-12-20 01:48:29 +0000 | [diff] [blame] | 3073 | struct snd_soc_dai *dai; |
Kuninori Morimoto | 6833c45 | 2013-10-16 22:05:26 -0700 | [diff] [blame] | 3074 | int id = -1; |
| 3075 | |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3076 | switch (args->args_count) { |
Kuninori Morimoto | 6833c45 | 2013-10-16 22:05:26 -0700 | [diff] [blame] | 3077 | case 0: |
| 3078 | id = 0; /* same as dai_drv[0] */ |
| 3079 | break; |
| 3080 | case 1: |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3081 | id = args->args[0]; |
Kuninori Morimoto | 6833c45 | 2013-10-16 22:05:26 -0700 | [diff] [blame] | 3082 | break; |
| 3083 | default: |
| 3084 | /* not supported */ |
| 3085 | break; |
| 3086 | } |
| 3087 | |
| 3088 | if (id < 0 || id >= pos->num_dai) { |
| 3089 | ret = -EINVAL; |
Nicolin Chen | 3dcba28 | 2014-04-21 19:14:46 +0800 | [diff] [blame] | 3090 | continue; |
Kuninori Morimoto | 6833c45 | 2013-10-16 22:05:26 -0700 | [diff] [blame] | 3091 | } |
Xiubo Li | e41975e | 2013-12-20 14:39:51 +0800 | [diff] [blame] | 3092 | |
| 3093 | ret = 0; |
| 3094 | |
Kuninori Morimoto | 58bf417 | 2017-12-20 01:48:29 +0000 | [diff] [blame] | 3095 | /* find target DAI */ |
Kuninori Morimoto | 15a0c64 | 2018-09-21 05:23:17 +0000 | [diff] [blame] | 3096 | for_each_component_dais(pos, dai) { |
Kuninori Morimoto | 58bf417 | 2017-12-20 01:48:29 +0000 | [diff] [blame] | 3097 | if (id == 0) |
| 3098 | break; |
| 3099 | id--; |
| 3100 | } |
| 3101 | |
| 3102 | *dai_name = dai->driver->name; |
Xiubo Li | e41975e | 2013-12-20 14:39:51 +0800 | [diff] [blame] | 3103 | if (!*dai_name) |
| 3104 | *dai_name = pos->name; |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3107 | break; |
| 3108 | } |
| 3109 | mutex_unlock(&client_mutex); |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3110 | return ret; |
| 3111 | } |
Kuninori Morimoto | 1ad8ec5 | 2016-11-11 01:19:28 +0000 | [diff] [blame] | 3112 | EXPORT_SYMBOL_GPL(snd_soc_get_dai_name); |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3113 | |
| 3114 | int snd_soc_of_get_dai_name(struct device_node *of_node, |
| 3115 | const char **dai_name) |
| 3116 | { |
| 3117 | struct of_phandle_args args; |
| 3118 | int ret; |
| 3119 | |
| 3120 | ret = of_parse_phandle_with_args(of_node, "sound-dai", |
| 3121 | "#sound-dai-cells", 0, &args); |
| 3122 | if (ret) |
| 3123 | return ret; |
| 3124 | |
| 3125 | ret = snd_soc_get_dai_name(&args, dai_name); |
Kuninori Morimoto | cb47008 | 2013-09-10 17:39:56 -0700 | [diff] [blame] | 3126 | |
| 3127 | of_node_put(args.np); |
| 3128 | |
| 3129 | return ret; |
| 3130 | } |
| 3131 | EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_name); |
| 3132 | |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3133 | /* |
Sylwester Nawrocki | 9468576 | 2018-03-09 18:48:54 +0100 | [diff] [blame] | 3134 | * snd_soc_of_put_dai_link_codecs - Dereference device nodes in the codecs array |
| 3135 | * @dai_link: DAI link |
| 3136 | * |
| 3137 | * Dereference device nodes acquired by snd_soc_of_get_dai_link_codecs(). |
| 3138 | */ |
| 3139 | void snd_soc_of_put_dai_link_codecs(struct snd_soc_dai_link *dai_link) |
| 3140 | { |
Kuninori Morimoto | 3db769f | 2018-09-03 02:12:40 +0000 | [diff] [blame] | 3141 | struct snd_soc_dai_link_component *component; |
Sylwester Nawrocki | 9468576 | 2018-03-09 18:48:54 +0100 | [diff] [blame] | 3142 | int index; |
| 3143 | |
Kuninori Morimoto | 3db769f | 2018-09-03 02:12:40 +0000 | [diff] [blame] | 3144 | for_each_link_codecs(dai_link, index, component) { |
Sylwester Nawrocki | 9468576 | 2018-03-09 18:48:54 +0100 | [diff] [blame] | 3145 | if (!component->of_node) |
| 3146 | break; |
| 3147 | of_node_put(component->of_node); |
| 3148 | component->of_node = NULL; |
| 3149 | } |
| 3150 | } |
| 3151 | EXPORT_SYMBOL_GPL(snd_soc_of_put_dai_link_codecs); |
| 3152 | |
| 3153 | /* |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3154 | * snd_soc_of_get_dai_link_codecs - Parse a list of CODECs in the devicetree |
| 3155 | * @dev: Card device |
| 3156 | * @of_node: Device node |
| 3157 | * @dai_link: DAI link |
| 3158 | * |
| 3159 | * Builds an array of CODEC DAI components from the DAI link property |
| 3160 | * 'sound-dai'. |
| 3161 | * The array is set in the DAI link and the number of DAIs is set accordingly. |
Sylwester Nawrocki | 9468576 | 2018-03-09 18:48:54 +0100 | [diff] [blame] | 3162 | * The device nodes in the array (of_node) must be dereferenced by calling |
| 3163 | * snd_soc_of_put_dai_link_codecs() on @dai_link. |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3164 | * |
| 3165 | * Returns 0 for success |
| 3166 | */ |
| 3167 | int snd_soc_of_get_dai_link_codecs(struct device *dev, |
| 3168 | struct device_node *of_node, |
| 3169 | struct snd_soc_dai_link *dai_link) |
| 3170 | { |
| 3171 | struct of_phandle_args args; |
| 3172 | struct snd_soc_dai_link_component *component; |
| 3173 | char *name; |
| 3174 | int index, num_codecs, ret; |
| 3175 | |
| 3176 | /* Count the number of CODECs */ |
| 3177 | name = "sound-dai"; |
| 3178 | num_codecs = of_count_phandle_with_args(of_node, name, |
| 3179 | "#sound-dai-cells"); |
| 3180 | if (num_codecs <= 0) { |
| 3181 | if (num_codecs == -ENOENT) |
| 3182 | dev_err(dev, "No 'sound-dai' property\n"); |
| 3183 | else |
| 3184 | dev_err(dev, "Bad phandle in 'sound-dai'\n"); |
| 3185 | return num_codecs; |
| 3186 | } |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 3187 | component = devm_kcalloc(dev, |
| 3188 | num_codecs, sizeof(*component), |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3189 | GFP_KERNEL); |
| 3190 | if (!component) |
| 3191 | return -ENOMEM; |
| 3192 | dai_link->codecs = component; |
| 3193 | dai_link->num_codecs = num_codecs; |
| 3194 | |
| 3195 | /* Parse the list */ |
Kuninori Morimoto | 3db769f | 2018-09-03 02:12:40 +0000 | [diff] [blame] | 3196 | for_each_link_codecs(dai_link, index, component) { |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3197 | ret = of_parse_phandle_with_args(of_node, name, |
| 3198 | "#sound-dai-cells", |
Marcel Ziswiler | 2c7b696 | 2018-10-18 13:18:28 +0200 | [diff] [blame] | 3199 | index, &args); |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3200 | if (ret) |
| 3201 | goto err; |
| 3202 | component->of_node = args.np; |
| 3203 | ret = snd_soc_get_dai_name(&args, &component->dai_name); |
| 3204 | if (ret < 0) |
| 3205 | goto err; |
| 3206 | } |
| 3207 | return 0; |
| 3208 | err: |
Sylwester Nawrocki | 9468576 | 2018-03-09 18:48:54 +0100 | [diff] [blame] | 3209 | snd_soc_of_put_dai_link_codecs(dai_link); |
Jean-Francois Moine | 93b0f3ee | 2014-11-25 13:16:12 +0100 | [diff] [blame] | 3210 | dai_link->codecs = NULL; |
| 3211 | dai_link->num_codecs = 0; |
| 3212 | return ret; |
| 3213 | } |
| 3214 | EXPORT_SYMBOL_GPL(snd_soc_of_get_dai_link_codecs); |
| 3215 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 3216 | static int __init snd_soc_init(void) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3217 | { |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 3218 | snd_soc_debugfs_init(); |
Mark Brown | fb25789 | 2011-04-28 10:57:54 +0100 | [diff] [blame] | 3219 | snd_soc_util_init(); |
| 3220 | |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3221 | return platform_driver_register(&soc_driver); |
| 3222 | } |
Mark Brown | 4abe8e1 | 2010-10-12 17:41:03 +0100 | [diff] [blame] | 3223 | module_init(snd_soc_init); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3224 | |
Mark Brown | 7d8c16a | 2008-11-30 22:11:24 +0000 | [diff] [blame] | 3225 | static void __exit snd_soc_exit(void) |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3226 | { |
Mark Brown | fb25789 | 2011-04-28 10:57:54 +0100 | [diff] [blame] | 3227 | snd_soc_util_exit(); |
Lars-Peter Clausen | 6553bf06 | 2015-04-09 10:52:38 +0200 | [diff] [blame] | 3228 | snd_soc_debugfs_exit(); |
Mark Brown | fb25789 | 2011-04-28 10:57:54 +0100 | [diff] [blame] | 3229 | |
Mark Brown | 3ff3f64 | 2008-05-19 12:32:25 +0200 | [diff] [blame] | 3230 | platform_driver_unregister(&soc_driver); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3231 | } |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3232 | module_exit(snd_soc_exit); |
| 3233 | |
| 3234 | /* Module information */ |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 3235 | MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk"); |
Frank Mandarino | db2a416 | 2006-10-06 18:31:09 +0200 | [diff] [blame] | 3236 | MODULE_DESCRIPTION("ALSA SoC Core"); |
| 3237 | MODULE_LICENSE("GPL"); |
Kay Sievers | 8b45a20 | 2008-04-14 13:33:36 +0200 | [diff] [blame] | 3238 | MODULE_ALIAS("platform:soc-audio"); |