2 * rk_rk616.c -- SoC audio for rockchip
4 * Driver for rockchip rk616 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>
17 #include <linux/of_gpio.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
23 #include "../codecs/rk616_codec.h"
24 #include "card_info.h"
29 #define DBG(x...) pr_info(x)
34 static bool get_hdmi_state(void)
41 #ifdef CONFIG_HDMI_RK30
43 if (hdmi_get_hotplug() == 2)
50 static const struct snd_soc_dapm_widget rk_rk616_dapm_widgets[] = {
51 SND_SOC_DAPM_MIC("Mic Jack", NULL),
52 SND_SOC_DAPM_MIC("Headset Jack", NULL),
53 SND_SOC_DAPM_SPK("Ext Spk", NULL),
54 SND_SOC_DAPM_HP("Headphone Jack", NULL),
57 static const struct snd_soc_dapm_route rk_rk616_audio_map[] = {
59 /* Mic Jack --> MIC_IN*/
60 {"Mic1 Bias", NULL, "Mic Jack"},
61 {"MIC1P", NULL, "Mic1 Bias"},
62 {"MIC1N", NULL, "Mic1 Bias"},
65 {"Mic2 Bias", NULL, "Headset Jack"},
66 {"MIC2P", NULL, "Mic2 Bias"},
67 {"MIC2N", NULL, "Mic2 Bias"},
69 {"Ext Spk", NULL, "SPKOUTR"},
70 {"Ext Spk", NULL, "SPKOUTL"},
72 {"Headphone Jack", NULL, "HPOUTR"},
73 {"Headphone Jack", NULL, "HPOUTL"},
76 static const struct snd_kcontrol_new rk_rk616_controls[] = {
77 SOC_DAPM_PIN_SWITCH("Mic Jack"),
78 SOC_DAPM_PIN_SWITCH("Headset Jack"),
79 SOC_DAPM_PIN_SWITCH("Ext Spk"),
80 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
83 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
85 struct snd_soc_codec *codec = rtd->codec;
86 struct snd_soc_dapm_context *dapm = &codec->dapm;
89 * if is for mid that using tiny alsa,
90 * it don't need this controls and route, so return.
92 if (rk616_get_for_mid())
95 DBG("%s() %d\n", __func__, __LINE__);
97 mutex_lock(&dapm->card->dapm_mutex);
99 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
100 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
101 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
102 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
104 mutex_unlock(&dapm->card->dapm_mutex);
106 snd_soc_dapm_sync(dapm);
111 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
112 struct snd_pcm_hw_params *params)
114 struct snd_soc_pcm_runtime *rtd = substream->private_data;
115 struct snd_soc_dai *codec_dai = rtd->codec_dai;
116 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
117 unsigned int pll_out = 0, div = 4, dai_fmt = rtd->dai_link->dai_fmt;
120 DBG("%s() %d\n", __func__, __LINE__);
122 /* set codec DAI configuration */
123 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
125 pr_err("%s():failed to set the format for codec side\n",
130 /* set cpu DAI configuration */
131 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
133 pr_err("%s():failed to set the format for cpu side\n",
138 switch (params_rate(params)) {
155 DBG("Enter:%s, %d, Error rate=%d\n",
157 params_rate(params));
162 DBG("Enter:%s, %d, rate=%d\n",
164 params_rate(params));
166 #if defined(CONFIG_RK616_USE_MCLK_12M)
167 /* MCLK must be 12M when RK616 HDMI is in */
168 if (get_hdmi_state() && pll_out != 12000000) {
169 DBG("%s : HDMI is in, don't set sys clk %u\n",
175 /* Set the system clk for codec
176 mclk will be setted in set_sysclk of codec_dai*/
177 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
178 pll_out, SND_SOC_CLOCK_IN);
180 DBG("%s : failed to set the sysclk for codec side\n",
184 #if defined(CONFIG_RK616_USE_MCLK_12M)
187 snd_soc_dai_set_clkdiv(cpu_dai,
189 (pll_out / div)/params_rate(params)-1);
190 snd_soc_dai_set_clkdiv(cpu_dai,
191 ROCKCHIP_DIV_MCLK, div - 1);
193 DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d\n",
194 __func__, __LINE__, (pll_out/div)/params_rate(params));
199 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
200 struct snd_pcm_hw_params *params)
202 struct snd_soc_pcm_runtime *rtd = substream->private_data;
203 struct snd_soc_dai *codec_dai = rtd->codec_dai;
204 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
205 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
208 DBG("%s() %d\n", __func__, __LINE__);
210 /* set codec DAI configuration */
211 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
213 pr_err("rk_voice_hw_params:failed to set the format for codec side\n");
217 switch (params_rate(params)) {
231 DBG("Enter:%s, %d, Error rate=%d\n",
233 params_rate(params));
238 /* MCLK must be 12M when RK616 HDMI is in */
239 #if defined(CONFIG_RK616_USE_MCLK_12M)
240 if (get_hdmi_state() && pll_out != 12000000) {
241 DBG("%s : HDMI is in, set mclk to 12Mn", __func__);
246 /*Set the system clk for codec*/
247 ret = snd_soc_dai_set_sysclk(codec_dai, 0,
248 pll_out, SND_SOC_CLOCK_IN);
250 pr_err("rk_voice_hw_params:failed to set the sysclk for codec side\n");
254 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
256 pr_err("rk_voice_hw_params:failed to set the sysclk for cpu side\n");
263 static struct snd_soc_ops rk616_hifi_ops = {
264 .hw_params = rk_hifi_hw_params,
267 static struct snd_soc_ops rk616_voice_ops = {
268 .hw_params = rk_voice_hw_params,
271 static struct snd_soc_dai_link rk_dai[] = {
273 .name = "RK616 I2S1",
274 .stream_name = "RK616 PCM",
275 .codec_dai_name = "rk616-hifi",
277 .ops = &rk616_hifi_ops,
280 .name = "RK616 I2S2",
281 .stream_name = "RK616 PCM",
282 .codec_dai_name = "rk616-voice",
283 .ops = &rk616_voice_ops,
287 static struct snd_soc_card rockchip_rk616_snd_card = {
291 .controls = rk_rk616_controls,
292 .num_controls = ARRAY_SIZE(rk_rk616_controls),
293 .dapm_widgets = rk_rk616_dapm_widgets,
294 .num_dapm_widgets = ARRAY_SIZE(rk_rk616_dapm_widgets),
295 .dapm_routes = rk_rk616_audio_map,
296 .num_dapm_routes = ARRAY_SIZE(rk_rk616_audio_map),
301 * compatible = "rockchip-rk616";
304 * audio-codec = <&rk616>;
305 * i2s-controller = <&i2s0>;
307 * //continuous-clock;
308 * //bitclock-inversion;
315 * audio-codec = <&rk616>;
316 * i2s-controller = <&i2s0>;
318 * //continuous-clock;
319 * bitclock-inversion;
327 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
330 struct snd_soc_card *card = &rockchip_rk616_snd_card;
332 card->dev = &pdev->dev;
334 ret = rockchip_of_get_sound_card_info(card);
336 pr_err("%s() get sound card info failed:%d\n",
341 ret = snd_soc_register_card(card);
343 pr_err("%s() register card failed:%d\n",
349 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
351 struct snd_soc_card *card = platform_get_drvdata(pdev);
353 snd_soc_unregister_card(card);
359 static const struct of_device_id rockchip_rk616_of_match[] = {
360 { .compatible = "rockchip-rk616", },
363 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
364 #endif /* CONFIG_OF */
366 static struct platform_driver rockchip_rk616_audio_driver = {
368 .name = "rockchip-rk616",
369 .owner = THIS_MODULE,
370 .pm = &snd_soc_pm_ops,
371 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
373 .probe = rockchip_rk616_audio_probe,
374 .remove = rockchip_rk616_audio_remove,
377 module_platform_driver(rockchip_rk616_audio_driver);
379 /* Module information */
380 MODULE_AUTHOR("rockchip");
381 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
382 MODULE_LICENSE("GPL");