Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 1 | /* |
Kuninori Morimoto | 29a43aa | 2016-12-02 05:27:30 +0000 | [diff] [blame] | 2 | * simple-card-utils.c |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2016 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 10 | #include <linux/clk.h> |
Kuninori Morimoto | 1f85e11 | 2016-08-03 01:24:05 +0000 | [diff] [blame] | 11 | #include <linux/module.h> |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 12 | #include <linux/of.h> |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame^] | 13 | #include <linux/of_graph.h> |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 14 | #include <sound/simple_card_utils.h> |
| 15 | |
| 16 | int asoc_simple_card_parse_daifmt(struct device *dev, |
| 17 | struct device_node *node, |
| 18 | struct device_node *codec, |
| 19 | char *prefix, |
| 20 | unsigned int *retfmt) |
| 21 | { |
| 22 | struct device_node *bitclkmaster = NULL; |
| 23 | struct device_node *framemaster = NULL; |
| 24 | int prefix_len = prefix ? strlen(prefix) : 0; |
| 25 | unsigned int daifmt; |
| 26 | |
| 27 | daifmt = snd_soc_of_parse_daifmt(node, prefix, |
| 28 | &bitclkmaster, &framemaster); |
| 29 | daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; |
| 30 | |
| 31 | if (prefix_len && !bitclkmaster && !framemaster) { |
| 32 | /* |
| 33 | * No dai-link level and master setting was not found from |
| 34 | * sound node level, revert back to legacy DT parsing and |
| 35 | * take the settings from codec node. |
| 36 | */ |
| 37 | dev_dbg(dev, "Revert to legacy daifmt parsing\n"); |
| 38 | |
| 39 | daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) | |
| 40 | (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK); |
| 41 | } else { |
| 42 | if (codec == bitclkmaster) |
| 43 | daifmt |= (codec == framemaster) ? |
| 44 | SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS; |
| 45 | else |
| 46 | daifmt |= (codec == framemaster) ? |
| 47 | SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS; |
| 48 | } |
| 49 | |
| 50 | of_node_put(bitclkmaster); |
| 51 | of_node_put(framemaster); |
| 52 | |
| 53 | *retfmt = daifmt; |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt); |
Kuninori Morimoto | 1db3312 | 2016-07-11 23:57:14 +0000 | [diff] [blame] | 58 | |
| 59 | int asoc_simple_card_set_dailink_name(struct device *dev, |
| 60 | struct snd_soc_dai_link *dai_link, |
| 61 | const char *fmt, ...) |
| 62 | { |
| 63 | va_list ap; |
| 64 | char *name = NULL; |
| 65 | int ret = -ENOMEM; |
| 66 | |
| 67 | va_start(ap, fmt); |
| 68 | name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap); |
| 69 | va_end(ap); |
| 70 | |
| 71 | if (name) { |
| 72 | ret = 0; |
| 73 | |
| 74 | dai_link->name = name; |
| 75 | dai_link->stream_name = name; |
| 76 | } |
| 77 | |
| 78 | return ret; |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name); |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 81 | |
| 82 | int asoc_simple_card_parse_card_name(struct snd_soc_card *card, |
| 83 | char *prefix) |
| 84 | { |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 85 | int ret; |
| 86 | |
Kuninori Morimoto | dedfaa1 | 2017-04-20 01:34:49 +0000 | [diff] [blame] | 87 | if (!prefix) |
| 88 | prefix = ""; |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 89 | |
| 90 | /* Parse the card name from DT */ |
Kuninori Morimoto | dedfaa1 | 2017-04-20 01:34:49 +0000 | [diff] [blame] | 91 | ret = snd_soc_of_parse_card_name(card, "label"); |
| 92 | if (ret < 0) { |
| 93 | char prop[128]; |
| 94 | |
| 95 | snprintf(prop, sizeof(prop), "%sname", prefix); |
| 96 | ret = snd_soc_of_parse_card_name(card, prop); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | } |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 100 | |
| 101 | if (!card->name && card->dai_link) |
| 102 | card->name = card->dai_link->name; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name); |
Kuninori Morimoto | 1f85e11 | 2016-08-03 01:24:05 +0000 | [diff] [blame] | 107 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 108 | int asoc_simple_card_parse_clk(struct device *dev, |
| 109 | struct device_node *node, |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 110 | struct device_node *dai_of_node, |
| 111 | struct asoc_simple_dai *simple_dai) |
| 112 | { |
| 113 | struct clk *clk; |
| 114 | u32 val; |
| 115 | |
| 116 | /* |
| 117 | * Parse dai->sysclk come from "clocks = <&xxx>" |
| 118 | * (if system has common clock) |
| 119 | * or "system-clock-frequency = <xxx>" |
| 120 | * or device's module clock. |
| 121 | */ |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 122 | clk = devm_get_clk_from_child(dev, node, NULL); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 123 | if (!IS_ERR(clk)) { |
| 124 | simple_dai->sysclk = clk_get_rate(clk); |
Lucas Stach | 971edb0 | 2017-03-23 15:05:26 +0100 | [diff] [blame] | 125 | simple_dai->clk = clk; |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 126 | } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) { |
| 127 | simple_dai->sysclk = val; |
| 128 | } else { |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 129 | clk = devm_get_clk_from_child(dev, dai_of_node, NULL); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 130 | if (!IS_ERR(clk)) |
| 131 | simple_dai->sysclk = clk_get_rate(clk); |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk); |
| 137 | |
Kuninori Morimoto | ae30a69 | 2016-08-08 06:01:43 +0000 | [diff] [blame] | 138 | int asoc_simple_card_parse_dai(struct device_node *node, |
| 139 | struct device_node **dai_of_node, |
| 140 | const char **dai_name, |
| 141 | const char *list_name, |
| 142 | const char *cells_name, |
| 143 | int *is_single_link) |
| 144 | { |
| 145 | struct of_phandle_args args; |
| 146 | int ret; |
| 147 | |
| 148 | if (!node) |
| 149 | return 0; |
| 150 | |
| 151 | /* |
| 152 | * Get node via "sound-dai = <&phandle port>" |
| 153 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 154 | */ |
| 155 | ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | /* Get dai->name */ |
| 160 | if (dai_name) { |
| 161 | ret = snd_soc_of_get_dai_name(node, dai_name); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | *dai_of_node = args.np; |
| 167 | |
| 168 | if (is_single_link) |
| 169 | *is_single_link = !args.args_count; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai); |
| 174 | |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame^] | 175 | static int asoc_simple_card_get_dai_id(struct device_node *ep) |
| 176 | { |
| 177 | struct device_node *node; |
| 178 | struct device_node *endpoint; |
| 179 | int i, id; |
| 180 | |
| 181 | node = of_graph_get_port_parent(ep); |
| 182 | |
| 183 | i = 0; |
| 184 | id = -1; |
| 185 | for_each_endpoint_of_node(node, endpoint) { |
| 186 | if (endpoint == ep) |
| 187 | id = i; |
| 188 | i++; |
| 189 | } |
| 190 | if (id < 0) |
| 191 | return -ENODEV; |
| 192 | |
| 193 | return id; |
| 194 | } |
| 195 | |
| 196 | int asoc_simple_card_parse_graph_dai(struct device_node *ep, |
| 197 | struct device_node **dai_of_node, |
| 198 | const char **dai_name) |
| 199 | { |
| 200 | struct device_node *node; |
| 201 | struct of_phandle_args args; |
| 202 | int ret; |
| 203 | |
| 204 | if (!ep) |
| 205 | return 0; |
| 206 | if (!dai_name) |
| 207 | return 0; |
| 208 | |
| 209 | /* |
| 210 | * of_graph_get_port_parent() will call |
| 211 | * of_node_put(). So, call of_node_get() here |
| 212 | */ |
| 213 | of_node_get(ep); |
| 214 | node = of_graph_get_port_parent(ep); |
| 215 | |
| 216 | /* Get dai->name */ |
| 217 | args.np = node; |
| 218 | args.args[0] = asoc_simple_card_get_dai_id(ep); |
| 219 | args.args_count = (of_graph_get_endpoint_count(node) > 1); |
| 220 | |
| 221 | ret = snd_soc_get_dai_name(&args, dai_name); |
| 222 | if (ret < 0) |
| 223 | return ret; |
| 224 | |
| 225 | *dai_of_node = node; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai); |
| 230 | |
Kuninori Morimoto | 21ba62f | 2016-08-09 05:48:30 +0000 | [diff] [blame] | 231 | int asoc_simple_card_init_dai(struct snd_soc_dai *dai, |
| 232 | struct asoc_simple_dai *simple_dai) |
| 233 | { |
| 234 | int ret; |
| 235 | |
| 236 | if (simple_dai->sysclk) { |
| 237 | ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0); |
| 238 | if (ret && ret != -ENOTSUPP) { |
| 239 | dev_err(dai->dev, "simple-card: set_sysclk error\n"); |
| 240 | return ret; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (simple_dai->slots) { |
| 245 | ret = snd_soc_dai_set_tdm_slot(dai, |
| 246 | simple_dai->tx_slot_mask, |
| 247 | simple_dai->rx_slot_mask, |
| 248 | simple_dai->slots, |
| 249 | simple_dai->slot_width); |
| 250 | if (ret && ret != -ENOTSUPP) { |
| 251 | dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); |
| 252 | return ret; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai); |
| 259 | |
Kuninori Morimoto | c262c9a | 2016-08-09 05:49:41 +0000 | [diff] [blame] | 260 | int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link) |
| 261 | { |
Kuninori Morimoto | c262c9a | 2016-08-09 05:49:41 +0000 | [diff] [blame] | 262 | /* Assumes platform == cpu */ |
| 263 | if (!dai_link->platform_of_node) |
| 264 | dai_link->platform_of_node = dai_link->cpu_of_node; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink); |
| 269 | |
Kuninori Morimoto | 983cebd | 2016-08-10 02:20:19 +0000 | [diff] [blame] | 270 | void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link, |
| 271 | int is_single_links) |
| 272 | { |
| 273 | /* |
| 274 | * In soc_bind_dai_link() will check cpu name after |
| 275 | * of_node matching if dai_link has cpu_dai_name. |
| 276 | * but, it will never match if name was created by |
| 277 | * fmt_single_name() remove cpu_dai_name if cpu_args |
| 278 | * was 0. See: |
| 279 | * fmt_single_name() |
| 280 | * fmt_multiple_name() |
| 281 | */ |
| 282 | if (is_single_links) |
| 283 | dai_link->cpu_dai_name = NULL; |
| 284 | } |
| 285 | EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu); |
| 286 | |
Kuninori Morimoto | 0f4e071 | 2016-08-10 02:21:25 +0000 | [diff] [blame] | 287 | int asoc_simple_card_clean_reference(struct snd_soc_card *card) |
| 288 | { |
| 289 | struct snd_soc_dai_link *dai_link; |
| 290 | int num_links; |
| 291 | |
| 292 | for (num_links = 0, dai_link = card->dai_link; |
| 293 | num_links < card->num_links; |
| 294 | num_links++, dai_link++) { |
| 295 | of_node_put(dai_link->cpu_of_node); |
| 296 | of_node_put(dai_link->codec_of_node); |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference); |
| 301 | |
Kuninori Morimoto | 1f85e11 | 2016-08-03 01:24:05 +0000 | [diff] [blame] | 302 | /* Module information */ |
| 303 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |
| 304 | MODULE_DESCRIPTION("ALSA SoC Simple Card Utils"); |
| 305 | MODULE_LICENSE("GPL v2"); |