xref: /OK3568_Linux_fs/kernel/sound/soc/rockchip/rockchip_rt5651.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * rockchip_rt5651.c  --  RK3399 machine driver with RT5651 codecs
3  *
4  * Copyright (c) 2016, ROCKCHIP CORPORATION.  All rights reserved.
5  * Author: Xiaotan Luo <lxt@rock-chips.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 <sound/soc.h>
19 
20 #include "rockchip_i2s.h"
21 #include "../codecs/rt5651.h"
22 
23 #define DRV_NAME "rockchip-rt5651"
24 
25 static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
26 	SND_SOC_DAPM_HP("Headphones", NULL),
27 	SND_SOC_DAPM_SPK("Lineout", NULL),
28 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
29 	SND_SOC_DAPM_MIC("Int Mic", NULL),
30 	SND_SOC_DAPM_MIC("HDMIIN", NULL),
31 };
32 
33 static const struct snd_soc_dapm_route rockchip_dapm_routes[] = {
34 	{"Headphones", NULL, "HPOL"},
35 	{"Headphones", NULL, "HPOR"},
36 	{"Lineout", NULL, "LOUTL"},
37 	{"Lineout", NULL, "LOUTR"},
38 	{"AIF2 Playback", NULL, "HDMIIN"},
39 };
40 
41 static const struct snd_kcontrol_new rockchip_controls[] = {
42 	SOC_DAPM_PIN_SWITCH("Headphones"),
43 	SOC_DAPM_PIN_SWITCH("Lineout"),
44 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
45 	SOC_DAPM_PIN_SWITCH("Int Mic"),
46 };
47 
rockchip_rt5651_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)48 static int rockchip_rt5651_hw_params(struct snd_pcm_substream *substream,
49 				     struct snd_pcm_hw_params *params)
50 {
51 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
52 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
53 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
54 	int mclk, ret;
55 
56 	/* in bypass mode, the mclk has to be one of the frequencies below */
57 	switch (params_rate(params)) {
58 	case 8000:
59 	case 16000:
60 	case 24000:
61 	case 32000:
62 	case 48000:
63 	case 64000:
64 	case 96000:
65 		mclk = 12288000;
66 		break;
67 	case 11025:
68 	case 22050:
69 	case 44100:
70 	case 88200:
71 		mclk = 11289600;
72 		break;
73 	default:
74 		return -EINVAL;
75 	}
76 
77 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT);
78 	if (ret < 0) {
79 		dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
80 		return ret;
81 	}
82 
83 	snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_MCLK, mclk, mclk * 2);
84 
85 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, mclk * 2,
86 				     SND_SOC_CLOCK_IN);
87 	if (ret < 0) {
88 		dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
rockchip_rt5651_voice_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)95 static int rockchip_rt5651_voice_hw_params(struct snd_pcm_substream *substream,
96 					   struct snd_pcm_hw_params *params)
97 {
98 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
99 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
100 	int mclk, ret;
101 
102 	/* in bypass mode, the mclk has to be one of the frequencies below */
103 	switch (params_rate(params)) {
104 	case 8000:
105 	case 16000:
106 	case 24000:
107 	case 32000:
108 	case 48000:
109 	case 64000:
110 	case 96000:
111 		mclk = 12288000;
112 		break;
113 	case 11025:
114 	case 22050:
115 	case 44100:
116 	case 88200:
117 		mclk = 11289600;
118 		break;
119 	default:
120 		return -EINVAL;
121 	}
122 
123 	/*Set the system clk for codec*/
124 	snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_MCLK, mclk, 24576000);
125 
126 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, 24576000,
127 				     SND_SOC_CLOCK_IN);
128 	if (ret < 0) {
129 		dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
130 		return ret;
131 	}
132 	return 0;
133 }
134 
135 static struct snd_soc_ops rockchip_sound_rt5651_hifi_ops = {
136 	.hw_params = rockchip_rt5651_hw_params,
137 };
138 
139 static struct snd_soc_ops rockchip_sound_rt5651_voice_ops = {
140 	.hw_params = rockchip_rt5651_voice_hw_params,
141 };
142 
143 enum {
144 	DAILINK_RT5651_HIFI,
145 	DAILINK_RT5651_VOICE,
146 	DAILINK_RT5651_MAX
147 };
148 
149 static struct snd_soc_dai_link rockchip_dailinks[] = {
150 	[DAILINK_RT5651_HIFI] = {
151 		.name = "RT5651 HIFI",
152 		.stream_name = "RT5651 PCM",
153 		.codec_dai_name = "rt5651-aif1",
154 		.ops = &rockchip_sound_rt5651_hifi_ops,
155 		/* set rt5651 as slave */
156 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
157 			SND_SOC_DAIFMT_CBS_CFS,
158 	},
159 	[DAILINK_RT5651_VOICE] = {
160 		.name = "RT5651 HDMIIN",
161 		.stream_name = "RT5651 PCM",
162 		.codec_dai_name = "rt5651-aif2",
163 		.ops = &rockchip_sound_rt5651_voice_ops,
164 		/* set rt5651 as slave */
165 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
166 			SND_SOC_DAIFMT_CBS_CFS,
167 	},
168 };
169 
170 static struct snd_soc_card rockchip_sound_card = {
171 	.name = "realtekrt5651codec_hdmiin",
172 	.owner = THIS_MODULE,
173 	.dai_link = rockchip_dailinks,
174 	.num_links =  ARRAY_SIZE(rockchip_dailinks),
175 	.dapm_widgets = rockchip_dapm_widgets,
176 	.num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
177 	.dapm_routes = rockchip_dapm_routes,
178 	.num_dapm_routes = ARRAY_SIZE(rockchip_dapm_routes),
179 	.controls = rockchip_controls,
180 	.num_controls = ARRAY_SIZE(rockchip_controls),
181 };
182 
rockchip_sound_probe(struct platform_device * pdev)183 static int rockchip_sound_probe(struct platform_device *pdev)
184 {
185 	struct snd_soc_card *card = &rockchip_sound_card;
186 	struct device_node *cpu_node;
187 	int i, ret;
188 
189 	dev_info(&pdev->dev, "%s\n", __func__);
190 
191 	cpu_node = of_parse_phandle(pdev->dev.of_node, "rockchip,cpu", 0);
192 	if (!cpu_node) {
193 		dev_err(&pdev->dev,
194 			"Property 'rockchip,cpu' failed\n");
195 		return -EINVAL;
196 	}
197 
198 	for (i = 0; i < DAILINK_RT5651_MAX; i++) {
199 		rockchip_dailinks[i].platform_of_node = cpu_node;
200 		rockchip_dailinks[i].cpu_of_node = cpu_node;
201 
202 		rockchip_dailinks[i].codec_of_node =
203 			of_parse_phandle(pdev->dev.of_node,
204 					 "rockchip,codec", i);
205 		if (!rockchip_dailinks[i].codec_of_node) {
206 			dev_err(&pdev->dev,
207 				"Property[%d] 'rockchip,codec' failed\n", i);
208 			return -EINVAL;
209 		}
210 	}
211 
212 	card->dev = &pdev->dev;
213 	platform_set_drvdata(pdev, card);
214 	ret = devm_snd_soc_register_card(&pdev->dev, card);
215 	if (ret)
216 		dev_err(&pdev->dev, "%s register card failed %d\n",
217 			__func__, ret);
218 
219 	dev_info(&pdev->dev, "snd_soc_register_card successful\n");
220 	return ret;
221 }
222 
223 static const struct of_device_id rockchip_sound_of_match[] = {
224 	{ .compatible = "rockchip,rockchip-rt5651-sound", },
225 	{},
226 };
227 
228 static struct platform_driver rockchip_sound_driver = {
229 	.probe = rockchip_sound_probe,
230 	.driver = {
231 		.name = DRV_NAME,
232 		.of_match_table = rockchip_sound_of_match,
233 #ifdef CONFIG_PM
234 		.pm = &snd_soc_pm_ops,
235 #endif
236 	},
237 };
238 
239 module_platform_driver(rockchip_sound_driver);
240 
241 MODULE_AUTHOR("Xiaotan Luo <lxt@rock-chips.com>");
242 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
243 MODULE_LICENSE("GPL v2");
244 MODULE_ALIAS("platform:" DRV_NAME);
245 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
246