linux/sound/soc/generic/audio-graph-card.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// ASoC audio graph sound card support
   4//
   5// Copyright (C) 2016 Renesas Solutions Corp.
   6// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
   7//
   8// based on ${LINUX}/sound/soc/generic/simple-card.c
   9
  10#include <linux/clk.h>
  11#include <linux/device.h>
  12#include <linux/gpio.h>
  13#include <linux/gpio/consumer.h>
  14#include <linux/module.h>
  15#include <linux/of.h>
  16#include <linux/of_device.h>
  17#include <linux/of_gpio.h>
  18#include <linux/of_graph.h>
  19#include <linux/platform_device.h>
  20#include <linux/string.h>
  21#include <sound/simple_card_utils.h>
  22
  23struct graph_card_data {
  24        struct snd_soc_card snd_card;
  25        struct graph_dai_props {
  26                struct asoc_simple_dai cpu_dai;
  27                struct asoc_simple_dai codec_dai;
  28                unsigned int mclk_fs;
  29        } *dai_props;
  30        unsigned int mclk_fs;
  31        struct asoc_simple_jack hp_jack;
  32        struct asoc_simple_jack mic_jack;
  33        struct snd_soc_dai_link *dai_link;
  34        struct gpio_desc *pa_gpio;
  35};
  36
  37static int asoc_graph_card_outdrv_event(struct snd_soc_dapm_widget *w,
  38                                        struct snd_kcontrol *kcontrol,
  39                                        int event)
  40{
  41        struct snd_soc_dapm_context *dapm = w->dapm;
  42        struct graph_card_data *priv = snd_soc_card_get_drvdata(dapm->card);
  43
  44        switch (event) {
  45        case SND_SOC_DAPM_POST_PMU:
  46                gpiod_set_value_cansleep(priv->pa_gpio, 1);
  47                break;
  48        case SND_SOC_DAPM_PRE_PMD:
  49                gpiod_set_value_cansleep(priv->pa_gpio, 0);
  50                break;
  51        default:
  52                return -EINVAL;
  53        }
  54
  55        return 0;
  56}
  57
  58static const struct snd_soc_dapm_widget asoc_graph_card_dapm_widgets[] = {
  59        SND_SOC_DAPM_OUT_DRV_E("Amplifier", SND_SOC_NOPM,
  60                               0, 0, NULL, 0, asoc_graph_card_outdrv_event,
  61                               SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  62};
  63
  64#define graph_priv_to_card(priv) (&(priv)->snd_card)
  65#define graph_priv_to_props(priv, i) ((priv)->dai_props + (i))
  66#define graph_priv_to_dev(priv) (graph_priv_to_card(priv)->dev)
  67#define graph_priv_to_link(priv, i) (graph_priv_to_card(priv)->dai_link + (i))
  68
  69static int asoc_graph_card_startup(struct snd_pcm_substream *substream)
  70{
  71        struct snd_soc_pcm_runtime *rtd = substream->private_data;
  72        struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  73        struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
  74        int ret;
  75
  76        ret = asoc_simple_card_clk_enable(&dai_props->cpu_dai);
  77        if (ret)
  78                return ret;
  79
  80        ret = asoc_simple_card_clk_enable(&dai_props->codec_dai);
  81        if (ret)
  82                asoc_simple_card_clk_disable(&dai_props->cpu_dai);
  83
  84        return ret;
  85}
  86
  87static void asoc_graph_card_shutdown(struct snd_pcm_substream *substream)
  88{
  89        struct snd_soc_pcm_runtime *rtd = substream->private_data;
  90        struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
  91        struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
  92
  93        asoc_simple_card_clk_disable(&dai_props->cpu_dai);
  94
  95        asoc_simple_card_clk_disable(&dai_props->codec_dai);
  96}
  97
  98static int asoc_graph_card_hw_params(struct snd_pcm_substream *substream,
  99                                     struct snd_pcm_hw_params *params)
 100{
 101        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 102        struct snd_soc_dai *codec_dai = rtd->codec_dai;
 103        struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
 104        struct graph_card_data *priv = snd_soc_card_get_drvdata(rtd->card);
 105        struct graph_dai_props *dai_props = graph_priv_to_props(priv, rtd->num);
 106        unsigned int mclk, mclk_fs = 0;
 107        int ret = 0;
 108
 109        if (priv->mclk_fs)
 110                mclk_fs = priv->mclk_fs;
 111        else if (dai_props->mclk_fs)
 112                mclk_fs = dai_props->mclk_fs;
 113
 114        if (mclk_fs) {
 115                mclk = params_rate(params) * mclk_fs;
 116                ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
 117                                             SND_SOC_CLOCK_IN);
 118                if (ret && ret != -ENOTSUPP)
 119                        goto err;
 120
 121                ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
 122                                             SND_SOC_CLOCK_OUT);
 123                if (ret && ret != -ENOTSUPP)
 124                        goto err;
 125        }
 126        return 0;
 127err:
 128        return ret;
 129}
 130
 131static const struct snd_soc_ops asoc_graph_card_ops = {
 132        .startup = asoc_graph_card_startup,
 133        .shutdown = asoc_graph_card_shutdown,
 134        .hw_params = asoc_graph_card_hw_params,
 135};
 136
 137static int asoc_graph_card_dai_init(struct snd_soc_pcm_runtime *rtd)
 138{
 139        struct graph_card_data *priv =  snd_soc_card_get_drvdata(rtd->card);
 140        struct snd_soc_dai *codec = rtd->codec_dai;
 141        struct snd_soc_dai *cpu = rtd->cpu_dai;
 142        struct graph_dai_props *dai_props =
 143                graph_priv_to_props(priv, rtd->num);
 144        int ret;
 145
 146        ret = asoc_simple_card_init_dai(codec, &dai_props->codec_dai);
 147        if (ret < 0)
 148                return ret;
 149
 150        ret = asoc_simple_card_init_dai(cpu, &dai_props->cpu_dai);
 151        if (ret < 0)
 152                return ret;
 153
 154        return 0;
 155}
 156
 157static int asoc_graph_card_dai_link_of(struct device_node *cpu_port,
 158                                        struct graph_card_data *priv,
 159                                        int idx)
 160{
 161        struct device *dev = graph_priv_to_dev(priv);
 162        struct snd_soc_dai_link *dai_link = graph_priv_to_link(priv, idx);
 163        struct graph_dai_props *dai_props = graph_priv_to_props(priv, idx);
 164        struct asoc_simple_dai *cpu_dai = &dai_props->cpu_dai;
 165        struct asoc_simple_dai *codec_dai = &dai_props->codec_dai;
 166        struct device_node *cpu_ep    = of_get_next_child(cpu_port, NULL);
 167        struct device_node *codec_ep = of_graph_get_remote_endpoint(cpu_ep);
 168        struct device_node *rcpu_ep = of_graph_get_remote_endpoint(codec_ep);
 169        int ret;
 170
 171        if (rcpu_ep != cpu_ep) {
 172                dev_err(dev, "remote-endpoint mismatch (%s/%s/%s)\n",
 173                        cpu_ep->name, codec_ep->name, rcpu_ep->name);
 174                ret = -EINVAL;
 175                goto dai_link_of_err;
 176        }
 177
 178        ret = asoc_simple_card_parse_daifmt(dev, cpu_ep, codec_ep,
 179                                            NULL, &dai_link->dai_fmt);
 180        if (ret < 0)
 181                goto dai_link_of_err;
 182
 183        of_property_read_u32(rcpu_ep, "mclk-fs", &dai_props->mclk_fs);
 184
 185        ret = asoc_simple_card_parse_graph_cpu(cpu_ep, dai_link);
 186        if (ret < 0)
 187                goto dai_link_of_err;
 188
 189        ret = asoc_simple_card_parse_graph_codec(codec_ep, dai_link);
 190        if (ret < 0)
 191                goto dai_link_of_err;
 192
 193        ret = asoc_simple_card_of_parse_tdm(cpu_ep, cpu_dai);
 194        if (ret < 0)
 195                goto dai_link_of_err;
 196
 197        ret = asoc_simple_card_of_parse_tdm(codec_ep, codec_dai);
 198        if (ret < 0)
 199                goto dai_link_of_err;
 200
 201        ret = asoc_simple_card_parse_clk_cpu(dev, cpu_ep, dai_link, cpu_dai);
 202        if (ret < 0)
 203                goto dai_link_of_err;
 204
 205        ret = asoc_simple_card_parse_clk_codec(dev, codec_ep, dai_link, codec_dai);
 206        if (ret < 0)
 207                goto dai_link_of_err;
 208
 209        ret = asoc_simple_card_canonicalize_dailink(dai_link);
 210        if (ret < 0)
 211                goto dai_link_of_err;
 212
 213        ret = asoc_simple_card_set_dailink_name(dev, dai_link,
 214                                                "%s-%s",
 215                                                dai_link->cpu_dai_name,
 216                                                dai_link->codec_dai_name);
 217        if (ret < 0)
 218                goto dai_link_of_err;
 219
 220        dai_link->ops = &asoc_graph_card_ops;
 221        dai_link->init = asoc_graph_card_dai_init;
 222
 223        asoc_simple_card_canonicalize_cpu(dai_link,
 224                of_graph_get_endpoint_count(dai_link->cpu_of_node) == 1);
 225
 226dai_link_of_err:
 227        of_node_put(cpu_ep);
 228        of_node_put(rcpu_ep);
 229        of_node_put(codec_ep);
 230
 231        return ret;
 232}
 233
 234static int asoc_graph_card_parse_of(struct graph_card_data *priv)
 235{
 236        struct of_phandle_iterator it;
 237        struct device *dev = graph_priv_to_dev(priv);
 238        struct snd_soc_card *card = graph_priv_to_card(priv);
 239        struct device_node *node = dev->of_node;
 240        int rc, idx = 0;
 241        int ret;
 242
 243        ret = asoc_simple_card_of_parse_widgets(card, NULL);
 244        if (ret < 0)
 245                return ret;
 246
 247        ret = asoc_simple_card_of_parse_routing(card, NULL, 1);
 248        if (ret < 0)
 249                return ret;
 250
 251        /* Factor to mclk, used in hw_params() */
 252        of_property_read_u32(node, "mclk-fs", &priv->mclk_fs);
 253
 254        of_for_each_phandle(&it, rc, node, "dais", NULL, 0) {
 255                ret = asoc_graph_card_dai_link_of(it.node, priv, idx++);
 256                if (ret < 0) {
 257                        of_node_put(it.node);
 258
 259                        return ret;
 260                }
 261        }
 262
 263        return asoc_simple_card_parse_card_name(card, NULL);
 264}
 265
 266static int asoc_graph_get_dais_count(struct device *dev)
 267{
 268        struct of_phandle_iterator it;
 269        struct device_node *node = dev->of_node;
 270        int count = 0;
 271        int rc;
 272
 273        of_for_each_phandle(&it, rc, node, "dais", NULL, 0)
 274                count++;
 275
 276        return count;
 277}
 278
 279static int asoc_graph_soc_card_probe(struct snd_soc_card *card)
 280{
 281        struct graph_card_data *priv = snd_soc_card_get_drvdata(card);
 282        int ret;
 283
 284        ret = asoc_simple_card_init_hp(card, &priv->hp_jack, NULL);
 285        if (ret < 0)
 286                return ret;
 287
 288        ret = asoc_simple_card_init_mic(card, &priv->mic_jack, NULL);
 289        if (ret < 0)
 290                return ret;
 291
 292        return 0;
 293}
 294
 295static int asoc_graph_card_probe(struct platform_device *pdev)
 296{
 297        struct graph_card_data *priv;
 298        struct snd_soc_dai_link *dai_link;
 299        struct graph_dai_props *dai_props;
 300        struct device *dev = &pdev->dev;
 301        struct snd_soc_card *card;
 302        int num, ret;
 303
 304        /* Allocate the private data and the DAI link array */
 305        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 306        if (!priv)
 307                return -ENOMEM;
 308
 309        num = asoc_graph_get_dais_count(dev);
 310        if (num == 0)
 311                return -EINVAL;
 312
 313        dai_props = devm_kcalloc(dev, num, sizeof(*dai_props), GFP_KERNEL);
 314        dai_link  = devm_kcalloc(dev, num, sizeof(*dai_link), GFP_KERNEL);
 315        if (!dai_props || !dai_link)
 316                return -ENOMEM;
 317
 318        priv->pa_gpio = devm_gpiod_get_optional(dev, "pa", GPIOD_OUT_LOW);
 319        if (IS_ERR(priv->pa_gpio)) {
 320                ret = PTR_ERR(priv->pa_gpio);
 321                dev_err(dev, "failed to get amplifier gpio: %d\n", ret);
 322                return ret;
 323        }
 324
 325        priv->dai_props                 = dai_props;
 326        priv->dai_link                  = dai_link;
 327
 328        /* Init snd_soc_card */
 329        card = graph_priv_to_card(priv);
 330        card->owner     = THIS_MODULE;
 331        card->dev       = dev;
 332        card->dai_link  = dai_link;
 333        card->num_links = num;
 334        card->dapm_widgets = asoc_graph_card_dapm_widgets;
 335        card->num_dapm_widgets = ARRAY_SIZE(asoc_graph_card_dapm_widgets);
 336        card->probe     = asoc_graph_soc_card_probe;
 337
 338        ret = asoc_graph_card_parse_of(priv);
 339        if (ret < 0) {
 340                if (ret != -EPROBE_DEFER)
 341                        dev_err(dev, "parse error %d\n", ret);
 342                goto err;
 343        }
 344
 345        snd_soc_card_set_drvdata(card, priv);
 346
 347        ret = devm_snd_soc_register_card(dev, card);
 348        if (ret < 0)
 349                goto err;
 350
 351        return 0;
 352err:
 353        asoc_simple_card_clean_reference(card);
 354
 355        return ret;
 356}
 357
 358static int asoc_graph_card_remove(struct platform_device *pdev)
 359{
 360        struct snd_soc_card *card = platform_get_drvdata(pdev);
 361
 362        return asoc_simple_card_clean_reference(card);
 363}
 364
 365static const struct of_device_id asoc_graph_of_match[] = {
 366        { .compatible = "audio-graph-card", },
 367        {},
 368};
 369MODULE_DEVICE_TABLE(of, asoc_graph_of_match);
 370
 371static struct platform_driver asoc_graph_card = {
 372        .driver = {
 373                .name = "asoc-audio-graph-card",
 374                .pm = &snd_soc_pm_ops,
 375                .of_match_table = asoc_graph_of_match,
 376        },
 377        .probe = asoc_graph_card_probe,
 378        .remove = asoc_graph_card_remove,
 379};
 380module_platform_driver(asoc_graph_card);
 381
 382MODULE_ALIAS("platform:asoc-audio-graph-card");
 383MODULE_LICENSE("GPL v2");
 384MODULE_DESCRIPTION("ASoC Audio Graph Sound Card");
 385MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
 386