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