xref: /linux/sound/soc/loongson/loongson_card.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Loongson ASoC Audio Machine driver
4 //
5 // Copyright (C) 2023 Loongson Technology Corporation Limited
6 // Author: Yingkun Meng <mengyingkun@loongson.cn>
7 //
8 
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/soc-acpi.h>
12 #include <linux/acpi.h>
13 #include <linux/pci.h>
14 #include <sound/pcm_params.h>
15 
16 static char codec_name[SND_ACPI_I2C_ID_LEN];
17 
18 struct loongson_card_data {
19 	struct snd_soc_card snd_card;
20 	unsigned int mclk_fs;
21 };
22 
23 static int loongson_card_hw_params(struct snd_pcm_substream *substream,
24 				   struct snd_pcm_hw_params *params)
25 {
26 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
27 	struct loongson_card_data *ls_card = snd_soc_card_get_drvdata(rtd->card);
28 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
29 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
30 	int ret, mclk;
31 
32 	if (!ls_card->mclk_fs)
33 		return 0;
34 
35 	mclk = ls_card->mclk_fs * params_rate(params);
36 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT);
37 	if (ret < 0) {
38 		dev_err(codec_dai->dev, "cpu_dai clock not set\n");
39 		return ret;
40 	}
41 
42 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
43 	if (ret < 0) {
44 		dev_err(codec_dai->dev, "codec_dai clock not set\n");
45 		return ret;
46 	}
47 
48 	return 0;
49 }
50 
51 static const struct snd_soc_ops loongson_ops = {
52 	.hw_params = loongson_card_hw_params,
53 };
54 
55 SND_SOC_DAILINK_DEFS(analog,
56 	DAILINK_COMP_ARRAY(COMP_CPU("loongson-i2s")),
57 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
58 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
59 
60 static struct snd_soc_dai_link loongson_dai_links[] = {
61 	{
62 		.name = "Loongson Audio Port",
63 		.stream_name = "Loongson Audio",
64 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_IB_NF
65 			| SND_SOC_DAIFMT_CBC_CFC,
66 		SND_SOC_DAILINK_REG(analog),
67 		.ops = &loongson_ops,
68 	},
69 };
70 
71 static struct acpi_device *loongson_card_acpi_find_device(struct snd_soc_card *card,
72 							  const char *name)
73 {
74 	struct fwnode_handle *fwnode = card->dev->fwnode;
75 	struct fwnode_reference_args args;
76 	int status;
77 
78 	memset(&args, 0, sizeof(args));
79 	status = acpi_node_get_property_reference(fwnode, name, 0, &args);
80 	if (status || !is_acpi_device_node(args.fwnode)) {
81 		dev_err(card->dev, "No matching phy in ACPI table\n");
82 		return NULL;
83 	}
84 
85 	return to_acpi_device_node(args.fwnode);
86 }
87 
88 static int loongson_card_parse_acpi(struct loongson_card_data *data)
89 {
90 	struct snd_soc_card *card = &data->snd_card;
91 	const char *codec_dai_name;
92 	struct acpi_device *adev;
93 	struct device *phy_dev;
94 	int i;
95 
96 	/* fixup platform name based on reference node */
97 	adev = loongson_card_acpi_find_device(card, "cpu");
98 	if (!adev)
99 		return -ENOENT;
100 
101 	phy_dev = acpi_get_first_physical_node(adev);
102 	if (!phy_dev)
103 		return -EPROBE_DEFER;
104 
105 	/* fixup codec name based on reference node */
106 	adev = loongson_card_acpi_find_device(card, "codec");
107 	if (!adev)
108 		return -ENOENT;
109 	snprintf(codec_name, sizeof(codec_name), "i2c-%s", acpi_dev_name(adev));
110 
111 	device_property_read_string(card->dev, "codec-dai-name", &codec_dai_name);
112 
113 	for (i = 0; i < card->num_links; i++) {
114 		loongson_dai_links[i].platforms->name = dev_name(phy_dev);
115 		loongson_dai_links[i].codecs->name = codec_name;
116 		loongson_dai_links[i].codecs->dai_name = codec_dai_name;
117 	}
118 
119 	return 0;
120 }
121 
122 static int loongson_card_parse_of(struct loongson_card_data *data)
123 {
124 	struct device_node *cpu, *codec;
125 	struct snd_soc_card *card = &data->snd_card;
126 	struct device *dev = card->dev;
127 	int ret, i;
128 
129 	cpu = of_get_child_by_name(dev->of_node, "cpu");
130 	if (!cpu) {
131 		dev_err(dev, "platform property missing or invalid\n");
132 		return -EINVAL;
133 	}
134 	codec = of_get_child_by_name(dev->of_node, "codec");
135 	if (!codec) {
136 		dev_err(dev, "audio-codec property missing or invalid\n");
137 		of_node_put(cpu);
138 		return -EINVAL;
139 	}
140 
141 	for (i = 0; i < card->num_links; i++) {
142 		ret = snd_soc_of_get_dlc(cpu, NULL, loongson_dai_links[i].cpus, 0);
143 		if (ret < 0) {
144 			dev_err(dev, "getting cpu dlc error (%d)\n", ret);
145 			goto err;
146 		}
147 		loongson_dai_links[i].platforms->of_node = loongson_dai_links[i].cpus->of_node;
148 
149 		ret = snd_soc_of_get_dlc(codec, NULL, loongson_dai_links[i].codecs, 0);
150 		if (ret < 0) {
151 			dev_err(dev, "getting codec dlc error (%d)\n", ret);
152 			goto err;
153 		}
154 	}
155 
156 	of_node_put(cpu);
157 	of_node_put(codec);
158 
159 	return 0;
160 
161 err:
162 	of_node_put(cpu);
163 	of_node_put(codec);
164 	return ret;
165 }
166 
167 static int loongson_asoc_card_probe(struct platform_device *pdev)
168 {
169 	struct loongson_card_data *ls_priv;
170 	struct device *dev = &pdev->dev;
171 	struct snd_soc_card *card;
172 	int ret;
173 
174 	ls_priv = devm_kzalloc(dev, sizeof(*ls_priv), GFP_KERNEL);
175 	if (!ls_priv)
176 		return -ENOMEM;
177 
178 	card = &ls_priv->snd_card;
179 
180 	card->dev = dev;
181 	card->owner = THIS_MODULE;
182 	card->dai_link = loongson_dai_links;
183 	card->num_links = ARRAY_SIZE(loongson_dai_links);
184 	snd_soc_card_set_drvdata(card, ls_priv);
185 
186 	ret = device_property_read_string(dev, "model", &card->name);
187 	if (ret)
188 		return dev_err_probe(dev, ret, "Error parsing card name\n");
189 
190 	ret = device_property_read_u32(dev, "mclk-fs", &ls_priv->mclk_fs);
191 	if (ret)
192 		return dev_err_probe(dev, ret, "Error parsing mclk-fs\n");
193 
194 	ret = has_acpi_companion(dev) ? loongson_card_parse_acpi(ls_priv)
195 				      : loongson_card_parse_of(ls_priv);
196 	if (ret)
197 		return dev_err_probe(dev, ret, "Error parsing acpi/of properties\n");
198 
199 	return devm_snd_soc_register_card(dev, card);
200 }
201 
202 static const struct of_device_id loongson_asoc_dt_ids[] = {
203 	{ .compatible = "loongson,ls-audio-card" },
204 	{ /* sentinel */ },
205 };
206 MODULE_DEVICE_TABLE(of, loongson_asoc_dt_ids);
207 
208 static struct platform_driver loongson_audio_driver = {
209 	.probe = loongson_asoc_card_probe,
210 	.driver = {
211 		.name = "loongson-asoc-card",
212 		.pm = &snd_soc_pm_ops,
213 		.of_match_table = loongson_asoc_dt_ids,
214 	},
215 };
216 module_platform_driver(loongson_audio_driver);
217 
218 MODULE_DESCRIPTION("Loongson ASoc Sound Card driver");
219 MODULE_AUTHOR("Loongson Technology Corporation Limited");
220 MODULE_LICENSE("GPL");
221