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