1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * mt2701-wm8960.c -- MT2701 WM8960 ALSA SoC machine driver
4 *
5 * Copyright (c) 2017 MediaTek Inc.
6 * Author: Ryder Lee <[email protected]>
7 */
8
9 #include <linux/module.h>
10 #include <sound/soc.h>
11
12 #include "mt2701-afe-common.h"
13
14 static const struct snd_soc_dapm_widget mt2701_wm8960_widgets[] = {
15 SND_SOC_DAPM_HP("Headphone", NULL),
16 SND_SOC_DAPM_MIC("AMIC", NULL),
17 };
18
19 static const struct snd_kcontrol_new mt2701_wm8960_controls[] = {
20 SOC_DAPM_PIN_SWITCH("Headphone"),
21 SOC_DAPM_PIN_SWITCH("AMIC"),
22 };
23
mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)24 static int mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream *substream,
25 struct snd_pcm_hw_params *params)
26 {
27 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
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 unsigned int mclk_rate;
31 unsigned int rate = params_rate(params);
32 unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
33 unsigned int div_bck_over_lrck = 64;
34
35 mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
36
37 snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
38 snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
39
40 return 0;
41 }
42
43 static const struct snd_soc_ops mt2701_wm8960_be_ops = {
44 .hw_params = mt2701_wm8960_be_ops_hw_params
45 };
46
47 SND_SOC_DAILINK_DEFS(playback,
48 DAILINK_COMP_ARRAY(COMP_CPU("PCMO0")),
49 DAILINK_COMP_ARRAY(COMP_DUMMY()),
50 DAILINK_COMP_ARRAY(COMP_EMPTY()));
51
52 SND_SOC_DAILINK_DEFS(capture,
53 DAILINK_COMP_ARRAY(COMP_CPU("PCM0")),
54 DAILINK_COMP_ARRAY(COMP_DUMMY()),
55 DAILINK_COMP_ARRAY(COMP_EMPTY()));
56
57 SND_SOC_DAILINK_DEFS(codec,
58 DAILINK_COMP_ARRAY(COMP_CPU("I2S0")),
59 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8960-hifi")),
60 DAILINK_COMP_ARRAY(COMP_EMPTY()));
61
62 static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {
63 /* FE */
64 {
65 .name = "wm8960-playback",
66 .stream_name = "wm8960-playback",
67 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
68 SND_SOC_DPCM_TRIGGER_POST},
69 .dynamic = 1,
70 .playback_only = 1,
71 SND_SOC_DAILINK_REG(playback),
72 },
73 {
74 .name = "wm8960-capture",
75 .stream_name = "wm8960-capture",
76 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
77 SND_SOC_DPCM_TRIGGER_POST},
78 .dynamic = 1,
79 .capture_only = 1,
80 SND_SOC_DAILINK_REG(capture),
81 },
82 /* BE */
83 {
84 .name = "wm8960-codec",
85 .no_pcm = 1,
86 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
87 | SND_SOC_DAIFMT_GATED,
88 .ops = &mt2701_wm8960_be_ops,
89 SND_SOC_DAILINK_REG(codec),
90 },
91 };
92
93 static struct snd_soc_card mt2701_wm8960_card = {
94 .name = "mt2701-wm8960",
95 .owner = THIS_MODULE,
96 .dai_link = mt2701_wm8960_dai_links,
97 .num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),
98 .controls = mt2701_wm8960_controls,
99 .num_controls = ARRAY_SIZE(mt2701_wm8960_controls),
100 .dapm_widgets = mt2701_wm8960_widgets,
101 .num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),
102 };
103
mt2701_wm8960_machine_probe(struct platform_device * pdev)104 static int mt2701_wm8960_machine_probe(struct platform_device *pdev)
105 {
106 struct snd_soc_card *card = &mt2701_wm8960_card;
107 struct device_node *platform_node, *codec_node;
108 struct snd_soc_dai_link *dai_link;
109 int ret, i;
110
111 platform_node = of_parse_phandle(pdev->dev.of_node,
112 "mediatek,platform", 0);
113 if (!platform_node) {
114 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
115 return -EINVAL;
116 }
117 for_each_card_prelinks(card, i, dai_link) {
118 if (dai_link->platforms->name)
119 continue;
120 dai_link->platforms->of_node = platform_node;
121 }
122
123 card->dev = &pdev->dev;
124
125 codec_node = of_parse_phandle(pdev->dev.of_node,
126 "mediatek,audio-codec", 0);
127 if (!codec_node) {
128 dev_err(&pdev->dev,
129 "Property 'audio-codec' missing or invalid\n");
130 ret = -EINVAL;
131 goto put_platform_node;
132 }
133 for_each_card_prelinks(card, i, dai_link) {
134 if (dai_link->codecs->name)
135 continue;
136 dai_link->codecs->of_node = codec_node;
137 }
138
139 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
140 if (ret) {
141 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
142 goto put_codec_node;
143 }
144
145 ret = devm_snd_soc_register_card(&pdev->dev, card);
146 if (ret)
147 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
148 __func__, ret);
149
150 put_codec_node:
151 of_node_put(codec_node);
152 put_platform_node:
153 of_node_put(platform_node);
154 return ret;
155 }
156
157 #ifdef CONFIG_OF
158 static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {
159 {.compatible = "mediatek,mt2701-wm8960-machine",},
160 {}
161 };
162 MODULE_DEVICE_TABLE(of, mt2701_wm8960_machine_dt_match);
163 #endif
164
165 static struct platform_driver mt2701_wm8960_machine = {
166 .driver = {
167 .name = "mt2701-wm8960",
168 #ifdef CONFIG_OF
169 .of_match_table = mt2701_wm8960_machine_dt_match,
170 #endif
171 },
172 .probe = mt2701_wm8960_machine_probe,
173 };
174
175 module_platform_driver(mt2701_wm8960_machine);
176
177 /* Module information */
178 MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");
179 MODULE_AUTHOR("Ryder Lee <[email protected]>");
180 MODULE_LICENSE("GPL v2");
181 MODULE_ALIAS("mt2701 wm8960 soc card");
182
183