2 * rk29_rt3261.c -- SoC audio for rockchip
4 * Driver for rockchip rt3261 audio
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
22 #include <linux/of_gpio.h>
24 #include "../codecs/rt3261.h"
25 #include "card_info.h"
30 #define DBG(x...) printk(KERN_INFO x)
35 static int rockchip_rt3261_hifi_hw_params(struct snd_pcm_substream *substream,
36 struct snd_pcm_hw_params *params)
38 struct snd_soc_pcm_runtime *rtd = substream->private_data;
39 struct snd_soc_dai *codec_dai = rtd->codec_dai;
40 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
41 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
44 DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
46 /* set codec DAI configuration */
47 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
49 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
53 /* set cpu DAI configuration */
54 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
56 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
60 switch(params_rate(params)) {
74 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
79 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
81 /*Set the system clk for codec*/
82 snd_soc_dai_set_pll(codec_dai, 0, RT3261_PLL1_S_MCLK, pll_out, pll_out*2); //bard 8-29
83 ret = snd_soc_dai_set_sysclk(codec_dai, RT3261_SCLK_S_PLL1, pll_out*2, SND_SOC_CLOCK_IN); //bard 8-29
86 DBG("rk29_hw_params_rt3261:failed to set the sysclk for codec side\n");
90 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
91 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
92 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);// 256k = 48-1 3M=3
94 DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
99 static int rockchip_rt3261_voice_hw_params(struct snd_pcm_substream *substream,
100 struct snd_pcm_hw_params *params)
102 struct snd_soc_pcm_runtime *rtd = substream->private_data;
103 struct snd_soc_dai *codec_dai = rtd->codec_dai;
104 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
105 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
108 DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
110 /* set codec DAI configuration */
111 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
113 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
117 switch(params_rate(params)) {
131 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
136 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
138 /*Set the system clk for codec*/
139 snd_soc_dai_set_pll(codec_dai, 0, RT3261_PLL1_S_MCLK, pll_out, pll_out*2); //bard 8-29
140 ret = snd_soc_dai_set_sysclk(codec_dai, RT3261_SCLK_S_PLL1, pll_out*2, SND_SOC_CLOCK_IN); //bard 8-29
144 printk("rk29_hw_params_rt3261:failed to set the sysclk for codec side\n");
148 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
149 //snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
150 //snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
152 DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
157 static const struct snd_soc_dapm_widget rockchip_rt3261_dapm_widgets[] = {
158 SND_SOC_DAPM_MIC("Mic Jack", NULL),
159 SND_SOC_DAPM_MIC("Headset Jack", NULL),
160 SND_SOC_DAPM_SPK("Ext Spk", NULL),
161 SND_SOC_DAPM_HP("Headphone Jack", NULL),
164 static const struct snd_soc_dapm_route audio_map[]={
166 /* Mic Jack --> MIC_IN*/
167 {"micbias1", NULL, "Mic Jack"},
168 {"MIC1", NULL, "micbias1"},
171 {"micbias1", NULL, "Headset Jack"},
172 {"MIC3", NULL, "micbias1"},
174 {"Ext Spk", NULL, "SPOLP"},
175 {"Ext Spk", NULL, "SPOLN"},
176 {"Ext Spk", NULL, "SPORP"},
177 {"Ext Spk", NULL, "SPORN"},
179 {"Headphone Jack", NULL, "HPOL"},
180 {"Headphone Jack", NULL, "HPOR"},
183 static const struct snd_kcontrol_new rockchip_rt3261_controls[] = {
184 SOC_DAPM_PIN_SWITCH("Mic Jack"),
185 SOC_DAPM_PIN_SWITCH("Headset Jack"),
186 SOC_DAPM_PIN_SWITCH("Ext Spk"),
187 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
191 * Logic for a rt3261 as connected on a rockchip board.
193 static int rockchip_rt3261_init(struct snd_soc_pcm_runtime *rtd)
195 struct snd_soc_codec *codec = rtd->codec;
196 struct snd_soc_dapm_context *dapm = &codec->dapm;
198 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
200 mutex_lock(&dapm->card->dapm_mutex);
202 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
203 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
204 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
205 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
207 mutex_unlock(&dapm->card->dapm_mutex);
209 snd_soc_dapm_sync(dapm);
214 static struct snd_soc_ops rockchip_rt3261_hifi_ops = {
215 .hw_params = rockchip_rt3261_hifi_hw_params,
218 static struct snd_soc_ops rockchip_rt3261_voice_ops = {
219 .hw_params = rockchip_rt3261_voice_hw_params,
222 static struct snd_soc_dai_link rockchip_rt3261_dai[] = {
224 .name = "RT3261 I2S1",
225 .stream_name = "RT3261 PCM1",
226 .codec_dai_name = "rt3261-aif1",
227 .init = rockchip_rt3261_init,
228 .ops = &rockchip_rt3261_hifi_ops,
231 .name = "RT3261 I2S2",
232 .stream_name = "RT3261 PCM2",
233 .codec_dai_name = "rt3261-aif2",
234 .ops = &rockchip_rt3261_voice_ops,
238 static struct snd_soc_card rockchip_rt3261_snd_card = {
239 #if defined (CONFIG_SND_SOC_RT3224)
244 .owner = THIS_MODULE,
245 .dai_link = rockchip_rt3261_dai,
246 .num_links = ARRAY_SIZE(rockchip_rt3261_dai),
247 .controls = rockchip_rt3261_controls,
248 .num_controls = ARRAY_SIZE(rockchip_rt3261_controls),
249 .dapm_widgets = rockchip_rt3261_dapm_widgets,
250 .num_dapm_widgets = ARRAY_SIZE(rockchip_rt3261_dapm_widgets),
251 .dapm_routes = audio_map,
252 .num_dapm_routes = ARRAY_SIZE(audio_map),
258 compatible = "rockchip-rt3261";
261 audio-codec = <&rt3261>;
262 i2s-controller = <&i2s0>;
265 //bitclock-inversion;
272 audio-codec = <&rt3261>;
273 i2s-controller = <&i2s0>;
284 static int rockchip_rt3261_audio_probe(struct platform_device *pdev)
287 struct snd_soc_card *card = &rockchip_rt3261_snd_card;
289 card->dev = &pdev->dev;
291 ret = rockchip_of_get_sound_card_info(card);
293 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
297 ret = snd_soc_register_card(card);
299 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
304 static int rockchip_rt3261_audio_remove(struct platform_device *pdev)
306 struct snd_soc_card *card = platform_get_drvdata(pdev);
308 snd_soc_unregister_card(card);
314 static const struct of_device_id rockchip_rt3261_of_match[] = {
315 { .compatible = "rockchip-rt3261", },
318 MODULE_DEVICE_TABLE(of, rockchip_rt3261_of_match);
319 #endif /* CONFIG_OF */
321 static struct platform_driver rockchip_rt3261_audio_driver = {
323 .name = "rockchip-rt3261",
324 .owner = THIS_MODULE,
325 .pm = &snd_soc_pm_ops,
326 .of_match_table = of_match_ptr(rockchip_rt3261_of_match),
328 .probe = rockchip_rt3261_audio_probe,
329 .remove = rockchip_rt3261_audio_remove,
332 module_platform_driver(rockchip_rt3261_audio_driver);
334 /* Module information */
335 MODULE_AUTHOR("rockchip");
336 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
337 MODULE_LICENSE("GPL");