ASoC: rockchip: add rt5651_tc358749x driver
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rockchip_rt5651_tc358749x.c
1 /*
2  * rockchip_rt5651_tc358749x.c  --  RK3399 machine driver with
3  * RT5651/TC358749 codecs
4  *
5  * Copyright (c) 2016, ROCKCHIP CORPORATION.  All rights reserved.
6  * Author: Xiaotan Luo <lxt@rock-chips.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 and
10  * only version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/module.h>
19 #include <sound/soc.h>
20
21 #include "rockchip_i2s.h"
22 #include "../codecs/rt5651.h"
23 #include "../codecs/tc358749x.h"
24
25 #define DRV_NAME "rk3399-rt5651-tc358749x"
26
27 static const struct snd_soc_dapm_widget rockchip_dapm_widgets[] = {
28         SND_SOC_DAPM_HP("Headphones", NULL),
29         SND_SOC_DAPM_SPK("Lineout", NULL),
30         SND_SOC_DAPM_MIC("Headset Mic", NULL),
31         SND_SOC_DAPM_MIC("Int Mic", NULL),
32 };
33
34 static const struct snd_soc_dapm_route rockchip_dapm_routes[] = {
35         {"Headphones", NULL, "HPOL"},
36         {"Headphones", NULL, "HPOR"},
37         {"Lineout", NULL, "LOUTL"},
38         {"Lineout", NULL, "LOUTR"},
39 };
40
41 static const struct snd_kcontrol_new rockchip_controls[] = {
42         SOC_DAPM_PIN_SWITCH("Headphones"),
43         SOC_DAPM_PIN_SWITCH("Lineout"),
44         SOC_DAPM_PIN_SWITCH("Headset Mic"),
45         SOC_DAPM_PIN_SWITCH("Int Mic"),
46 };
47
48 static int rockchip_rt5651_hw_params(struct snd_pcm_substream *substream,
49                                      struct snd_pcm_hw_params *params)
50 {
51         struct snd_soc_pcm_runtime *rtd = substream->private_data;
52         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
53         struct snd_soc_dai *codec_dai = rtd->codec_dai;
54         int mclk, ret;
55
56         /* in bypass mode, the mclk has to be one of the frequencies below */
57         switch (params_rate(params)) {
58         case 8000:
59         case 16000:
60         case 24000:
61         case 32000:
62         case 48000:
63         case 64000:
64         case 96000:
65                 mclk = 12288000;
66                 break;
67         case 11025:
68         case 22050:
69         case 44100:
70         case 88200:
71                 mclk = 11289600;
72                 break;
73         default:
74                 return -EINVAL;
75         }
76
77         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk, SND_SOC_CLOCK_OUT);
78         if (ret < 0) {
79                 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
80                 return ret;
81         }
82
83         ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
84         if (ret < 0) {
85                 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
86                 return ret;
87         }
88
89         return 0;
90 }
91
92 static int rockchip_rt5651_voice_hw_params(struct snd_pcm_substream *substream,
93                                            struct snd_pcm_hw_params *params)
94 {
95         struct snd_soc_pcm_runtime *rtd = substream->private_data;
96         struct snd_soc_dai *codec_dai = rtd->codec_dai;
97         int mclk, ret;
98
99         /* in bypass mode, the mclk has to be one of the frequencies below */
100         switch (params_rate(params)) {
101         case 8000:
102         case 16000:
103         case 24000:
104         case 32000:
105         case 48000:
106         case 64000:
107         case 96000:
108                 mclk = 12288000;
109                 break;
110         case 11025:
111         case 22050:
112         case 44100:
113         case 88200:
114                 mclk = 11289600;
115                 break;
116         default:
117                 return -EINVAL;
118         }
119
120         /*Set the system clk for codec*/
121         snd_soc_dai_set_pll(codec_dai, 0, RT5651_PLL1_S_MCLK, mclk, 24576000);
122
123         ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1, 24576000,
124                                      SND_SOC_CLOCK_IN);
125         if (ret < 0) {
126                 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
127                 return ret;
128         }
129         return 0;
130 }
131
132 static struct snd_soc_ops rockchip_sound_rt5651_hifi_ops = {
133         .hw_params = rockchip_rt5651_hw_params,
134 };
135
136 static struct snd_soc_ops rockchip_sound_rt5651_voice_ops = {
137         .hw_params = rockchip_rt5651_voice_hw_params,
138 };
139
140 enum {
141         DAILINK_RT5651_HIFI,
142         DAILINK_RT5651_VOICE,
143         DAILINK_TC358749_HDMIIN,
144 };
145
146 #define DAILINK_ENTITIES        (DAILINK_TC358749_HDMIIN + 1)
147
148 static struct snd_soc_dai_link rockchip_dailinks[] = {
149         [DAILINK_RT5651_HIFI] = {
150                 .name = "RT5651 HIFI",
151                 .stream_name = "RT5651 PCM",
152                 .codec_dai_name = "rt5651-aif1",
153                 .ops = &rockchip_sound_rt5651_hifi_ops,
154                 /* set rt5651 as slave */
155                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
156                         SND_SOC_DAIFMT_CBS_CFS,
157         },
158         [DAILINK_RT5651_VOICE] = {
159                 .name = "RT5651 HDMIIN",
160                 .stream_name = "RT5651 PCM",
161                 .codec_dai_name = "rt5651-aif2",
162                 .ops = &rockchip_sound_rt5651_voice_ops,
163                 /* set rt5651 as slave */
164                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
165                         SND_SOC_DAIFMT_CBS_CFS,
166         },
167         [DAILINK_TC358749_HDMIIN] = {
168                 .name = "TC358749 HDMIIN",
169                 .stream_name = "TC358749 PCM",
170                 .codec_dai_name = "tc358749x-audio",
171         },
172 };
173
174 static struct snd_soc_card rockchip_sound_card = {
175         .name = "realtekrt5651codec_hdmiin",
176         .owner = THIS_MODULE,
177         .dai_link = rockchip_dailinks,
178         .num_links =  ARRAY_SIZE(rockchip_dailinks),
179         .dapm_widgets = rockchip_dapm_widgets,
180         .num_dapm_widgets = ARRAY_SIZE(rockchip_dapm_widgets),
181         .dapm_routes = rockchip_dapm_routes,
182         .num_dapm_routes = ARRAY_SIZE(rockchip_dapm_routes),
183         .controls = rockchip_controls,
184         .num_controls = ARRAY_SIZE(rockchip_controls),
185 };
186
187 static int rockchip_sound_probe(struct platform_device *pdev)
188 {
189         struct snd_soc_card *card = &rockchip_sound_card;
190         struct device_node *cpu_node;
191         int i, ret;
192
193         dev_info(&pdev->dev, "%s\n", __func__);
194
195         cpu_node = of_parse_phandle(pdev->dev.of_node, "rockchip,cpu", 0);
196         if (!cpu_node) {
197                 dev_err(&pdev->dev,
198                         "Property 'rockchip,cpu' failed\n");
199                 return -EINVAL;
200         }
201
202         for (i = 0; i < DAILINK_ENTITIES; i++) {
203                 rockchip_dailinks[i].platform_of_node = cpu_node;
204                 rockchip_dailinks[i].cpu_of_node = cpu_node;
205
206                 rockchip_dailinks[i].codec_of_node =
207                         of_parse_phandle(pdev->dev.of_node,
208                                          "rockchip,codec", i);
209                 if (!rockchip_dailinks[i].codec_of_node) {
210                         dev_err(&pdev->dev,
211                                 "Property[%d] 'rockchip,codec' failed\n", i);
212                         return -EINVAL;
213                 }
214         }
215
216         card->dev = &pdev->dev;
217         platform_set_drvdata(pdev, card);
218         ret = devm_snd_soc_register_card(&pdev->dev, card);
219         if (ret)
220                 dev_err(&pdev->dev, "%s register card failed %d\n",
221                         __func__, ret);
222
223         dev_info(&pdev->dev, "snd_soc_register_card successful\n");
224         return ret;
225 }
226
227 static const struct of_device_id rockchip_sound_of_match[] = {
228         { .compatible = "rockchip,rockchip-rt5651-tc358749x-sound", },
229         {},
230 };
231
232 static struct platform_driver rockchip_sound_driver = {
233         .probe = rockchip_sound_probe,
234         .driver = {
235                 .name = DRV_NAME,
236                 .of_match_table = rockchip_sound_of_match,
237 #ifdef CONFIG_PM
238                 .pm = &snd_soc_pm_ops,
239 #endif
240         },
241 };
242
243 module_platform_driver(rockchip_sound_driver);
244
245 MODULE_AUTHOR("Xiaotan Luo <lxt@rock-chips.com>");
246 MODULE_DESCRIPTION("Rockchip ASoC Machine Driver");
247 MODULE_LICENSE("GPL v2");
248 MODULE_ALIAS("platform:" DRV_NAME);
249 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);