xref: /OK3568_Linux_fs/kernel/sound/soc/rockchip/rockchip_cdndp.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Rockchip machine ASoC driver for boards using CDN DP
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2016, ROCKCHIP CORPORATION.  All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify it
7*4882a593Smuzhiyun  * under the terms and conditions of the GNU General Public License,
8*4882a593Smuzhiyun  * version 2, as published by the Free Software Foundation.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  * This program is distributed in the hope it will be useful, but WITHOUT
11*4882a593Smuzhiyun  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13*4882a593Smuzhiyun  * more details.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
16*4882a593Smuzhiyun  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/input.h>
21*4882a593Smuzhiyun #include <sound/jack.h>
22*4882a593Smuzhiyun #include <sound/hdmi-codec.h>
23*4882a593Smuzhiyun #include <sound/soc.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define DRV_NAME "rockchip-cdndp-sound"
26*4882a593Smuzhiyun 
rockchip_sound_cdndp_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)27*4882a593Smuzhiyun static int rockchip_sound_cdndp_hw_params(struct snd_pcm_substream *substream,
28*4882a593Smuzhiyun 					  struct snd_pcm_hw_params *params)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
31*4882a593Smuzhiyun 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
32*4882a593Smuzhiyun 	int mclk, ret;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun 	/* in bypass mode, the mclk has to be one of the frequencies below */
35*4882a593Smuzhiyun 	switch (params_rate(params)) {
36*4882a593Smuzhiyun 	case 8000:
37*4882a593Smuzhiyun 	case 16000:
38*4882a593Smuzhiyun 	case 24000:
39*4882a593Smuzhiyun 	case 32000:
40*4882a593Smuzhiyun 	case 48000:
41*4882a593Smuzhiyun 	case 64000:
42*4882a593Smuzhiyun 	case 96000:
43*4882a593Smuzhiyun 		mclk = 12288000;
44*4882a593Smuzhiyun 		break;
45*4882a593Smuzhiyun 	case 11025:
46*4882a593Smuzhiyun 	case 22050:
47*4882a593Smuzhiyun 	case 44100:
48*4882a593Smuzhiyun 	case 88200:
49*4882a593Smuzhiyun 		mclk = 11289600;
50*4882a593Smuzhiyun 		break;
51*4882a593Smuzhiyun 	case 176400:
52*4882a593Smuzhiyun 		mclk = 11289600 * 2;
53*4882a593Smuzhiyun 		break;
54*4882a593Smuzhiyun 	case 192000:
55*4882a593Smuzhiyun 		mclk = 12288000 * 2;
56*4882a593Smuzhiyun 		break;
57*4882a593Smuzhiyun 	default:
58*4882a593Smuzhiyun 		return -EINVAL;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
62*4882a593Smuzhiyun 				     SND_SOC_CLOCK_OUT);
63*4882a593Smuzhiyun 	if (ret && ret != -ENOTSUPP) {
64*4882a593Smuzhiyun 		dev_err(cpu_dai->dev, "Can't set cpu clock %d\n", ret);
65*4882a593Smuzhiyun 		return ret;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return 0;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun static struct snd_soc_jack cdn_dp_card_jack;
72*4882a593Smuzhiyun 
rockchip_sound_cdndp_init(struct snd_soc_pcm_runtime * runtime)73*4882a593Smuzhiyun static int rockchip_sound_cdndp_init(struct snd_soc_pcm_runtime *runtime)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct snd_soc_card *card = runtime->card;
76*4882a593Smuzhiyun 	struct snd_soc_codec *codec = runtime->codec;
77*4882a593Smuzhiyun 	int ret;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* enable jack detection */
80*4882a593Smuzhiyun 	ret = snd_soc_card_jack_new(card, "DP Jack", SND_JACK_LINEOUT,
81*4882a593Smuzhiyun 				    &cdn_dp_card_jack, NULL, 0);
82*4882a593Smuzhiyun 	if (ret) {
83*4882a593Smuzhiyun 		dev_err(card->dev, "Can't create DP Jack %d\n", ret);
84*4882a593Smuzhiyun 		return ret;
85*4882a593Smuzhiyun 	}
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	return hdmi_codec_set_jack_detect(codec, &cdn_dp_card_jack);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun static struct snd_soc_ops rockchip_sound_cdndp_ops = {
91*4882a593Smuzhiyun 	.hw_params = rockchip_sound_cdndp_hw_params,
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun static struct snd_soc_dai_link cdndp_dailink = {
95*4882a593Smuzhiyun 	.name = "DP",
96*4882a593Smuzhiyun 	.stream_name = "DP PCM",
97*4882a593Smuzhiyun 	.codec_dai_name = "spdif-hifi",
98*4882a593Smuzhiyun 	.init = rockchip_sound_cdndp_init,
99*4882a593Smuzhiyun 	.ops = &rockchip_sound_cdndp_ops,
100*4882a593Smuzhiyun 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
101*4882a593Smuzhiyun 		   SND_SOC_DAIFMT_CBS_CFS,
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun static struct snd_soc_card rockchip_sound_card = {
105*4882a593Smuzhiyun 	.name = "rockchip-cdndp-sound",
106*4882a593Smuzhiyun 	.owner = THIS_MODULE,
107*4882a593Smuzhiyun 	.dai_link = &cdndp_dailink,
108*4882a593Smuzhiyun 	.num_links = 1,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
rockchip_sound_probe(struct platform_device * pdev)111*4882a593Smuzhiyun static int rockchip_sound_probe(struct platform_device *pdev)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	struct snd_soc_card *card = &rockchip_sound_card;
114*4882a593Smuzhiyun 	struct device_node *cpu_node;
115*4882a593Smuzhiyun 	int ret;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	cpu_node = of_parse_phandle(pdev->dev.of_node, "rockchip,cpu", 0);
118*4882a593Smuzhiyun 	if (!cpu_node) {
119*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Property 'rockchip,cpu' missing or invalid\n");
120*4882a593Smuzhiyun 		return -EINVAL;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	cdndp_dailink.platform_of_node = cpu_node;
124*4882a593Smuzhiyun 	cdndp_dailink.cpu_of_node = cpu_node;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	cdndp_dailink.codec_of_node = of_parse_phandle(pdev->dev.of_node,
127*4882a593Smuzhiyun 							   "rockchip,codec", 0);
128*4882a593Smuzhiyun 	if (!cdndp_dailink.codec_of_node) {
129*4882a593Smuzhiyun 		dev_err(&pdev->dev, "Property 'rockchip,codec' invalid\n");
130*4882a593Smuzhiyun 		return -EINVAL;
131*4882a593Smuzhiyun 	}
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	card->dev = &pdev->dev;
134*4882a593Smuzhiyun 	platform_set_drvdata(pdev, card);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	ret = devm_snd_soc_register_card(&pdev->dev, card);
137*4882a593Smuzhiyun 	if (ret)
138*4882a593Smuzhiyun 		dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
139*4882a593Smuzhiyun 			__func__, ret);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return ret;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun static const struct of_device_id rockchip_sound_of_match[] = {
145*4882a593Smuzhiyun 	{ .compatible = "rockchip,cdndp-sound", },
146*4882a593Smuzhiyun 	{},
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun static struct platform_driver rockchip_sound_driver = {
150*4882a593Smuzhiyun 	.probe = rockchip_sound_probe,
151*4882a593Smuzhiyun 	.driver = {
152*4882a593Smuzhiyun 		.name = DRV_NAME,
153*4882a593Smuzhiyun 		.of_match_table = rockchip_sound_of_match,
154*4882a593Smuzhiyun #ifdef CONFIG_PM
155*4882a593Smuzhiyun 		.pm = &snd_soc_pm_ops,
156*4882a593Smuzhiyun #endif
157*4882a593Smuzhiyun 	},
158*4882a593Smuzhiyun };
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun module_platform_driver(rockchip_sound_driver);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
163*4882a593Smuzhiyun MODULE_DESCRIPTION("Rockchip CDN DP Machine Driver");
164*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
165*4882a593Smuzhiyun MODULE_ALIAS("platform:" DRV_NAME);
166*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
167