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