dd354c04e3d318201c29c763715b93a57ea18f6e
[openwrt/staging/981213.git] /
1 From ddf6abc1c78072f8ccad59166be95f0ca5af8ca4 Mon Sep 17 00:00:00 2001
2 From: Maso Huang <maso.huang@mediatek.com>
3 Date: Thu, 17 Aug 2023 18:13:36 +0800
4 Subject: [PATCH 4/9] ASoC: mediatek: mt7986: add machine driver with wm8960
5
6 Add support for mt7986 board with wm8960.
7
8 Signed-off-by: Maso Huang <maso.huang@mediatek.com>
9 Reviewed-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
10 Link: https://lore.kernel.org/r/20230817101338.18782-5-maso.huang@mediatek.com
11 Signed-off-by: Mark Brown <broonie@kernel.org>
12 ---
13 sound/soc/mediatek/Kconfig | 10 ++
14 sound/soc/mediatek/mt7986/Makefile | 1 +
15 sound/soc/mediatek/mt7986/mt7986-wm8960.c | 196 ++++++++++++++++++++++
16 3 files changed, 207 insertions(+)
17 create mode 100644 sound/soc/mediatek/mt7986/mt7986-wm8960.c
18
19 --- a/sound/soc/mediatek/Kconfig
20 +++ b/sound/soc/mediatek/Kconfig
21 @@ -64,6 +64,16 @@ config SND_SOC_MT7986
22 Select Y if you have such device.
23 If unsure select "N".
24
25 +config SND_SOC_MT7986_WM8960
26 + tristate "ASoc Audio driver for MT7986 with WM8960 codec"
27 + depends on SND_SOC_MT7986 && I2C
28 + select SND_SOC_WM8960
29 + help
30 + This adds support for ASoC machine driver for MediaTek MT7986
31 + boards with the WM8960 codecs.
32 + Select Y if you have such device.
33 + If unsure select "N".
34 +
35 config SND_SOC_MT8173
36 tristate "ASoC support for Mediatek MT8173 chip"
37 depends on ARCH_MEDIATEK
38 --- a/sound/soc/mediatek/mt7986/Makefile
39 +++ b/sound/soc/mediatek/mt7986/Makefile
40 @@ -6,3 +6,4 @@ snd-soc-mt7986-afe-objs := \
41 mt7986-dai-etdm.o
42
43 obj-$(CONFIG_SND_SOC_MT7986) += snd-soc-mt7986-afe.o
44 +obj-$(CONFIG_SND_SOC_MT7986_WM8960) += mt7986-wm8960.o
45 --- /dev/null
46 +++ b/sound/soc/mediatek/mt7986/mt7986-wm8960.c
47 @@ -0,0 +1,196 @@
48 +// SPDX-License-Identifier: GPL-2.0
49 +/*
50 + * mt7986-wm8960.c -- MT7986-WM8960 ALSA SoC machine driver
51 + *
52 + * Copyright (c) 2023 MediaTek Inc.
53 + * Authors: Vic Wu <vic.wu@mediatek.com>
54 + * Maso Huang <maso.huang@mediatek.com>
55 + */
56 +
57 +#include <linux/module.h>
58 +#include <sound/soc.h>
59 +
60 +#include "mt7986-afe-common.h"
61 +
62 +struct mt7986_wm8960_priv {
63 + struct device_node *platform_node;
64 + struct device_node *codec_node;
65 +};
66 +
67 +static const struct snd_soc_dapm_widget mt7986_wm8960_widgets[] = {
68 + SND_SOC_DAPM_HP("Headphone", NULL),
69 + SND_SOC_DAPM_MIC("AMIC", NULL),
70 +};
71 +
72 +static const struct snd_kcontrol_new mt7986_wm8960_controls[] = {
73 + SOC_DAPM_PIN_SWITCH("Headphone"),
74 + SOC_DAPM_PIN_SWITCH("AMIC"),
75 +};
76 +
77 +SND_SOC_DAILINK_DEFS(playback,
78 + DAILINK_COMP_ARRAY(COMP_CPU("DL1")),
79 + DAILINK_COMP_ARRAY(COMP_DUMMY()),
80 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
81 +
82 +SND_SOC_DAILINK_DEFS(capture,
83 + DAILINK_COMP_ARRAY(COMP_CPU("UL1")),
84 + DAILINK_COMP_ARRAY(COMP_DUMMY()),
85 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
86 +
87 +SND_SOC_DAILINK_DEFS(codec,
88 + DAILINK_COMP_ARRAY(COMP_CPU("ETDM")),
89 + DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8960-hifi")),
90 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
91 +
92 +static struct snd_soc_dai_link mt7986_wm8960_dai_links[] = {
93 + /* FE */
94 + {
95 + .name = "wm8960-playback",
96 + .stream_name = "wm8960-playback",
97 + .trigger = {SND_SOC_DPCM_TRIGGER_POST,
98 + SND_SOC_DPCM_TRIGGER_POST},
99 + .dynamic = 1,
100 + .dpcm_playback = 1,
101 + SND_SOC_DAILINK_REG(playback),
102 + },
103 + {
104 + .name = "wm8960-capture",
105 + .stream_name = "wm8960-capture",
106 + .trigger = {SND_SOC_DPCM_TRIGGER_POST,
107 + SND_SOC_DPCM_TRIGGER_POST},
108 + .dynamic = 1,
109 + .dpcm_capture = 1,
110 + SND_SOC_DAILINK_REG(capture),
111 + },
112 + /* BE */
113 + {
114 + .name = "wm8960-codec",
115 + .no_pcm = 1,
116 + .dai_fmt = SND_SOC_DAIFMT_I2S |
117 + SND_SOC_DAIFMT_NB_NF |
118 + SND_SOC_DAIFMT_CBS_CFS |
119 + SND_SOC_DAIFMT_GATED,
120 + .dpcm_playback = 1,
121 + .dpcm_capture = 1,
122 + SND_SOC_DAILINK_REG(codec),
123 + },
124 +};
125 +
126 +static struct snd_soc_card mt7986_wm8960_card = {
127 + .name = "mt7986-wm8960",
128 + .owner = THIS_MODULE,
129 + .dai_link = mt7986_wm8960_dai_links,
130 + .num_links = ARRAY_SIZE(mt7986_wm8960_dai_links),
131 + .controls = mt7986_wm8960_controls,
132 + .num_controls = ARRAY_SIZE(mt7986_wm8960_controls),
133 + .dapm_widgets = mt7986_wm8960_widgets,
134 + .num_dapm_widgets = ARRAY_SIZE(mt7986_wm8960_widgets),
135 +};
136 +
137 +static int mt7986_wm8960_machine_probe(struct platform_device *pdev)
138 +{
139 + struct snd_soc_card *card = &mt7986_wm8960_card;
140 + struct snd_soc_dai_link *dai_link;
141 + struct device_node *platform, *codec;
142 + struct mt7986_wm8960_priv *priv;
143 + int ret, i;
144 +
145 + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
146 + if (!priv)
147 + return -ENOMEM;
148 +
149 + platform = of_get_child_by_name(pdev->dev.of_node, "platform");
150 +
151 + if (platform) {
152 + priv->platform_node = of_parse_phandle(platform, "sound-dai", 0);
153 + of_node_put(platform);
154 +
155 + if (!priv->platform_node) {
156 + dev_err(&pdev->dev, "Failed to parse platform/sound-dai property\n");
157 + return -EINVAL;
158 + }
159 + } else {
160 + dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
161 + return -EINVAL;
162 + }
163 +
164 + for_each_card_prelinks(card, i, dai_link) {
165 + if (dai_link->platforms->name)
166 + continue;
167 + dai_link->platforms->of_node = priv->platform_node;
168 + }
169 +
170 + card->dev = &pdev->dev;
171 +
172 + codec = of_get_child_by_name(pdev->dev.of_node, "codec");
173 +
174 + if (codec) {
175 + priv->codec_node = of_parse_phandle(codec, "sound-dai", 0);
176 + of_node_put(codec);
177 +
178 + if (!priv->codec_node) {
179 + of_node_put(priv->platform_node);
180 + dev_err(&pdev->dev, "Failed to parse codec/sound-dai property\n");
181 + return -EINVAL;
182 + }
183 + } else {
184 + of_node_put(priv->platform_node);
185 + dev_err(&pdev->dev, "Property 'codec' missing or invalid\n");
186 + return -EINVAL;
187 + }
188 +
189 + for_each_card_prelinks(card, i, dai_link) {
190 + if (dai_link->codecs->name)
191 + continue;
192 + dai_link->codecs->of_node = priv->codec_node;
193 + }
194 +
195 + ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
196 + if (ret) {
197 + dev_err(&pdev->dev, "Failed to parse audio-routing: %d\n", ret);
198 + goto err_of_node_put;
199 + }
200 +
201 + ret = devm_snd_soc_register_card(&pdev->dev, card);
202 + if (ret) {
203 + dev_err(&pdev->dev, "%s snd_soc_register_card fail: %d\n", __func__, ret);
204 + goto err_of_node_put;
205 + }
206 +
207 +err_of_node_put:
208 + of_node_put(priv->codec_node);
209 + of_node_put(priv->platform_node);
210 + return ret;
211 +}
212 +
213 +static void mt7986_wm8960_machine_remove(struct platform_device *pdev)
214 +{
215 + struct snd_soc_card *card = platform_get_drvdata(pdev);
216 + struct mt7986_wm8960_priv *priv = snd_soc_card_get_drvdata(card);
217 +
218 + of_node_put(priv->codec_node);
219 + of_node_put(priv->platform_node);
220 +}
221 +
222 +static const struct of_device_id mt7986_wm8960_machine_dt_match[] = {
223 + {.compatible = "mediatek,mt7986-wm8960-sound"},
224 + { /* sentinel */ }
225 +};
226 +MODULE_DEVICE_TABLE(of, mt7986_wm8960_machine_dt_match);
227 +
228 +static struct platform_driver mt7986_wm8960_machine = {
229 + .driver = {
230 + .name = "mt7986-wm8960",
231 + .of_match_table = mt7986_wm8960_machine_dt_match,
232 + },
233 + .probe = mt7986_wm8960_machine_probe,
234 + .remove_new = mt7986_wm8960_machine_remove,
235 +};
236 +
237 +module_platform_driver(mt7986_wm8960_machine);
238 +
239 +/* Module information */
240 +MODULE_DESCRIPTION("MT7986 WM8960 ALSA SoC machine driver");
241 +MODULE_AUTHOR("Vic Wu <vic.wu@mediatek.com>");
242 +MODULE_LICENSE("GPL");
243 +MODULE_ALIAS("mt7986 wm8960 soc card");