Audio: delete codec_set_spk, update rk616 codec driver
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_rk616.c
1 /*
2  * rk_rk616.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rk616 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/rk616_codec.h"
24 #include "card_info.h"
25 #include "rk_pcm.h"
26 #include "rk_i2s.h"
27
28 #if 1
29 #define DBG(x...)       printk(KERN_INFO x)
30 #else
31 #define DBG(x...)
32 #endif
33
34 #ifdef CONFIG_HDMI
35         extern int hdmi_is_insert(void);
36 #endif
37
38 #ifdef CONFIG_HDMI_RK30
39         extern int hdmi_get_hotplug(void);
40 #endif
41
42 static bool get_hdmi_state(void)
43 {
44 #ifdef CONFIG_HDMI
45         if(hdmi_is_insert())
46                 return true;
47 #endif
48
49 #ifdef CONFIG_HDMI_RK30
50         if(hdmi_get_hotplug() == 2/*HDMI_HPD_ACTIVED*/)
51                 return true;
52 #endif
53
54                         return false;
55 }
56
57 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
58         SND_SOC_DAPM_MIC("Mic Jack", NULL),
59         SND_SOC_DAPM_MIC("Headset Jack", NULL),
60         SND_SOC_DAPM_SPK("Ext Spk", NULL),
61         SND_SOC_DAPM_HP("Headphone Jack", NULL),
62 };
63
64 static const struct snd_soc_dapm_route rk_audio_map[]={
65
66         /* Mic Jack --> MIC_IN*/
67         {"Mic1 Bias", NULL, "Mic Jack"},
68         {"MIC1P", NULL, "Mic1 Bias"},
69         {"MIC1N", NULL, "Mic1 Bias"},
70
71         // HP MIC
72         {"Mic2 Bias", NULL, "Headset Jack"},
73         {"MIC2P", NULL, "Mic2 Bias"},
74         {"MIC2N", NULL, "Mic2 Bias"},
75
76         {"Ext Spk", NULL, "SPKOUTR"},
77         {"Ext Spk", NULL, "SPKOUTL"},
78
79         {"Headphone Jack", NULL, "HPOUTR"},
80         {"Headphone Jack", NULL, "HPOUTL"},
81 } ;
82
83 static const struct snd_kcontrol_new rk_controls[] = {
84         SOC_DAPM_PIN_SWITCH("Mic Jack"),
85         SOC_DAPM_PIN_SWITCH("Headset Jack"),
86         SOC_DAPM_PIN_SWITCH("Ext Spk"),
87         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
88 };
89
90 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
91 {
92         struct snd_soc_codec *codec = rtd->codec;
93         struct snd_soc_dapm_context *dapm = &codec->dapm;
94
95         // if is for mid that using tiny alsa, 
96         // it don't need this controls and route, so return.
97         if (rk616_get_for_mid())
98                 return 0;
99
100         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
101
102         snd_soc_add_codec_controls(codec, rk_controls,
103                         ARRAY_SIZE(rk_controls));
104
105         /* Add specific widgets */
106         snd_soc_dapm_new_controls(dapm, rk_dapm_widgets,
107                                   ARRAY_SIZE(rk_dapm_widgets));
108         /* Set up specific audio path audio_mapnects */
109         snd_soc_dapm_add_routes(dapm, rk_audio_map, ARRAY_SIZE(rk_audio_map));
110
111         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
112         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
113         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
114         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
115
116         snd_soc_dapm_sync(dapm);
117
118         return 0;
119 }
120
121 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
122         struct snd_pcm_hw_params *params)
123 {
124         struct snd_soc_pcm_runtime *rtd = substream->private_data;
125         struct snd_soc_dai *codec_dai = rtd->codec_dai;
126         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
127         unsigned int pll_out = 0, div = 4, dai_fmt = rtd->dai_link->dai_fmt;
128         int ret;
129
130         DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
131
132         /* set codec DAI configuration */
133         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
134         if (ret < 0) {
135                 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
136                 return ret;
137         }
138
139         /* set cpu DAI configuration */
140         ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
141         if (ret < 0) {
142                 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
143                 return ret;
144         }
145
146         switch(params_rate(params)) {
147                 case 16000:
148                 case 24000:
149                 case 32000:
150                 case 48000:
151                         pll_out = 12288000;
152                         break;
153                 case 11025:
154                 case 22050:
155                 case 44100:
156                         pll_out = 11289600;
157                         break;
158                 case 8000:
159                         pll_out = 12000000;
160                         div = 6;
161                         break;
162                 default:
163                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
164                         return -EINVAL;
165                         break;
166         }
167
168         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
169
170         #if defined(CONFIG_RK616_USE_MCLK_12M)
171         /* MCLK must be 12M when RK616 HDMI is in */
172         if (get_hdmi_state() && pll_out != 12000000) {
173                 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
174                 goto __setdiv;
175         }
176         #endif
177
178         /* Set the system clk for codec
179            mclk will be setted in set_sysclk of codec_dai*/
180         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
181         if (ret < 0) {
182                 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
183                 return ret;
184         }
185 #if defined(CONFIG_RK616_USE_MCLK_12M)
186 __setdiv:
187 #endif
188         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
189         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
190
191         DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
192
193         return 0;
194 }
195
196 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
197         struct snd_pcm_hw_params *params)
198 {
199         struct snd_soc_pcm_runtime *rtd = substream->private_data;
200         struct snd_soc_dai *codec_dai = rtd->codec_dai;
201         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
202         unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
203         int ret;
204
205         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
206
207         /* set codec DAI configuration */
208         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
209         if (ret < 0) {
210                 printk("rk_voice_hw_params:failed to set the format for codec side\n");
211                 return ret;
212         }
213
214         switch(params_rate(params)) {
215                 case 8000:
216                 case 16000:
217                 case 24000:
218                 case 32000:
219                 case 48000:
220                         pll_out = 12288000;
221                         break;
222                 case 11025:
223                 case 22050:
224                 case 44100:
225                         pll_out = 11289600;
226                         break;
227                 default:
228                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
229                         return -EINVAL;
230                         break;
231         }
232
233         /* MCLK must be 12M when RK616 HDMI is in */
234         #if defined(CONFIG_RK616_USE_MCLK_12M)
235         if (get_hdmi_state() && pll_out != 12000000) {
236                 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
237                 pll_out = 12000000;
238         }
239         #endif
240
241         //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
242
243         /*Set the system clk for codec*/
244         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
245         if (ret < 0) {
246                 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
247                 return ret;
248         }
249
250         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
251         if (ret < 0) {
252                 printk("rk_voice_hw_params:failed to set the sysclk for cpu side\n");
253                 return ret;
254         }
255
256         return 0;
257 }
258
259 static struct snd_soc_ops rk616_hifi_ops = {
260         .hw_params = rk_hifi_hw_params,
261 };
262
263 static struct snd_soc_ops rk616_voice_ops = {
264         .hw_params = rk_voice_hw_params,
265 };
266
267 static struct snd_soc_dai_link rk_dai[] = {
268         {
269                 .name = "RK616 I2S1",
270                 .stream_name = "RK616 PCM",
271                 .codec_dai_name = "rk616-hifi",
272                 .init = rk616_init,
273                 .ops = &rk616_hifi_ops,
274         },
275         {
276                 .name = "RK616 I2S2",
277                 .stream_name = "RK616 PCM",
278                 .codec_dai_name = "rk616-voice",
279                 .ops = &rk616_voice_ops,
280                 //.no_pcm = 1,
281         },
282 };
283
284 static struct snd_soc_card rockchip_rk616_snd_card = {
285         .name = "RK_RK616",
286         .dai_link = rk_dai,
287         .num_links = 2,
288 };
289 /*
290 dts:
291         rockchip-rk616 {
292                 compatible = "rockchip-rk616";
293                 dais {
294                         dai0 {
295                                 audio-codec = <&rk616>;
296                                 i2s-controller = <&i2s0>;
297                                 format = "i2s";
298                                 //continuous-clock;
299                                 //bitclock-inversion;
300                                 //frame-inversion;
301                                 //bitclock-master;
302                                 //frame-master;
303                         };
304
305                         dai1 {
306                                 audio-codec = <&rk616>;
307                                 i2s-controller = <&i2s0>;
308                                 format = "dsp_a";
309                                 //continuous-clock;
310                                 bitclock-inversion;
311                                 //frame-inversion;
312                                 //bitclock-master;
313                                 //frame-master;
314                         };
315                 };
316         };
317 */
318 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
319 {
320         int ret;
321         struct snd_soc_card *card = &rockchip_rk616_snd_card;
322
323         card->dev = &pdev->dev;
324
325         ret = rockchip_of_get_sound_card_info(card);
326         if (ret) {
327                 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
328                 return ret;
329         }
330
331         ret = snd_soc_register_card(card);
332         if (ret)
333                 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
334
335         return ret;
336 }
337
338 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
339 {
340         struct snd_soc_card *card = platform_get_drvdata(pdev);
341
342         snd_soc_unregister_card(card);
343
344         return 0;
345 }
346
347 #ifdef CONFIG_OF
348 static const struct of_device_id rockchip_rk616_of_match[] = {
349         { .compatible = "rockchip-rk616", },
350         {},
351 };
352 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
353 #endif /* CONFIG_OF */
354
355 static struct platform_driver rockchip_rk616_audio_driver = {
356         .driver         = {
357                 .name   = "rockchip-rk616",
358                 .owner  = THIS_MODULE,
359                 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
360         },
361         .probe          = rockchip_rk616_audio_probe,
362         .remove         = rockchip_rk616_audio_remove,
363 };
364
365 module_platform_driver(rockchip_rk616_audio_driver);
366
367 /* Module information */
368 MODULE_AUTHOR("rockchip");
369 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
370 MODULE_LICENSE("GPL");