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