Change path of audio platform device from rk to rockchip
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_rk3026.c
1 /*
2  * rk_rk3026.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rk3026 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 <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
20 #include <asm/io.h>
21 #include <mach/hardware.h>
22 #include "../codecs/rk3026_codec.h"
23 #include "rk_pcm.h"
24 #include "rk29_i2s.h"
25
26 #if 0
27 #define DBG(x...)       printk(KERN_INFO x)
28 #else
29 #define DBG(x...)
30 #endif
31
32 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
33         SND_SOC_DAPM_MIC("Mic Jack", NULL),
34         SND_SOC_DAPM_MIC("Headset Jack", NULL),
35         SND_SOC_DAPM_SPK("Ext Spk", NULL),
36         SND_SOC_DAPM_HP("Headphone Jack", NULL),
37 };
38
39 static const struct snd_soc_dapm_route rk_audio_map[]={
40
41         /* Mic Jack --> MIC_IN*/
42         {"Mic Bias", NULL, "Mic Jack"},
43         {"MICP", NULL, "Mic Bias"},
44         {"MICN", NULL, "Mic Bias"},
45
46         // HP MIC
47         {"Mic Bias", NULL, "Headset Jack"},
48
49         {"Ext Spk", NULL, "HPOUTR"},
50         {"Ext Spk", NULL, "HPOUTL"},
51
52         {"Headphone Jack", NULL, "HPOUTR"},
53         {"Headphone Jack", NULL, "HPOUTL"},
54 } ;
55
56 static const struct snd_kcontrol_new rk_controls[] = {
57         SOC_DAPM_PIN_SWITCH("Mic Jack"),
58         SOC_DAPM_PIN_SWITCH("Headset Jack"),
59         SOC_DAPM_PIN_SWITCH("Ext Spk"),
60         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
61 };
62
63 static int rk3026_init(struct snd_soc_pcm_runtime *rtd)
64 {
65         struct snd_soc_codec *codec = rtd->codec;
66         struct snd_soc_dapm_context *dapm = &codec->dapm;
67
68         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
69
70         snd_soc_add_codec_controls(codec, rk_controls,
71                         ARRAY_SIZE(rk_controls));
72
73         /* Add specific widgets */
74         snd_soc_dapm_new_controls(dapm, rk_dapm_widgets,
75                                   ARRAY_SIZE(rk_dapm_widgets));
76         /* Set up specific audio path audio_mapnects */
77         snd_soc_dapm_add_routes(dapm, rk_audio_map, ARRAY_SIZE(rk_audio_map));
78
79         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
80         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
81         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
82         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
83
84         snd_soc_dapm_sync(dapm);
85
86         return 0;
87 }
88
89 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
90         struct snd_pcm_hw_params *params)
91 {
92         struct snd_soc_pcm_runtime *rtd = substream->private_data;
93         struct snd_soc_dai *codec_dai = rtd->codec_dai;
94         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
95         unsigned int pll_out = 0;
96         int ret;
97
98         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
99
100         /* set codec DAI configuration */
101         #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
102
103         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
104                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
105         #endif
106         #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
107
108         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
109                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM );
110         #endif
111         if (ret < 0)
112                 return ret;
113
114         /* set cpu DAI configuration */
115         #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
116         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
117                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
118         #endif
119         #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
120         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
121                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
122         #endif
123         if (ret < 0)
124                 return ret;
125
126         switch(params_rate(params)) {
127                 case 8000:
128                 case 16000:
129                 case 24000:
130                 case 32000:
131                 case 48000:
132                         pll_out = 12288000;
133                         break;
134                 case 11025:
135                 case 22050:
136                 case 44100:
137                         pll_out = 11289600;
138                         break;
139                 default:
140                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
141                         return -EINVAL;
142                         break;
143         }
144
145         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
146
147         /*Set the system clk for codec*/
148         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
149         if (ret < 0) {
150                 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
151                 return ret;
152         }
153
154         snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
155         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
156         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
157
158         DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
159
160         return 0;
161 }
162
163 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
164         struct snd_pcm_hw_params *params)
165 {
166         struct snd_soc_pcm_runtime *rtd = substream->private_data;
167         struct snd_soc_dai *codec_dai = rtd->codec_dai;
168         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
169         unsigned int pll_out = 0;
170         int ret;
171
172         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
173
174         /* set codec DAI configuration */
175         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
176                                 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS);
177
178         switch(params_rate(params)) {
179                 case 8000:
180                 case 16000:
181                 case 24000:
182                 case 32000:
183                 case 48000:
184                         pll_out = 12288000;
185                         break;
186                 case 11025:
187                 case 22050:
188                 case 44100:
189                         pll_out = 11289600;
190                         break;
191                 default:
192                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
193                         return -EINVAL;
194                         break;
195         }
196
197         /*Set the system clk for codec*/
198         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
199
200         if (ret < 0) {
201                 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
202                 return ret;
203         }
204
205         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
206
207         return 0;
208 }
209
210 static struct snd_soc_ops rk3026_hifi_ops = {
211         .hw_params = rk_hifi_hw_params,
212 };
213
214 static struct snd_soc_ops rk3026_voice_ops = {
215         .hw_params = rk_voice_hw_params,
216 };
217
218 static struct snd_soc_dai_link rk_dai[] = {
219         {
220                 .name = "RK3026 I2S1",
221                 .stream_name = "RK3026 PCM",
222                 .codec_name = "rk3026-codec",
223                 .platform_name = "rockchip-audio",
224 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
225                 .cpu_dai_name = "rk_i2s.0",
226 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
227                 .cpu_dai_name = "rk_i2s.1",
228 #endif
229                 .codec_dai_name = "rk3026-hifi",
230                 .init = rk3026_init,
231                 .ops = &rk3026_hifi_ops,
232         },
233         {
234                 .name = "RK3026 I2S2",
235                 .stream_name = "RK3026 PCM",
236                 .codec_name = "rk3026-codec",
237                 .platform_name = "rockchip-audio",
238 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
239                 .cpu_dai_name = "rk_i2s.0",
240 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
241                 .cpu_dai_name = "rk_i2s.1",
242 #endif
243                 .codec_dai_name = "rk3026-voice",
244                 .ops = &rk3026_voice_ops,
245         },
246 };
247
248 static struct snd_soc_card snd_soc_card_rk = {
249         .name = "RK_RK3026",
250         .dai_link = rk_dai,
251         .num_links = 2,
252 };
253
254 static struct platform_device *rk_snd_device;
255
256 static int __init audio_card_init(void)
257 {
258         int ret =0;
259
260         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
261
262         rk_snd_device = platform_device_alloc("soc-audio", -1);
263         if (!rk_snd_device) {
264                   printk("platform device allocation failed\n");
265                   return -ENOMEM;
266         }
267
268         platform_set_drvdata(rk_snd_device, &snd_soc_card_rk);
269         ret = platform_device_add(rk_snd_device);
270         if (ret) {
271                 printk("platform device add failed\n");
272
273                 platform_device_put(rk_snd_device);
274                 return ret;
275         }
276
277         return ret;
278 }
279
280 static void __exit audio_card_exit(void)
281 {
282         platform_device_unregister(rk_snd_device);
283 }
284
285 module_init(audio_card_init);
286 module_exit(audio_card_exit);
287 /* Module information */
288 MODULE_AUTHOR("rockchip");
289 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
290 MODULE_LICENSE("GPL");