rk312x-codec : Closed the debug print switch
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_rk312x.c
1 /*
2  * rk_rk312x.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rk312x audio
5  *
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.
10  *
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/of.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>
22
23 #include "../codecs/rk312x_codec.h"
24 #include "card_info.h"
25 #include "rk_pcm.h"
26 #include "rk_i2s.h"
27
28 #if 0
29 #define DBG(x...)       pr_info("rk_rk312x" x)
30 #else
31 #define DBG(x...)
32 #endif
33
34 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
35         SND_SOC_DAPM_MIC("Mic Jack", NULL),
36         SND_SOC_DAPM_MIC("Headset Jack", NULL),
37         SND_SOC_DAPM_SPK("Ext Spk", NULL),
38         SND_SOC_DAPM_HP("Headphone Jack", NULL),
39 };
40
41 static const struct snd_soc_dapm_route rk_audio_map[] = {
42         /* Mic Jack --> MIC_IN*/
43         {"Mic Bias", NULL, "Mic Jack"},
44         {"MICP", NULL, "Mic Bias"},
45         {"MICN", NULL, "Mic Bias"},
46
47         /* HP MIC */
48         {"Mic Bias", NULL, "Headset Jack"},
49
50         {"Ext Spk", NULL, "HPOUTR"},
51         {"Ext Spk", NULL, "HPOUTL"},
52
53         {"Headphone Jack", NULL, "HPOUTR"},
54         {"Headphone Jack", NULL, "HPOUTL"},
55 };
56
57 static const struct snd_kcontrol_new rk_controls[] = {
58         SOC_DAPM_PIN_SWITCH("Mic Jack"),
59         SOC_DAPM_PIN_SWITCH("Headset Jack"),
60         SOC_DAPM_PIN_SWITCH("Ext Spk"),
61         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
62 };
63
64 static int rk312x_init(struct snd_soc_pcm_runtime *rtd)
65 {
66         struct snd_soc_codec *codec = rtd->codec;
67         struct snd_soc_dapm_context *dapm = &codec->dapm;
68
69         DBG("Enter::%s----%d\n", __func__, __LINE__);
70
71         mutex_lock(&dapm->card->dapm_mutex);
72
73         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
74         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
75         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
76         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
77
78         mutex_unlock(&dapm->card->dapm_mutex);
79
80         snd_soc_dapm_sync(dapm);
81
82         return 0;
83 }
84
85 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
86                              struct snd_pcm_hw_params *params)
87 {
88         struct snd_soc_pcm_runtime *rtd = substream->private_data;
89         struct snd_soc_dai *codec_dai = rtd->codec_dai;
90         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
91         unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
92         int ret;
93
94         DBG("Enter::%s----%d\n", __func__, __LINE__);
95
96         /* set codec DAI configuration */
97         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
98         if (ret < 0) {
99                 DBG("%s():failed to set the format for codec side\n",
100                     __func__);
101                 return ret;
102         }
103
104         /* set cpu DAI configuration */
105         ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
106         if (ret < 0) {
107                 DBG("%s():failed to set the format for cpu side\n",
108                     __func__);
109                 return ret;
110         }
111
112         switch (params_rate(params)) {
113         case 8000:
114         case 16000:
115         case 24000:
116         case 32000:
117         case 48000:
118                 pll_out = 12288000;
119                 break;
120         case 11025:
121         case 22050:
122         case 44100:
123                 pll_out = 11289600;
124                 break;
125         default:
126                 DBG("Enter:%s, %d, Error rate=%d\n",
127                     __func__, __LINE__, params_rate(params));
128                 return -EINVAL;
129                 break;
130         }
131
132         DBG("Enter:%s, %d, rate=%d\n",
133             __func__, __LINE__, params_rate(params));
134
135         /*Set the system clk for codec*/
136         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
137         if (ret < 0) {
138                 DBG("rk_hifi_hw_params:failed to set the sysclk for codec\n");
139                 return ret;
140         }
141
142         snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
143         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK,
144                                (pll_out/4)/params_rate(params)-1);
145         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
146         DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n",
147             __func__, __LINE__, (pll_out/4)/params_rate(params));
148
149         return 0;
150 }
151
152 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
153                               struct snd_pcm_hw_params *params)
154 {
155         struct snd_soc_pcm_runtime *rtd = substream->private_data;
156         struct snd_soc_dai *codec_dai = rtd->codec_dai;
157         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158         unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
159         int ret;
160
161         DBG("Enter::%s----%d\n", __func__, __LINE__);
162
163         /* set codec DAI configuration */
164         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
165         if (ret < 0) {
166                 DBG("%s():failed to set the format for codec side\n",
167                     __func__);
168                 return ret;
169         }
170
171         /* set codec DAI configuration */
172         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
173                                 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS);
174
175         switch (params_rate(params)) {
176         case 8000:
177         case 16000:
178         case 24000:
179         case 32000:
180         case 48000:
181                 pll_out = 12288000;
182                 break;
183         case 11025:
184         case 22050:
185         case 44100:
186                 pll_out = 11289600;
187                 break;
188         default:
189                 DBG("Enter:%s, %d, Error rate=%d\n",
190                     __func__, __LINE__,
191                 params_rate(params));
192                 return -EINVAL;
193                 break;
194         }
195
196         /*Set the system clk for codec*/
197         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
198
199         if (ret < 0) {
200                 DBG("rk_voice_hw_params:failed to set codec side\n");
201                 return ret;
202         }
203
204         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
205
206         return 0;
207 }
208
209 static struct snd_soc_ops rk312x_hifi_ops = {
210         .hw_params = rk_hifi_hw_params,
211 };
212
213 static struct snd_soc_ops rk312x_voice_ops = {
214         .hw_params = rk_voice_hw_params,
215 };
216
217 static struct snd_soc_dai_link rk_dai[] = {
218         {
219                 .name = "RK312X I2S1",
220                 .stream_name = "RK312X PCM",
221                 .codec_dai_name = "rk312x-hifi",
222                 .init = rk312x_init,
223                 .ops = &rk312x_hifi_ops,
224         },
225         {
226                 .name = "RK312X I2S2",
227                 .stream_name = "RK312X PCM",
228                 .codec_dai_name = "rk312x-voice",
229                 .ops = &rk312x_voice_ops,
230         },
231 };
232
233 static struct snd_soc_card rockchip_rk312x_snd_card = {
234         .name = "RK_RK312X",
235         .dai_link = rk_dai,
236         .num_links = 2,
237         .controls = rk_controls,
238         .num_controls = ARRAY_SIZE(rk_controls),
239         .dapm_widgets    = rk_dapm_widgets,
240         .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
241         .dapm_routes    = rk_audio_map,
242         .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
243 };
244
245 static int rockchip_rk312x_audio_probe(struct platform_device *pdev)
246 {
247         int ret;
248         struct snd_soc_card *card = &rockchip_rk312x_snd_card;
249
250         card->dev = &pdev->dev;
251         ret = rockchip_of_get_sound_card_info(card);
252         if (ret) {
253                 DBG("%s() get sound card info failed:%d\n", __func__, ret);
254                 return ret;
255         }
256
257         ret = snd_soc_register_card(card);
258         if (ret)
259                 DBG("%s() register card failed:%d\n", __func__, ret);
260
261         return ret;
262 }
263
264 static int rockchip_rk312x_audio_remove(struct platform_device *pdev)
265 {
266         struct snd_soc_card *card = platform_get_drvdata(pdev);
267
268         snd_soc_unregister_card(card);
269
270         return 0;
271 }
272
273 #ifdef CONFIG_OF
274 static const struct of_device_id rockchip_rk312x_of_match[] = {
275         { .compatible = "audio-rk312x", },
276         {},
277 };
278 MODULE_DEVICE_TABLE(of, rockchip_rk312x_of_match);
279 #endif /* CONFIG_OF */
280
281 static struct platform_driver rockchip_rk312x_audio_driver = {
282         .driver         = {
283                 .name   = "audio-rk312x",
284                 .owner  = THIS_MODULE,
285                 .pm = &snd_soc_pm_ops,
286                 .of_match_table = of_match_ptr(rockchip_rk312x_of_match),
287         },
288         .probe          = rockchip_rk312x_audio_probe,
289         .remove         = rockchip_rk312x_audio_remove,
290 };
291
292 module_platform_driver(rockchip_rk312x_audio_driver);
293
294 /* Module information */
295 MODULE_AUTHOR("rockchip");
296 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
297 MODULE_LICENSE("GPL");