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 | |
Kuninori Morimoto | 13bb1cc | 2017-06-15 00:24:09 +0000 | [diff] [blame] | 16 | void asoc_simple_card_convert_fixup(struct asoc_simple_card_data *data, |
| 17 | struct snd_pcm_hw_params *params) |
| 18 | { |
| 19 | struct snd_interval *rate = hw_param_interval(params, |
| 20 | SNDRV_PCM_HW_PARAM_RATE); |
| 21 | struct snd_interval *channels = hw_param_interval(params, |
| 22 | SNDRV_PCM_HW_PARAM_CHANNELS); |
| 23 | |
| 24 | if (data->convert_rate) |
| 25 | rate->min = |
| 26 | rate->max = data->convert_rate; |
| 27 | |
| 28 | if (data->convert_channels) |
| 29 | channels->min = |
| 30 | channels->max = data->convert_channels; |
| 31 | } |
| 32 | EXPORT_SYMBOL_GPL(asoc_simple_card_convert_fixup); |
| 33 | |
| 34 | void asoc_simple_card_parse_convert(struct device *dev, char *prefix, |
| 35 | struct asoc_simple_card_data *data) |
| 36 | { |
| 37 | struct device_node *np = dev->of_node; |
| 38 | char prop[128]; |
| 39 | |
| 40 | if (!prefix) |
| 41 | prefix = ""; |
| 42 | |
| 43 | /* sampling rate convert */ |
| 44 | snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-rate"); |
| 45 | of_property_read_u32(np, prop, &data->convert_rate); |
| 46 | |
| 47 | /* channels transfer */ |
| 48 | snprintf(prop, sizeof(prop), "%s%s", prefix, "convert-channels"); |
| 49 | of_property_read_u32(np, prop, &data->convert_channels); |
| 50 | |
| 51 | dev_dbg(dev, "convert_rate %d\n", data->convert_rate); |
| 52 | dev_dbg(dev, "convert_channels %d\n", data->convert_channels); |
| 53 | } |
| 54 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_convert); |
| 55 | |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 56 | int asoc_simple_card_parse_daifmt(struct device *dev, |
| 57 | struct device_node *node, |
| 58 | struct device_node *codec, |
| 59 | char *prefix, |
| 60 | unsigned int *retfmt) |
| 61 | { |
| 62 | struct device_node *bitclkmaster = NULL; |
| 63 | struct device_node *framemaster = NULL; |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 64 | unsigned int daifmt; |
| 65 | |
| 66 | daifmt = snd_soc_of_parse_daifmt(node, prefix, |
| 67 | &bitclkmaster, &framemaster); |
| 68 | daifmt &= ~SND_SOC_DAIFMT_MASTER_MASK; |
| 69 | |
Kuninori Morimoto | 155b8f3 | 2017-05-25 01:51:31 +0000 | [diff] [blame] | 70 | if (!bitclkmaster && !framemaster) { |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 71 | /* |
| 72 | * No dai-link level and master setting was not found from |
| 73 | * sound node level, revert back to legacy DT parsing and |
| 74 | * take the settings from codec node. |
| 75 | */ |
| 76 | dev_dbg(dev, "Revert to legacy daifmt parsing\n"); |
| 77 | |
| 78 | daifmt = snd_soc_of_parse_daifmt(codec, NULL, NULL, NULL) | |
| 79 | (daifmt & ~SND_SOC_DAIFMT_CLOCK_MASK); |
| 80 | } else { |
| 81 | if (codec == bitclkmaster) |
| 82 | daifmt |= (codec == framemaster) ? |
| 83 | SND_SOC_DAIFMT_CBM_CFM : SND_SOC_DAIFMT_CBM_CFS; |
| 84 | else |
| 85 | daifmt |= (codec == framemaster) ? |
| 86 | SND_SOC_DAIFMT_CBS_CFM : SND_SOC_DAIFMT_CBS_CFS; |
| 87 | } |
| 88 | |
| 89 | of_node_put(bitclkmaster); |
| 90 | of_node_put(framemaster); |
| 91 | |
| 92 | *retfmt = daifmt; |
| 93 | |
Kuninori Morimoto | aaad9c1 | 2017-06-05 04:28:12 +0000 | [diff] [blame] | 94 | dev_dbg(dev, "format : %04x\n", daifmt); |
| 95 | |
Kuninori Morimoto | abd3147 | 2016-05-31 09:00:14 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_daifmt); |
Kuninori Morimoto | 1db3312 | 2016-07-11 23:57:14 +0000 | [diff] [blame] | 99 | |
| 100 | int asoc_simple_card_set_dailink_name(struct device *dev, |
| 101 | struct snd_soc_dai_link *dai_link, |
| 102 | const char *fmt, ...) |
| 103 | { |
| 104 | va_list ap; |
| 105 | char *name = NULL; |
| 106 | int ret = -ENOMEM; |
| 107 | |
| 108 | va_start(ap, fmt); |
| 109 | name = devm_kvasprintf(dev, GFP_KERNEL, fmt, ap); |
| 110 | va_end(ap); |
| 111 | |
| 112 | if (name) { |
| 113 | ret = 0; |
| 114 | |
| 115 | dai_link->name = name; |
| 116 | dai_link->stream_name = name; |
Kuninori Morimoto | 4579771 | 2017-06-05 04:28:29 +0000 | [diff] [blame] | 117 | |
| 118 | dev_dbg(dev, "name : %s\n", name); |
Kuninori Morimoto | 1db3312 | 2016-07-11 23:57:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(asoc_simple_card_set_dailink_name); |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 124 | |
| 125 | int asoc_simple_card_parse_card_name(struct snd_soc_card *card, |
| 126 | char *prefix) |
| 127 | { |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 128 | int ret; |
| 129 | |
Kuninori Morimoto | dedfaa1 | 2017-04-20 01:34:49 +0000 | [diff] [blame] | 130 | if (!prefix) |
| 131 | prefix = ""; |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 132 | |
| 133 | /* Parse the card name from DT */ |
Kuninori Morimoto | dedfaa1 | 2017-04-20 01:34:49 +0000 | [diff] [blame] | 134 | ret = snd_soc_of_parse_card_name(card, "label"); |
| 135 | if (ret < 0) { |
| 136 | char prop[128]; |
| 137 | |
| 138 | snprintf(prop, sizeof(prop), "%sname", prefix); |
| 139 | ret = snd_soc_of_parse_card_name(card, prop); |
| 140 | if (ret < 0) |
| 141 | return ret; |
| 142 | } |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 143 | |
| 144 | if (!card->name && card->dai_link) |
| 145 | card->name = card->dai_link->name; |
| 146 | |
Kuninori Morimoto | d4dbcb6 | 2017-06-05 04:27:56 +0000 | [diff] [blame] | 147 | dev_dbg(card->dev, "Card Name: %s\n", card->name ? card->name : ""); |
| 148 | |
Kuninori Morimoto | fc55c9b | 2016-07-11 23:59:16 +0000 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_card_name); |
Kuninori Morimoto | 1f85e11 | 2016-08-03 01:24:05 +0000 | [diff] [blame] | 152 | |
Kuninori Morimoto | 891caea | 2017-06-09 00:43:18 +0000 | [diff] [blame] | 153 | static void asoc_simple_card_clk_register(struct asoc_simple_dai *dai, |
| 154 | struct clk *clk) |
| 155 | { |
| 156 | dai->clk = clk; |
| 157 | } |
| 158 | |
| 159 | int asoc_simple_card_clk_enable(struct asoc_simple_dai *dai) |
| 160 | { |
| 161 | return clk_prepare_enable(dai->clk); |
| 162 | } |
Kuninori Morimoto | 63a5f59 | 2017-06-14 01:04:11 +0000 | [diff] [blame] | 163 | EXPORT_SYMBOL_GPL(asoc_simple_card_clk_enable); |
Kuninori Morimoto | 891caea | 2017-06-09 00:43:18 +0000 | [diff] [blame] | 164 | |
| 165 | void asoc_simple_card_clk_disable(struct asoc_simple_dai *dai) |
| 166 | { |
| 167 | clk_disable_unprepare(dai->clk); |
| 168 | } |
Kuninori Morimoto | 63a5f59 | 2017-06-14 01:04:11 +0000 | [diff] [blame] | 169 | EXPORT_SYMBOL_GPL(asoc_simple_card_clk_disable); |
Kuninori Morimoto | 891caea | 2017-06-09 00:43:18 +0000 | [diff] [blame] | 170 | |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 171 | int asoc_simple_card_parse_clk(struct device *dev, |
| 172 | struct device_node *node, |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 173 | struct device_node *dai_of_node, |
Kuninori Morimoto | 8e16638 | 2017-06-05 04:28:45 +0000 | [diff] [blame] | 174 | struct asoc_simple_dai *simple_dai, |
| 175 | const char *name) |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 176 | { |
| 177 | struct clk *clk; |
| 178 | u32 val; |
| 179 | |
| 180 | /* |
| 181 | * Parse dai->sysclk come from "clocks = <&xxx>" |
| 182 | * (if system has common clock) |
| 183 | * or "system-clock-frequency = <xxx>" |
| 184 | * or device's module clock. |
| 185 | */ |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 186 | clk = devm_get_clk_from_child(dev, node, NULL); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 187 | if (!IS_ERR(clk)) { |
| 188 | simple_dai->sysclk = clk_get_rate(clk); |
Kuninori Morimoto | 891caea | 2017-06-09 00:43:18 +0000 | [diff] [blame] | 189 | |
| 190 | asoc_simple_card_clk_register(simple_dai, clk); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 191 | } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) { |
| 192 | simple_dai->sysclk = val; |
| 193 | } else { |
Kuninori Morimoto | e984fd6 | 2017-01-23 07:29:42 +0000 | [diff] [blame] | 194 | clk = devm_get_clk_from_child(dev, dai_of_node, NULL); |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 195 | if (!IS_ERR(clk)) |
| 196 | simple_dai->sysclk = clk_get_rate(clk); |
| 197 | } |
| 198 | |
Kuninori Morimoto | 8e16638 | 2017-06-05 04:28:45 +0000 | [diff] [blame] | 199 | dev_dbg(dev, "%s : sysclk = %d\n", name, simple_dai->sysclk); |
| 200 | |
Kuninori Morimoto | bb6fc62 | 2016-08-08 05:59:56 +0000 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_clk); |
| 204 | |
Kuninori Morimoto | ae30a69 | 2016-08-08 06:01:43 +0000 | [diff] [blame] | 205 | int asoc_simple_card_parse_dai(struct device_node *node, |
| 206 | struct device_node **dai_of_node, |
| 207 | const char **dai_name, |
| 208 | const char *list_name, |
| 209 | const char *cells_name, |
| 210 | int *is_single_link) |
| 211 | { |
| 212 | struct of_phandle_args args; |
| 213 | int ret; |
| 214 | |
| 215 | if (!node) |
| 216 | return 0; |
| 217 | |
| 218 | /* |
| 219 | * Get node via "sound-dai = <&phandle port>" |
| 220 | * it will be used as xxx_of_node on soc_bind_dai_link() |
| 221 | */ |
| 222 | ret = of_parse_phandle_with_args(node, list_name, cells_name, 0, &args); |
| 223 | if (ret) |
| 224 | return ret; |
| 225 | |
| 226 | /* Get dai->name */ |
| 227 | if (dai_name) { |
| 228 | ret = snd_soc_of_get_dai_name(node, dai_name); |
| 229 | if (ret < 0) |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | *dai_of_node = args.np; |
| 234 | |
| 235 | if (is_single_link) |
| 236 | *is_single_link = !args.args_count; |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_dai); |
| 241 | |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame] | 242 | static int asoc_simple_card_get_dai_id(struct device_node *ep) |
| 243 | { |
| 244 | struct device_node *node; |
| 245 | struct device_node *endpoint; |
| 246 | int i, id; |
Kuninori Morimoto | 73b17f1 | 2017-05-18 01:39:44 +0000 | [diff] [blame] | 247 | int ret; |
| 248 | |
| 249 | ret = snd_soc_get_dai_id(ep); |
| 250 | if (ret != -ENOTSUPP) |
| 251 | return ret; |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame] | 252 | |
| 253 | node = of_graph_get_port_parent(ep); |
| 254 | |
Kuninori Morimoto | 73b17f1 | 2017-05-18 01:39:44 +0000 | [diff] [blame] | 255 | /* |
| 256 | * Non HDMI sound case, counting port/endpoint on its DT |
| 257 | * is enough. Let's count it. |
| 258 | */ |
Kuninori Morimoto | 1689333 | 2017-04-20 01:35:18 +0000 | [diff] [blame] | 259 | i = 0; |
| 260 | id = -1; |
| 261 | for_each_endpoint_of_node(node, endpoint) { |
| 262 | if (endpoint == ep) |
| 263 | id = i; |
| 264 | i++; |
| 265 | } |
| 266 | if (id < 0) |
| 267 | return -ENODEV; |
| 268 | |
| 269 | return id; |
| 270 | } |
| 271 | |
| 272 | int asoc_simple_card_parse_graph_dai(struct device_node *ep, |
| 273 | struct device_node **dai_of_node, |
| 274 | const char **dai_name) |
| 275 | { |
| 276 | struct device_node *node; |
| 277 | struct of_phandle_args args; |
| 278 | int ret; |
| 279 | |
| 280 | if (!ep) |
| 281 | return 0; |
| 282 | if (!dai_name) |
| 283 | return 0; |
| 284 | |
| 285 | /* |
| 286 | * of_graph_get_port_parent() will call |
| 287 | * of_node_put(). So, call of_node_get() here |
| 288 | */ |
| 289 | of_node_get(ep); |
| 290 | node = of_graph_get_port_parent(ep); |
| 291 | |
| 292 | /* Get dai->name */ |
| 293 | args.np = node; |
| 294 | args.args[0] = asoc_simple_card_get_dai_id(ep); |
| 295 | args.args_count = (of_graph_get_endpoint_count(node) > 1); |
| 296 | |
| 297 | ret = snd_soc_get_dai_name(&args, dai_name); |
| 298 | if (ret < 0) |
| 299 | return ret; |
| 300 | |
| 301 | *dai_of_node = node; |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(asoc_simple_card_parse_graph_dai); |
| 306 | |
Kuninori Morimoto | 21ba62f | 2016-08-09 05:48:30 +0000 | [diff] [blame] | 307 | int asoc_simple_card_init_dai(struct snd_soc_dai *dai, |
| 308 | struct asoc_simple_dai *simple_dai) |
| 309 | { |
| 310 | int ret; |
| 311 | |
| 312 | if (simple_dai->sysclk) { |
| 313 | ret = snd_soc_dai_set_sysclk(dai, 0, simple_dai->sysclk, 0); |
| 314 | if (ret && ret != -ENOTSUPP) { |
| 315 | dev_err(dai->dev, "simple-card: set_sysclk error\n"); |
| 316 | return ret; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | if (simple_dai->slots) { |
| 321 | ret = snd_soc_dai_set_tdm_slot(dai, |
| 322 | simple_dai->tx_slot_mask, |
| 323 | simple_dai->rx_slot_mask, |
| 324 | simple_dai->slots, |
| 325 | simple_dai->slot_width); |
| 326 | if (ret && ret != -ENOTSUPP) { |
| 327 | dev_err(dai->dev, "simple-card: set_tdm_slot error\n"); |
| 328 | return ret; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(asoc_simple_card_init_dai); |
| 335 | |
Kuninori Morimoto | c262c9a | 2016-08-09 05:49:41 +0000 | [diff] [blame] | 336 | int asoc_simple_card_canonicalize_dailink(struct snd_soc_dai_link *dai_link) |
| 337 | { |
Kuninori Morimoto | c262c9a | 2016-08-09 05:49:41 +0000 | [diff] [blame] | 338 | /* Assumes platform == cpu */ |
| 339 | if (!dai_link->platform_of_node) |
| 340 | dai_link->platform_of_node = dai_link->cpu_of_node; |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_dailink); |
| 345 | |
Kuninori Morimoto | 983cebd | 2016-08-10 02:20:19 +0000 | [diff] [blame] | 346 | void asoc_simple_card_canonicalize_cpu(struct snd_soc_dai_link *dai_link, |
| 347 | int is_single_links) |
| 348 | { |
| 349 | /* |
| 350 | * In soc_bind_dai_link() will check cpu name after |
| 351 | * of_node matching if dai_link has cpu_dai_name. |
| 352 | * but, it will never match if name was created by |
| 353 | * fmt_single_name() remove cpu_dai_name if cpu_args |
| 354 | * was 0. See: |
| 355 | * fmt_single_name() |
| 356 | * fmt_multiple_name() |
| 357 | */ |
| 358 | if (is_single_links) |
| 359 | dai_link->cpu_dai_name = NULL; |
| 360 | } |
| 361 | EXPORT_SYMBOL_GPL(asoc_simple_card_canonicalize_cpu); |
| 362 | |
Kuninori Morimoto | 0f4e071 | 2016-08-10 02:21:25 +0000 | [diff] [blame] | 363 | int asoc_simple_card_clean_reference(struct snd_soc_card *card) |
| 364 | { |
| 365 | struct snd_soc_dai_link *dai_link; |
| 366 | int num_links; |
| 367 | |
| 368 | for (num_links = 0, dai_link = card->dai_link; |
| 369 | num_links < card->num_links; |
| 370 | num_links++, dai_link++) { |
| 371 | of_node_put(dai_link->cpu_of_node); |
| 372 | of_node_put(dai_link->codec_of_node); |
| 373 | } |
| 374 | return 0; |
| 375 | } |
| 376 | EXPORT_SYMBOL_GPL(asoc_simple_card_clean_reference); |
| 377 | |
Kuninori Morimoto | 3296d07 | 2017-06-15 00:25:02 +0000 | [diff] [blame] | 378 | int asoc_simple_card_of_parse_routing(struct snd_soc_card *card, |
| 379 | char *prefix, |
| 380 | int optional) |
| 381 | { |
| 382 | struct device_node *node = card->dev->of_node; |
| 383 | char prop[128]; |
| 384 | |
| 385 | if (!prefix) |
| 386 | prefix = ""; |
| 387 | |
| 388 | snprintf(prop, sizeof(prop), "%s%s", prefix, "routing"); |
| 389 | |
| 390 | if (!of_property_read_bool(node, prop)) { |
| 391 | if (optional) |
| 392 | return 0; |
| 393 | return -EINVAL; |
| 394 | } |
| 395 | |
| 396 | return snd_soc_of_parse_audio_routing(card, prop); |
| 397 | } |
| 398 | EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_routing); |
| 399 | |
Kuninori Morimoto | b31f11d | 2017-06-16 01:38:50 +0000 | [diff] [blame] | 400 | int asoc_simple_card_of_parse_widgets(struct snd_soc_card *card, |
| 401 | char *prefix) |
| 402 | { |
| 403 | struct device_node *node = card->dev->of_node; |
| 404 | char prop[128]; |
| 405 | |
| 406 | if (!prefix) |
| 407 | prefix = ""; |
| 408 | |
| 409 | snprintf(prop, sizeof(prop), "%s%s", prefix, "widgets"); |
| 410 | |
| 411 | if (of_property_read_bool(node, prop)) |
| 412 | return snd_soc_of_parse_audio_simple_widgets(card, prop); |
| 413 | |
| 414 | /* no widgets is not error */ |
| 415 | return 0; |
| 416 | } |
| 417 | EXPORT_SYMBOL_GPL(asoc_simple_card_of_parse_widgets); |
| 418 | |
Kuninori Morimoto | 1f85e11 | 2016-08-03 01:24:05 +0000 | [diff] [blame] | 419 | /* Module information */ |
| 420 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |
| 421 | MODULE_DESCRIPTION("ALSA SoC Simple Card Utils"); |
| 422 | MODULE_LICENSE("GPL v2"); |