GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / mediatek / mt8173 / mt8173-rt5650-rt5514.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8173-rt5650-rt5514.c  --  MT8173 machine driver with RT5650/5514 codecs
4  *
5  * Copyright (c) 2016 MediaTek Inc.
6  * Author: Koro Chen <koro.chen@mediatek.com>
7  */
8
9 #include <linux/module.h>
10 #include <linux/gpio.h>
11 #include <linux/of_gpio.h>
12 #include <sound/soc.h>
13 #include <sound/jack.h>
14 #include "../../codecs/rt5645.h"
15
16 #define MCLK_FOR_CODECS         12288000
17
18 static const struct snd_soc_dapm_widget mt8173_rt5650_rt5514_widgets[] = {
19         SND_SOC_DAPM_SPK("Speaker", NULL),
20         SND_SOC_DAPM_MIC("Int Mic", NULL),
21         SND_SOC_DAPM_HP("Headphone", NULL),
22         SND_SOC_DAPM_MIC("Headset Mic", NULL),
23 };
24
25 static const struct snd_soc_dapm_route mt8173_rt5650_rt5514_routes[] = {
26         {"Speaker", NULL, "SPOL"},
27         {"Speaker", NULL, "SPOR"},
28         {"Sub DMIC1L", NULL, "Int Mic"},
29         {"Sub DMIC1R", NULL, "Int Mic"},
30         {"Headphone", NULL, "HPOL"},
31         {"Headphone", NULL, "HPOR"},
32         {"IN1P", NULL, "Headset Mic"},
33         {"IN1N", NULL, "Headset Mic"},
34 };
35
36 static const struct snd_kcontrol_new mt8173_rt5650_rt5514_controls[] = {
37         SOC_DAPM_PIN_SWITCH("Speaker"),
38         SOC_DAPM_PIN_SWITCH("Int Mic"),
39         SOC_DAPM_PIN_SWITCH("Headphone"),
40         SOC_DAPM_PIN_SWITCH("Headset Mic"),
41 };
42
43 static int mt8173_rt5650_rt5514_hw_params(struct snd_pcm_substream *substream,
44                                           struct snd_pcm_hw_params *params)
45 {
46         struct snd_soc_pcm_runtime *rtd = substream->private_data;
47         int i, ret;
48
49         for (i = 0; i < rtd->num_codecs; i++) {
50                 struct snd_soc_dai *codec_dai = rtd->codec_dais[i];
51
52                 /* pll from mclk 12.288M */
53                 ret = snd_soc_dai_set_pll(codec_dai, 0, 0, MCLK_FOR_CODECS,
54                                           params_rate(params) * 512);
55                 if (ret)
56                         return ret;
57
58                 /* sysclk from pll */
59                 ret = snd_soc_dai_set_sysclk(codec_dai, 1,
60                                              params_rate(params) * 512,
61                                              SND_SOC_CLOCK_IN);
62                 if (ret)
63                         return ret;
64         }
65         return 0;
66 }
67
68 static const struct snd_soc_ops mt8173_rt5650_rt5514_ops = {
69         .hw_params = mt8173_rt5650_rt5514_hw_params,
70 };
71
72 static struct snd_soc_jack mt8173_rt5650_rt5514_jack;
73
74 static int mt8173_rt5650_rt5514_init(struct snd_soc_pcm_runtime *runtime)
75 {
76         struct snd_soc_card *card = runtime->card;
77         struct snd_soc_component *component = runtime->codec_dais[0]->component;
78         int ret;
79
80         rt5645_sel_asrc_clk_src(component,
81                                 RT5645_DA_STEREO_FILTER |
82                                 RT5645_AD_STEREO_FILTER,
83                                 RT5645_CLK_SEL_I2S1_ASRC);
84
85         /* enable jack detection */
86         ret = snd_soc_card_jack_new(card, "Headset Jack",
87                                     SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
88                                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
89                                     SND_JACK_BTN_2 | SND_JACK_BTN_3,
90                                     &mt8173_rt5650_rt5514_jack, NULL, 0);
91         if (ret) {
92                 dev_err(card->dev, "Can't new Headset Jack %d\n", ret);
93                 return ret;
94         }
95
96         return rt5645_set_jack_detect(component,
97                                       &mt8173_rt5650_rt5514_jack,
98                                       &mt8173_rt5650_rt5514_jack,
99                                       &mt8173_rt5650_rt5514_jack);
100 }
101
102 static struct snd_soc_dai_link_component mt8173_rt5650_rt5514_codecs[] = {
103         {
104                 .dai_name = "rt5645-aif1",
105         },
106         {
107                 .dai_name = "rt5514-aif1",
108         },
109 };
110
111 enum {
112         DAI_LINK_PLAYBACK,
113         DAI_LINK_CAPTURE,
114         DAI_LINK_CODEC_I2S,
115 };
116
117 /* Digital audio interface glue - connects codec <---> CPU */
118 static struct snd_soc_dai_link mt8173_rt5650_rt5514_dais[] = {
119         /* Front End DAI links */
120         [DAI_LINK_PLAYBACK] = {
121                 .name = "rt5650_rt5514 Playback",
122                 .stream_name = "rt5650_rt5514 Playback",
123                 .cpu_dai_name = "DL1",
124                 .codec_name = "snd-soc-dummy",
125                 .codec_dai_name = "snd-soc-dummy-dai",
126                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
127                 .dynamic = 1,
128                 .dpcm_playback = 1,
129         },
130         [DAI_LINK_CAPTURE] = {
131                 .name = "rt5650_rt5514 Capture",
132                 .stream_name = "rt5650_rt5514 Capture",
133                 .cpu_dai_name = "VUL",
134                 .codec_name = "snd-soc-dummy",
135                 .codec_dai_name = "snd-soc-dummy-dai",
136                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
137                 .dynamic = 1,
138                 .dpcm_capture = 1,
139         },
140         /* Back End DAI links */
141         [DAI_LINK_CODEC_I2S] = {
142                 .name = "Codec",
143                 .cpu_dai_name = "I2S",
144                 .no_pcm = 1,
145                 .codecs = mt8173_rt5650_rt5514_codecs,
146                 .num_codecs = 2,
147                 .init = mt8173_rt5650_rt5514_init,
148                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
149                            SND_SOC_DAIFMT_CBS_CFS,
150                 .ops = &mt8173_rt5650_rt5514_ops,
151                 .ignore_pmdown_time = 1,
152                 .dpcm_playback = 1,
153                 .dpcm_capture = 1,
154         },
155 };
156
157 static struct snd_soc_codec_conf mt8173_rt5650_rt5514_codec_conf[] = {
158         {
159                 .name_prefix = "Sub",
160         },
161 };
162
163 static struct snd_soc_card mt8173_rt5650_rt5514_card = {
164         .name = "mtk-rt5650-rt5514",
165         .owner = THIS_MODULE,
166         .dai_link = mt8173_rt5650_rt5514_dais,
167         .num_links = ARRAY_SIZE(mt8173_rt5650_rt5514_dais),
168         .codec_conf = mt8173_rt5650_rt5514_codec_conf,
169         .num_configs = ARRAY_SIZE(mt8173_rt5650_rt5514_codec_conf),
170         .controls = mt8173_rt5650_rt5514_controls,
171         .num_controls = ARRAY_SIZE(mt8173_rt5650_rt5514_controls),
172         .dapm_widgets = mt8173_rt5650_rt5514_widgets,
173         .num_dapm_widgets = ARRAY_SIZE(mt8173_rt5650_rt5514_widgets),
174         .dapm_routes = mt8173_rt5650_rt5514_routes,
175         .num_dapm_routes = ARRAY_SIZE(mt8173_rt5650_rt5514_routes),
176 };
177
178 static int mt8173_rt5650_rt5514_dev_probe(struct platform_device *pdev)
179 {
180         struct snd_soc_card *card = &mt8173_rt5650_rt5514_card;
181         struct device_node *platform_node;
182         int i, ret;
183
184         platform_node = of_parse_phandle(pdev->dev.of_node,
185                                          "mediatek,platform", 0);
186         if (!platform_node) {
187                 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
188                 return -EINVAL;
189         }
190
191         for (i = 0; i < card->num_links; i++) {
192                 if (mt8173_rt5650_rt5514_dais[i].platform_name)
193                         continue;
194                 mt8173_rt5650_rt5514_dais[i].platform_of_node = platform_node;
195         }
196
197         mt8173_rt5650_rt5514_codecs[0].of_node =
198                 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 0);
199         if (!mt8173_rt5650_rt5514_codecs[0].of_node) {
200                 dev_err(&pdev->dev,
201                         "Property 'audio-codec' missing or invalid\n");
202                 ret = -EINVAL;
203                 goto out;
204         }
205         mt8173_rt5650_rt5514_codecs[1].of_node =
206                 of_parse_phandle(pdev->dev.of_node, "mediatek,audio-codec", 1);
207         if (!mt8173_rt5650_rt5514_codecs[1].of_node) {
208                 dev_err(&pdev->dev,
209                         "Property 'audio-codec' missing or invalid\n");
210                 ret = -EINVAL;
211                 goto out;
212         }
213         mt8173_rt5650_rt5514_codec_conf[0].of_node =
214                 mt8173_rt5650_rt5514_codecs[1].of_node;
215
216         card->dev = &pdev->dev;
217
218         ret = devm_snd_soc_register_card(&pdev->dev, card);
219         if (ret)
220                 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
221                         __func__, ret);
222
223 out:
224         of_node_put(platform_node);
225         return ret;
226 }
227
228 static const struct of_device_id mt8173_rt5650_rt5514_dt_match[] = {
229         { .compatible = "mediatek,mt8173-rt5650-rt5514", },
230         { }
231 };
232 MODULE_DEVICE_TABLE(of, mt8173_rt5650_rt5514_dt_match);
233
234 static struct platform_driver mt8173_rt5650_rt5514_driver = {
235         .driver = {
236                    .name = "mtk-rt5650-rt5514",
237                    .of_match_table = mt8173_rt5650_rt5514_dt_match,
238 #ifdef CONFIG_PM
239                    .pm = &snd_soc_pm_ops,
240 #endif
241         },
242         .probe = mt8173_rt5650_rt5514_dev_probe,
243 };
244
245 module_platform_driver(mt8173_rt5650_rt5514_driver);
246
247 /* Module information */
248 MODULE_DESCRIPTION("MT8173 RT5650 and RT5514 SoC machine driver");
249 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
250 MODULE_LICENSE("GPL v2");
251 MODULE_ALIAS("platform:mtk-rt5650-rt5514");
252