xref: /linux/sound/soc/rockchip/rockchip_rt5645.c (revision dc0d1c4519095a6c6bbd9ec4a808674aba502741)
1 /*
2  * Rockchip machine ASoC driver for boards using a RT5645/RT5650 CODEC.
3  *
4  * Copyright (c) 2015, 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 
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/gpio.h>
24 #include <linux/of_gpio.h>
25 #include <linux/delay.h>
26 #include <sound/core.h>
27 #include <sound/jack.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include "rockchip_i2s.h"
32 #include "../codecs/rt5645.h"
33 
34 #define DRV_NAME "rockchip-snd-rt5645"
35 
36 static struct snd_soc_jack headset_jack;
37 
38 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
39 	SND_SOC_DAPM_HP("Headphones", NULL),
40 	SND_SOC_DAPM_SPK("Speakers", NULL),
41 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
42 	SND_SOC_DAPM_MIC("Int Mic", NULL),
43 };
44 
45 static const struct snd_soc_dapm_route rk_audio_map[] = {
46 	/* Input Lines */
47 	{"DMIC L2", NULL, "Int Mic"},
48 	{"DMIC R2", NULL, "Int Mic"},
49 	{"RECMIXL", NULL, "Headset Mic"},
50 	{"RECMIXR", NULL, "Headset Mic"},
51 
52 	/* Output Lines */
53 	{"Headphones", NULL, "HPOR"},
54 	{"Headphones", NULL, "HPOL"},
55 	{"Speakers", NULL, "SPOL"},
56 	{"Speakers", NULL, "SPOR"},
57 };
58 
59 static const struct snd_kcontrol_new rk_mc_controls[] = {
60 	SOC_DAPM_PIN_SWITCH("Headphones"),
61 	SOC_DAPM_PIN_SWITCH("Speakers"),
62 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
63 	SOC_DAPM_PIN_SWITCH("Int Mic"),
64 };
65 
66 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
67 			     struct snd_pcm_hw_params *params)
68 {
69 	int ret = 0;
70 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
72 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
73 	int mclk;
74 
75 	switch (params_rate(params)) {
76 	case 8000:
77 	case 16000:
78 	case 24000:
79 	case 32000:
80 	case 48000:
81 	case 64000:
82 	case 96000:
83 		mclk = 12288000;
84 		break;
85 	case 11025:
86 	case 22050:
87 	case 44100:
88 	case 88200:
89 		mclk = 11289600;
90 		break;
91 	default:
92 		return -EINVAL;
93 	}
94 
95 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
96 				     SND_SOC_CLOCK_OUT);
97 	if (ret < 0) {
98 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
99 		return ret;
100 	}
101 
102 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
103 				     SND_SOC_CLOCK_IN);
104 	if (ret < 0) {
105 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
106 		return ret;
107 	}
108 
109 	return ret;
110 }
111 
112 static int rk_init(struct snd_soc_pcm_runtime *runtime)
113 {
114 	struct snd_soc_card *card = runtime->card;
115 	int ret;
116 
117 	/* Enable Headset and 4 Buttons Jack detection */
118 	ret = snd_soc_card_jack_new(card, "Headset Jack",
119 				    SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
120 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
121 				    SND_JACK_BTN_2 | SND_JACK_BTN_3,
122 				    &headset_jack, NULL, 0);
123 	if (ret) {
124 		dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret);
125 		return ret;
126 	}
127 
128 	return rt5645_set_jack_detect(runtime->codec_dai->component,
129 				     &headset_jack,
130 				     &headset_jack,
131 				     &headset_jack);
132 }
133 
134 static const struct snd_soc_ops rk_aif1_ops = {
135 	.hw_params = rk_aif1_hw_params,
136 };
137 
138 static struct snd_soc_dai_link rk_dailink = {
139 	.name = "rt5645",
140 	.stream_name = "rt5645 PCM",
141 	.codec_dai_name = "rt5645-aif1",
142 	.init = rk_init,
143 	.ops = &rk_aif1_ops,
144 	/* set rt5645 as slave */
145 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
146 		SND_SOC_DAIFMT_CBS_CFS,
147 };
148 
149 static struct snd_soc_card snd_soc_card_rk = {
150 	.name = "I2S-RT5650",
151 	.owner = THIS_MODULE,
152 	.dai_link = &rk_dailink,
153 	.num_links = 1,
154 	.dapm_widgets = rk_dapm_widgets,
155 	.num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
156 	.dapm_routes = rk_audio_map,
157 	.num_dapm_routes = ARRAY_SIZE(rk_audio_map),
158 	.controls = rk_mc_controls,
159 	.num_controls = ARRAY_SIZE(rk_mc_controls),
160 };
161 
162 static int snd_rk_mc_probe(struct platform_device *pdev)
163 {
164 	int ret = 0;
165 	struct snd_soc_card *card = &snd_soc_card_rk;
166 	struct device_node *np = pdev->dev.of_node;
167 
168 	/* register the soc card */
169 	card->dev = &pdev->dev;
170 
171 	rk_dailink.codec_of_node = of_parse_phandle(np,
172 			"rockchip,audio-codec", 0);
173 	if (!rk_dailink.codec_of_node) {
174 		dev_err(&pdev->dev,
175 			"Property 'rockchip,audio-codec' missing or invalid\n");
176 		return -EINVAL;
177 	}
178 
179 	rk_dailink.cpu_of_node = of_parse_phandle(np,
180 			"rockchip,i2s-controller", 0);
181 	if (!rk_dailink.cpu_of_node) {
182 		dev_err(&pdev->dev,
183 			"Property 'rockchip,i2s-controller' missing or invalid\n");
184 		ret = -EINVAL;
185 		goto put_codec_of_node;
186 	}
187 
188 	rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
189 
190 	ret = snd_soc_of_parse_card_name(card, "rockchip,model");
191 	if (ret) {
192 		dev_err(&pdev->dev,
193 			"Soc parse card name failed %d\n", ret);
194 		goto put_cpu_of_node;
195 	}
196 
197 	ret = devm_snd_soc_register_card(&pdev->dev, card);
198 	if (ret) {
199 		dev_err(&pdev->dev,
200 			"Soc register card failed %d\n", ret);
201 		goto put_cpu_of_node;
202 	}
203 
204 	return ret;
205 
206 put_cpu_of_node:
207 	of_node_put(rk_dailink.cpu_of_node);
208 	rk_dailink.cpu_of_node = NULL;
209 put_codec_of_node:
210 	of_node_put(rk_dailink.codec_of_node);
211 	rk_dailink.codec_of_node = NULL;
212 
213 	return ret;
214 }
215 
216 static int snd_rk_mc_remove(struct platform_device *pdev)
217 {
218 	of_node_put(rk_dailink.cpu_of_node);
219 	rk_dailink.cpu_of_node = NULL;
220 	of_node_put(rk_dailink.codec_of_node);
221 	rk_dailink.codec_of_node = NULL;
222 
223 	return 0;
224 }
225 
226 static const struct of_device_id rockchip_rt5645_of_match[] = {
227 	{ .compatible = "rockchip,rockchip-audio-rt5645", },
228 	{},
229 };
230 
231 MODULE_DEVICE_TABLE(of, rockchip_rt5645_of_match);
232 
233 static struct platform_driver snd_rk_mc_driver = {
234 	.probe = snd_rk_mc_probe,
235 	.remove = snd_rk_mc_remove,
236 	.driver = {
237 		.name = DRV_NAME,
238 		.pm = &snd_soc_pm_ops,
239 		.of_match_table = rockchip_rt5645_of_match,
240 	},
241 };
242 
243 module_platform_driver(snd_rk_mc_driver);
244 
245 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
246 MODULE_DESCRIPTION("Rockchip rt5645 machine ASoC driver");
247 MODULE_LICENSE("GPL v2");
248 MODULE_ALIAS("platform:" DRV_NAME);
249