1e37a0c31SJerome Brunet // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2e37a0c31SJerome Brunet //
3e37a0c31SJerome Brunet // Copyright (c) 2020 BayLibre, SAS.
4e37a0c31SJerome Brunet // Author: Jerome Brunet <jbrunet@baylibre.com>
5e37a0c31SJerome Brunet
6e37a0c31SJerome Brunet #include <linux/module.h>
7e37a0c31SJerome Brunet #include <linux/of_platform.h>
8e37a0c31SJerome Brunet #include <sound/soc.h>
9e37a0c31SJerome Brunet #include <sound/soc-dai.h>
10e37a0c31SJerome Brunet
11e37a0c31SJerome Brunet #include "meson-card.h"
12e37a0c31SJerome Brunet
13e37a0c31SJerome Brunet struct gx_dai_link_i2s_data {
14e37a0c31SJerome Brunet unsigned int mclk_fs;
15e37a0c31SJerome Brunet };
16e37a0c31SJerome Brunet
17e37a0c31SJerome Brunet /*
18e37a0c31SJerome Brunet * Base params for the codec to codec links
19e37a0c31SJerome Brunet * Those will be over-written by the CPU side of the link
20e37a0c31SJerome Brunet */
21e37a0c31SJerome Brunet static const struct snd_soc_pcm_stream codec_params = {
22e37a0c31SJerome Brunet .formats = SNDRV_PCM_FMTBIT_S24_LE,
23e37a0c31SJerome Brunet .rate_min = 5525,
24e37a0c31SJerome Brunet .rate_max = 192000,
25e37a0c31SJerome Brunet .channels_min = 1,
26e37a0c31SJerome Brunet .channels_max = 8,
27e37a0c31SJerome Brunet };
28e37a0c31SJerome Brunet
gx_card_i2s_be_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)29e37a0c31SJerome Brunet static int gx_card_i2s_be_hw_params(struct snd_pcm_substream *substream,
30e37a0c31SJerome Brunet struct snd_pcm_hw_params *params)
31e37a0c31SJerome Brunet {
32371a014dSKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
33e37a0c31SJerome Brunet struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
34e37a0c31SJerome Brunet struct gx_dai_link_i2s_data *be =
35e37a0c31SJerome Brunet (struct gx_dai_link_i2s_data *)priv->link_data[rtd->num];
36e37a0c31SJerome Brunet
37e37a0c31SJerome Brunet return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
38e37a0c31SJerome Brunet }
39e37a0c31SJerome Brunet
40e37a0c31SJerome Brunet static const struct snd_soc_ops gx_card_i2s_be_ops = {
41e37a0c31SJerome Brunet .hw_params = gx_card_i2s_be_hw_params,
42e37a0c31SJerome Brunet };
43e37a0c31SJerome Brunet
gx_card_parse_i2s(struct snd_soc_card * card,struct device_node * node,int * index)44e37a0c31SJerome Brunet static int gx_card_parse_i2s(struct snd_soc_card *card,
45e37a0c31SJerome Brunet struct device_node *node,
46e37a0c31SJerome Brunet int *index)
47e37a0c31SJerome Brunet {
48e37a0c31SJerome Brunet struct meson_card *priv = snd_soc_card_get_drvdata(card);
49e37a0c31SJerome Brunet struct snd_soc_dai_link *link = &card->dai_link[*index];
50e37a0c31SJerome Brunet struct gx_dai_link_i2s_data *be;
51e37a0c31SJerome Brunet
52e37a0c31SJerome Brunet /* Allocate i2s link parameters */
53e37a0c31SJerome Brunet be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
54e37a0c31SJerome Brunet if (!be)
55e37a0c31SJerome Brunet return -ENOMEM;
56e37a0c31SJerome Brunet priv->link_data[*index] = be;
57e37a0c31SJerome Brunet
58e37a0c31SJerome Brunet /* Setup i2s link */
59e37a0c31SJerome Brunet link->ops = &gx_card_i2s_be_ops;
60e37a0c31SJerome Brunet link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
61e37a0c31SJerome Brunet
62e37a0c31SJerome Brunet of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
63e37a0c31SJerome Brunet
64e37a0c31SJerome Brunet return 0;
65e37a0c31SJerome Brunet }
66e37a0c31SJerome Brunet
gx_card_cpu_identify(struct snd_soc_dai_link_component * c,char * match)67e37a0c31SJerome Brunet static int gx_card_cpu_identify(struct snd_soc_dai_link_component *c,
68e37a0c31SJerome Brunet char *match)
69e37a0c31SJerome Brunet {
70e37a0c31SJerome Brunet if (of_device_is_compatible(c->of_node, DT_PREFIX "aiu")) {
71e37a0c31SJerome Brunet if (strstr(c->dai_name, match))
72e37a0c31SJerome Brunet return 1;
73e37a0c31SJerome Brunet }
74e37a0c31SJerome Brunet
75e37a0c31SJerome Brunet /* dai not matched */
76e37a0c31SJerome Brunet return 0;
77e37a0c31SJerome Brunet }
78e37a0c31SJerome Brunet
gx_card_add_link(struct snd_soc_card * card,struct device_node * np,int * index)79e37a0c31SJerome Brunet static int gx_card_add_link(struct snd_soc_card *card, struct device_node *np,
80e37a0c31SJerome Brunet int *index)
81e37a0c31SJerome Brunet {
82e37a0c31SJerome Brunet struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
83e37a0c31SJerome Brunet struct snd_soc_dai_link_component *cpu;
84e37a0c31SJerome Brunet int ret;
85e37a0c31SJerome Brunet
86e37a0c31SJerome Brunet cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
87e37a0c31SJerome Brunet if (!cpu)
88e37a0c31SJerome Brunet return -ENOMEM;
89e37a0c31SJerome Brunet
90e37a0c31SJerome Brunet dai_link->cpus = cpu;
91e37a0c31SJerome Brunet dai_link->num_cpus = 1;
92e37a0c31SJerome Brunet
93e37a0c31SJerome Brunet ret = meson_card_parse_dai(card, np, dai_link->cpus);
94e37a0c31SJerome Brunet if (ret)
95e37a0c31SJerome Brunet return ret;
96e37a0c31SJerome Brunet
97e37a0c31SJerome Brunet if (gx_card_cpu_identify(dai_link->cpus, "FIFO"))
98e37a0c31SJerome Brunet return meson_card_set_fe_link(card, dai_link, np, true);
99da3f23fdSJerome Brunet
100e37a0c31SJerome Brunet ret = meson_card_set_be_link(card, dai_link, np);
101da3f23fdSJerome Brunet if (ret)
102e37a0c31SJerome Brunet return ret;
103e37a0c31SJerome Brunet
104e37a0c31SJerome Brunet /* Or apply codec to codec params if necessary */
105da3f23fdSJerome Brunet if (gx_card_cpu_identify(dai_link->cpus, "CODEC CTRL")) {
106da3f23fdSJerome Brunet dai_link->c2c_params = &codec_params;
107*433f4a16SKuninori Morimoto dai_link->num_c2c_params = 1;
108*433f4a16SKuninori Morimoto } else {
109da3f23fdSJerome Brunet dai_link->no_pcm = 1;
110da3f23fdSJerome Brunet snd_soc_dai_link_set_capabilities(dai_link);
111da3f23fdSJerome Brunet /* Check if the cpu is the i2s encoder and parse i2s data */
112e37a0c31SJerome Brunet if (gx_card_cpu_identify(dai_link->cpus, "I2S Encoder"))
113e37a0c31SJerome Brunet ret = gx_card_parse_i2s(card, np, index);
114e37a0c31SJerome Brunet }
115de911b4eSJerome Brunet
116e37a0c31SJerome Brunet return ret;
117e37a0c31SJerome Brunet }
118e37a0c31SJerome Brunet
119e37a0c31SJerome Brunet static const struct meson_card_match_data gx_card_match_data = {
120e37a0c31SJerome Brunet .add_link = gx_card_add_link,
121e37a0c31SJerome Brunet };
122e37a0c31SJerome Brunet
123e37a0c31SJerome Brunet static const struct of_device_id gx_card_of_match[] = {
124e37a0c31SJerome Brunet {
125e37a0c31SJerome Brunet .compatible = "amlogic,gx-sound-card",
126e37a0c31SJerome Brunet .data = &gx_card_match_data,
127e37a0c31SJerome Brunet }, {}
128e37a0c31SJerome Brunet };
129e37a0c31SJerome Brunet MODULE_DEVICE_TABLE(of, gx_card_of_match);
130e37a0c31SJerome Brunet
131e37a0c31SJerome Brunet static struct platform_driver gx_card_pdrv = {
132e37a0c31SJerome Brunet .probe = meson_card_probe,
133e37a0c31SJerome Brunet .remove = meson_card_remove,
134e37a0c31SJerome Brunet .driver = {
135e37a0c31SJerome Brunet .name = "gx-sound-card",
136e37a0c31SJerome Brunet .of_match_table = gx_card_of_match,
137e37a0c31SJerome Brunet },
138e37a0c31SJerome Brunet };
139e37a0c31SJerome Brunet module_platform_driver(gx_card_pdrv);
140e37a0c31SJerome Brunet
141e37a0c31SJerome Brunet MODULE_DESCRIPTION("Amlogic GX ALSA machine driver");
142e37a0c31SJerome Brunet MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
143e37a0c31SJerome Brunet MODULE_LICENSE("GPL v2");
144e37a0c31SJerome Brunet