1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (c) 2020 BayLibre, SAS. 4 // Author: Jerome Brunet <jbrunet@baylibre.com> 5 6 #include <linux/module.h> 7 #include <linux/of_platform.h> 8 #include <sound/soc.h> 9 10 #include "meson-card.h" 11 12 int meson_card_i2s_set_sysclk(struct snd_pcm_substream *substream, 13 struct snd_pcm_hw_params *params, 14 unsigned int mclk_fs) 15 { 16 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 17 struct snd_soc_dai *codec_dai; 18 unsigned int mclk; 19 int ret, i; 20 21 if (!mclk_fs) 22 return 0; 23 24 mclk = params_rate(params) * mclk_fs; 25 26 for_each_rtd_codec_dais(rtd, i, codec_dai) { 27 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, 28 SND_SOC_CLOCK_IN); 29 if (ret && ret != -ENOTSUPP) 30 return ret; 31 } 32 33 ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), 0, mclk, 34 SND_SOC_CLOCK_OUT); 35 if (ret && ret != -ENOTSUPP) 36 return ret; 37 38 return 0; 39 } 40 EXPORT_SYMBOL_GPL(meson_card_i2s_set_sysclk); 41 42 int meson_card_reallocate_links(struct snd_soc_card *card, 43 unsigned int num_links) 44 { 45 struct meson_card *priv = snd_soc_card_get_drvdata(card); 46 struct snd_soc_dai_link *links; 47 void **ldata; 48 49 links = krealloc(priv->card.dai_link, 50 num_links * sizeof(*priv->card.dai_link), 51 GFP_KERNEL | __GFP_ZERO); 52 if (!links) 53 goto err_links; 54 55 ldata = krealloc(priv->link_data, 56 num_links * sizeof(*priv->link_data), 57 GFP_KERNEL | __GFP_ZERO); 58 if (!ldata) 59 goto err_ldata; 60 61 priv->card.dai_link = links; 62 priv->link_data = ldata; 63 priv->card.num_links = num_links; 64 return 0; 65 66 err_ldata: 67 kfree(links); 68 err_links: 69 dev_err(priv->card.dev, "failed to allocate links\n"); 70 return -ENOMEM; 71 72 } 73 EXPORT_SYMBOL_GPL(meson_card_reallocate_links); 74 75 int meson_card_parse_dai(struct snd_soc_card *card, 76 struct device_node *node, 77 struct snd_soc_dai_link_component *dlc) 78 { 79 int ret; 80 81 if (!dlc || !node) 82 return -EINVAL; 83 84 ret = snd_soc_of_get_dlc(node, NULL, dlc, 0); 85 if (ret) 86 return dev_err_probe(card->dev, ret, "can't parse dai\n"); 87 88 return ret; 89 } 90 EXPORT_SYMBOL_GPL(meson_card_parse_dai); 91 92 static int meson_card_set_link_name(struct snd_soc_card *card, 93 struct snd_soc_dai_link *link, 94 struct device_node *node, 95 const char *prefix) 96 { 97 char *name = devm_kasprintf(card->dev, GFP_KERNEL, "%s.%s", 98 prefix, node->full_name); 99 if (!name) 100 return -ENOMEM; 101 102 link->name = name; 103 link->stream_name = name; 104 105 return 0; 106 } 107 108 unsigned int meson_card_parse_daifmt(struct device_node *node, 109 struct device_node *cpu_node) 110 { 111 struct device_node *bitclkmaster = NULL; 112 struct device_node *framemaster = NULL; 113 unsigned int daifmt; 114 115 daifmt = snd_soc_daifmt_parse_format(node, NULL); 116 117 snd_soc_daifmt_parse_clock_provider_as_phandle(node, NULL, &bitclkmaster, &framemaster); 118 119 /* If no master is provided, default to cpu master */ 120 if (!bitclkmaster || bitclkmaster == cpu_node) { 121 daifmt |= (!framemaster || framemaster == cpu_node) ? 122 SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBS_CFM; 123 } else { 124 daifmt |= (!framemaster || framemaster == cpu_node) ? 125 SND_SOC_DAIFMT_CBM_CFS : SND_SOC_DAIFMT_CBM_CFM; 126 } 127 128 of_node_put(bitclkmaster); 129 of_node_put(framemaster); 130 131 return daifmt; 132 } 133 EXPORT_SYMBOL_GPL(meson_card_parse_daifmt); 134 135 int meson_card_set_be_link(struct snd_soc_card *card, 136 struct snd_soc_dai_link *link, 137 struct device_node *node) 138 { 139 struct snd_soc_dai_link_component *codec; 140 struct device_node *np; 141 int ret, num_codecs; 142 143 num_codecs = of_get_child_count(node); 144 if (!num_codecs) { 145 dev_err(card->dev, "be link %s has no codec\n", 146 node->full_name); 147 return -EINVAL; 148 } 149 150 codec = devm_kcalloc(card->dev, num_codecs, sizeof(*codec), GFP_KERNEL); 151 if (!codec) 152 return -ENOMEM; 153 154 link->codecs = codec; 155 link->num_codecs = num_codecs; 156 157 for_each_child_of_node(node, np) { 158 ret = meson_card_parse_dai(card, np, codec); 159 if (ret) { 160 of_node_put(np); 161 return ret; 162 } 163 164 codec++; 165 } 166 167 ret = meson_card_set_link_name(card, link, node, "be"); 168 if (ret) 169 dev_err(card->dev, "error setting %pOFn link name\n", np); 170 171 return ret; 172 } 173 EXPORT_SYMBOL_GPL(meson_card_set_be_link); 174 175 int meson_card_set_fe_link(struct snd_soc_card *card, 176 struct snd_soc_dai_link *link, 177 struct device_node *node, 178 bool is_playback) 179 { 180 link->codecs = &asoc_dummy_dlc; 181 link->num_codecs = 1; 182 183 link->dynamic = 1; 184 link->dpcm_merged_format = 1; 185 link->dpcm_merged_chan = 1; 186 link->dpcm_merged_rate = 1; 187 188 if (is_playback) 189 link->dpcm_playback = 1; 190 else 191 link->dpcm_capture = 1; 192 193 return meson_card_set_link_name(card, link, node, "fe"); 194 } 195 EXPORT_SYMBOL_GPL(meson_card_set_fe_link); 196 197 static int meson_card_add_links(struct snd_soc_card *card) 198 { 199 struct meson_card *priv = snd_soc_card_get_drvdata(card); 200 struct device_node *node = card->dev->of_node; 201 struct device_node *np; 202 int num, i, ret; 203 204 num = of_get_child_count(node); 205 if (!num) { 206 dev_err(card->dev, "card has no links\n"); 207 return -EINVAL; 208 } 209 210 ret = meson_card_reallocate_links(card, num); 211 if (ret) 212 return ret; 213 214 i = 0; 215 for_each_child_of_node(node, np) { 216 ret = priv->match_data->add_link(card, np, &i); 217 if (ret) { 218 of_node_put(np); 219 return ret; 220 } 221 222 i++; 223 } 224 225 return 0; 226 } 227 228 static int meson_card_parse_of_optional(struct snd_soc_card *card, 229 const char *propname, 230 int (*func)(struct snd_soc_card *c, 231 const char *p)) 232 { 233 /* If property is not provided, don't fail ... */ 234 if (!of_property_read_bool(card->dev->of_node, propname)) 235 return 0; 236 237 /* ... but do fail if it is provided and the parsing fails */ 238 return func(card, propname); 239 } 240 241 static void meson_card_clean_references(struct meson_card *priv) 242 { 243 struct snd_soc_card *card = &priv->card; 244 struct snd_soc_dai_link *link; 245 struct snd_soc_dai_link_component *codec; 246 struct snd_soc_aux_dev *aux; 247 int i, j; 248 249 if (card->dai_link) { 250 for_each_card_prelinks(card, i, link) { 251 if (link->cpus) 252 of_node_put(link->cpus->of_node); 253 for_each_link_codecs(link, j, codec) 254 of_node_put(codec->of_node); 255 } 256 } 257 258 if (card->aux_dev) { 259 for_each_card_pre_auxs(card, i, aux) 260 of_node_put(aux->dlc.of_node); 261 } 262 263 kfree(card->dai_link); 264 kfree(priv->link_data); 265 } 266 267 int meson_card_probe(struct platform_device *pdev) 268 { 269 const struct meson_card_match_data *data; 270 struct device *dev = &pdev->dev; 271 struct meson_card *priv; 272 int ret; 273 274 data = of_device_get_match_data(dev); 275 if (!data) { 276 dev_err(dev, "failed to match device\n"); 277 return -ENODEV; 278 } 279 280 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 281 if (!priv) 282 return -ENOMEM; 283 284 platform_set_drvdata(pdev, priv); 285 snd_soc_card_set_drvdata(&priv->card, priv); 286 287 priv->card.owner = THIS_MODULE; 288 priv->card.dev = dev; 289 priv->card.driver_name = dev->driver->name; 290 priv->match_data = data; 291 292 ret = snd_soc_of_parse_card_name(&priv->card, "model"); 293 if (ret < 0) 294 return ret; 295 296 ret = meson_card_parse_of_optional(&priv->card, "audio-routing", 297 snd_soc_of_parse_audio_routing); 298 if (ret) { 299 dev_err(dev, "error while parsing routing\n"); 300 return ret; 301 } 302 303 ret = meson_card_parse_of_optional(&priv->card, "audio-widgets", 304 snd_soc_of_parse_audio_simple_widgets); 305 if (ret) { 306 dev_err(dev, "error while parsing widgets\n"); 307 return ret; 308 } 309 310 ret = meson_card_add_links(&priv->card); 311 if (ret) 312 goto out_err; 313 314 ret = snd_soc_of_parse_aux_devs(&priv->card, "audio-aux-devs"); 315 if (ret) 316 goto out_err; 317 318 ret = devm_snd_soc_register_card(dev, &priv->card); 319 if (ret) 320 goto out_err; 321 322 return 0; 323 324 out_err: 325 meson_card_clean_references(priv); 326 return ret; 327 } 328 EXPORT_SYMBOL_GPL(meson_card_probe); 329 330 int meson_card_remove(struct platform_device *pdev) 331 { 332 struct meson_card *priv = platform_get_drvdata(pdev); 333 334 meson_card_clean_references(priv); 335 336 return 0; 337 } 338 EXPORT_SYMBOL_GPL(meson_card_remove); 339 340 MODULE_DESCRIPTION("Amlogic Sound Card Utils"); 341 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>"); 342 MODULE_LICENSE("GPL v2"); 343