Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ASoC audio graph sound card support |
| 3 | * |
| 4 | * Copyright (C) 2016 Renesas Solutions Corp. |
| 5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> |
| 6 | * |
| 7 | * based on ${LINUX}/sound/soc/generic/simple-card.c |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_gpio.h> |
| 20 | #include <linux/of_graph.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <sound/jack.h> |
| 24 | #include <sound/simple_card_utils.h> |
| 25 | |
| 26 | struct graph_card_data { |
| 27 | struct snd_soc_card snd_card; |
| 28 | struct graph_dai_props { |
| 29 | struct asoc_simple_dai cpu_dai; |
| 30 | struct asoc_simple_dai codec_dai; |
| 31 | } *dai_props; |
| 32 | struct snd_soc_dai_link *dai_link; |
| 33 | }; |
| 34 | |
| 35 | #define graph_priv_to_card(priv) (&(priv)->snd_card) |
| 36 | #define graph_priv_to_props(priv, i) ((priv)->dai_props + (i)) |
| 37 | #define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev) |
| 38 | #define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i)) |
| 39 | |
| 40 | static int asoc_graph_card_startup(struct snd_pcm_substream *substream) |
| 41 | { |
| 42 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 43 | struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 44 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 45 | int ret; |
| 46 | |
Kuninori Morimoto | d471d55 | 2017-06-09 00:45:23 +0000 | [diff] [blame] | 47 | ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 48 | if (ret) |
| 49 | return ret; |
| 50 | |
Kuninori Morimoto | d471d55 | 2017-06-09 00:45:23 +0000 | [diff] [blame] | 51 | ret = asoc_simple_card_clk_enable(&dai_props->codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 52 | if (ret) |
Kuninori Morimoto | d471d55 | 2017-06-09 00:45:23 +0000 | [diff] [blame] | 53 | asoc_simple_card_clk_disable(&dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 54 | |
| 55 | return ret; |
| 56 | } |
| 57 | |
| 58 | static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream) |
| 59 | { |
| 60 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 61 | struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 62 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num); |
| 63 | |
Kuninori Morimoto | d471d55 | 2017-06-09 00:45:23 +0000 | [diff] [blame] | 64 | asoc_simple_card_clk_disable(&dai_props->cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 65 | |
Kuninori Morimoto | d471d55 | 2017-06-09 00:45:23 +0000 | [diff] [blame] | 66 | asoc_simple_card_clk_disable(&dai_props->codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static struct snd_soc_ops asoc_graph_card_ops = { |
| 70 | .startup = asoc_graph_card_startup, |
| 71 | .shutdown = asoc_graph_card_shutdown, |
| 72 | }; |
| 73 | |
| 74 | static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd) |
| 75 | { |
| 76 | struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card); |
| 77 | struct snd_soc_dai *codec = rtd->codec_dai; |
| 78 | struct snd_soc_dai *cpu = rtd->cpu_dai; |
| 79 | struct graph_dai_props *dai_props = |
| 80 | graph_priv_to_props(priv, rtd->num); |
| 81 | int ret; |
| 82 | |
| 83 | ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai); |
| 84 | if (ret < 0) |
| 85 | return ret; |
| 86 | |
| 87 | ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai); |
| 88 | if (ret < 0) |
| 89 | return ret; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int asoc_graph_card_dai_link_of(struct device_node *cpu_port, |
| 95 | struct graph_card_data *priv, |
| 96 | int idx) |
| 97 | { |
| 98 | struct device *dev = graph_priv_to_dev(priv); |
| 99 | struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx); |
| 100 | struct graph_dai_props *dai_props = graph_priv_to_props(priv, idx); |
| 101 | struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai; |
| 102 | struct asoc_simple_dai *codec_dai = &dai_props->codec_dai; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 103 | struct device_node *cpu_ep = of_get_next_child(cpu_port, NULL); |
| 104 | struct device_node *codec_ep = of_graph_get_remote_endpoint(cpu_ep); |
| 105 | struct device_node *rcpu_ep = of_graph_get_remote_endpoint(codec_ep); |
| 106 | int ret; |
| 107 | |
| 108 | if (rcpu_ep != cpu_ep) { |
Colin Ian King | a619f04 | 2017-05-18 08:48:15 +0100 | [diff] [blame] | 109 | dev_err(dev, "remote-endpoint mismatch (%s/%s/%s)\n", |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 110 | cpu_ep->name, codec_ep->name, rcpu_ep->name); |
| 111 | ret = -EINVAL; |
| 112 | goto dai_link_of_err; |
| 113 | } |
| 114 | |
| 115 | ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep, |
| 116 | NULL, &dai_link->dai_fmt); |
| 117 | if (ret < 0) |
| 118 | goto dai_link_of_err; |
| 119 | |
| 120 | /* |
| 121 | * we need to consider "mclk-fs" around here |
| 122 | * see simple-card |
| 123 | */ |
| 124 | |
| 125 | ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link); |
| 126 | if (ret < 0) |
| 127 | goto dai_link_of_err; |
| 128 | |
| 129 | ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link); |
| 130 | if (ret < 0) |
| 131 | goto dai_link_of_err; |
| 132 | |
Kuninori Morimoto | c98907d | 2017-06-14 00:35:30 +0000 | [diff] [blame] | 133 | ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 134 | if (ret < 0) |
| 135 | goto dai_link_of_err; |
| 136 | |
Kuninori Morimoto | c98907d | 2017-06-14 00:35:30 +0000 | [diff] [blame] | 137 | ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 138 | if (ret < 0) |
| 139 | goto dai_link_of_err; |
| 140 | |
| 141 | ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai); |
| 142 | if (ret < 0) |
| 143 | goto dai_link_of_err; |
| 144 | |
| 145 | ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai); |
| 146 | if (ret < 0) |
| 147 | goto dai_link_of_err; |
| 148 | |
| 149 | ret = asoc_simple_card_canonicalize_dailink(dai_link); |
| 150 | if (ret < 0) |
| 151 | goto dai_link_of_err; |
| 152 | |
| 153 | ret = asoc_simple_card_set_dailink_name(dev, dai_link, |
| 154 | "%s-%s", |
| 155 | dai_link->cpu_dai_name, |
| 156 | dai_link->codec_dai_name); |
| 157 | if (ret < 0) |
| 158 | goto dai_link_of_err; |
| 159 | |
| 160 | dai_link->ops = &asoc_graph_card_ops; |
| 161 | dai_link->init = asoc_graph_card_dai_init; |
| 162 | |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 163 | asoc_simple_card_canonicalize_cpu(dai_link, |
Kuninori Morimoto | 47ca959 | 2017-06-22 06:21:49 +0000 | [diff] [blame^] | 164 | of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1); |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 165 | |
| 166 | dai_link_of_err: |
| 167 | of_node_put(cpu_ep); |
| 168 | of_node_put(rcpu_ep); |
| 169 | of_node_put(codec_ep); |
| 170 | |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | static int asoc_graph_card_parse_of(struct graph_card_data *priv) |
| 175 | { |
| 176 | struct of_phandle_iterator it; |
| 177 | struct device *dev = graph_priv_to_dev(priv); |
| 178 | struct snd_soc_card *card = graph_priv_to_card(priv); |
| 179 | struct device_node *node = dev->of_node; |
| 180 | int rc, idx = 0; |
| 181 | int ret; |
| 182 | |
| 183 | /* |
| 184 | * we need to consider "widgets", "routing", "mclk-fs" around here |
| 185 | * see simple-card |
| 186 | */ |
| 187 | |
| 188 | of_for_each_phandle(&it, rc, node, "dais", NULL, 0) { |
| 189 | ret = asoc_graph_card_dai_link_of(it.node, priv, idx++); |
| 190 | of_node_put(it.node); |
| 191 | if (ret < 0) |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | return asoc_simple_card_parse_card_name(card, NULL); |
| 196 | } |
| 197 | |
| 198 | static int asoc_graph_get_dais_count(struct device *dev) |
| 199 | { |
| 200 | struct of_phandle_iterator it; |
| 201 | struct device_node *node = dev->of_node; |
| 202 | int count = 0; |
| 203 | int rc; |
| 204 | |
| 205 | of_for_each_phandle(&it, rc, node, "dais", NULL, 0) { |
| 206 | count++; |
| 207 | of_node_put(it.node); |
| 208 | } |
| 209 | |
| 210 | return count; |
| 211 | } |
| 212 | |
| 213 | static int asoc_graph_card_probe(struct platform_device *pdev) |
| 214 | { |
| 215 | struct graph_card_data *priv; |
| 216 | struct snd_soc_dai_link *dai_link; |
| 217 | struct graph_dai_props *dai_props; |
| 218 | struct device *dev = &pdev->dev; |
| 219 | struct snd_soc_card *card; |
| 220 | int num, ret; |
| 221 | |
| 222 | /* Allocate the private data and the DAI link array */ |
| 223 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 224 | if (!priv) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | num = asoc_graph_get_dais_count(dev); |
| 228 | if (num == 0) |
| 229 | return -EINVAL; |
| 230 | |
| 231 | dai_props = devm_kzalloc(dev, sizeof(*dai_props) * num, GFP_KERNEL); |
| 232 | dai_link = devm_kzalloc(dev, sizeof(*dai_link) * num, GFP_KERNEL); |
| 233 | if (!dai_props || !dai_link) |
| 234 | return -ENOMEM; |
| 235 | |
| 236 | priv->dai_props = dai_props; |
| 237 | priv->dai_link = dai_link; |
| 238 | |
| 239 | /* Init snd_soc_card */ |
| 240 | card = graph_priv_to_card(priv); |
| 241 | card->owner = THIS_MODULE; |
| 242 | card->dev = dev; |
| 243 | card->dai_link = dai_link; |
| 244 | card->num_links = num; |
| 245 | |
| 246 | ret = asoc_graph_card_parse_of(priv); |
| 247 | if (ret < 0) { |
| 248 | if (ret != -EPROBE_DEFER) |
| 249 | dev_err(dev, "parse error %d\n", ret); |
| 250 | goto err; |
| 251 | } |
| 252 | |
| 253 | snd_soc_card_set_drvdata(card, priv); |
| 254 | |
| 255 | ret = devm_snd_soc_register_card(dev, card); |
Kuninori Morimoto | ecea931 | 2017-05-19 00:58:00 +0000 | [diff] [blame] | 256 | if (ret < 0) |
| 257 | goto err; |
| 258 | |
| 259 | return 0; |
Kuninori Morimoto | 2692c1c | 2017-04-20 01:36:08 +0000 | [diff] [blame] | 260 | err: |
| 261 | asoc_simple_card_clean_reference(card); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int asoc_graph_card_remove(struct platform_device *pdev) |
| 267 | { |
| 268 | struct snd_soc_card *card = platform_get_drvdata(pdev); |
| 269 | |
| 270 | return asoc_simple_card_clean_reference(card); |
| 271 | } |
| 272 | |
| 273 | static const struct of_device_id asoc_graph_of_match[] = { |
| 274 | { .compatible = "audio-graph-card", }, |
| 275 | {}, |
| 276 | }; |
| 277 | MODULE_DEVICE_TABLE(of, asoc_graph_of_match); |
| 278 | |
| 279 | static struct platform_driver asoc_graph_card = { |
| 280 | .driver = { |
| 281 | .name = "asoc-audio-graph-card", |
| 282 | .of_match_table = asoc_graph_of_match, |
| 283 | }, |
| 284 | .probe = asoc_graph_card_probe, |
| 285 | .remove = asoc_graph_card_remove, |
| 286 | }; |
| 287 | module_platform_driver(asoc_graph_card); |
| 288 | |
| 289 | MODULE_ALIAS("platform:asoc-audio-graph-card"); |
| 290 | MODULE_LICENSE("GPL v2"); |
| 291 | MODULE_DESCRIPTION("ASoC Audio Graph Sound Card"); |
| 292 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |