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