1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip machine ASoC driver for boards using a RT5645/RT5650 CODEC.
5 * Copyright (c) 2015, ROCKCHIP CORPORATION. All rights reserved.
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"
22 #define DRV_NAME "rockchip-snd-rt5645"
24 static struct snd_soc_jack headset_jack;
26 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
27 SND_SOC_DAPM_HP("Headphones", NULL),
28 SND_SOC_DAPM_SPK("Speakers", NULL),
29 SND_SOC_DAPM_MIC("Headset Mic", NULL),
30 SND_SOC_DAPM_MIC("Int Mic", NULL),
33 static const struct snd_soc_dapm_route rk_audio_map[] = {
35 {"DMIC L2", NULL, "Int Mic"},
36 {"DMIC R2", NULL, "Int Mic"},
37 {"RECMIXL", NULL, "Headset Mic"},
38 {"RECMIXR", NULL, "Headset Mic"},
41 {"Headphones", NULL, "HPOR"},
42 {"Headphones", NULL, "HPOL"},
43 {"Speakers", NULL, "SPOL"},
44 {"Speakers", NULL, "SPOR"},
47 static const struct snd_kcontrol_new rk_mc_controls[] = {
48 SOC_DAPM_PIN_SWITCH("Headphones"),
49 SOC_DAPM_PIN_SWITCH("Speakers"),
50 SOC_DAPM_PIN_SWITCH("Headset Mic"),
51 SOC_DAPM_PIN_SWITCH("Int Mic"),
54 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
55 struct snd_pcm_hw_params *params)
58 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
59 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
60 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
63 switch (params_rate(params)) {
83 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
86 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
90 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
93 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
100 static int rk_init(struct snd_soc_pcm_runtime *runtime)
102 struct snd_soc_card *card = runtime->card;
105 /* Enable Headset and 4 Buttons Jack detection */
106 ret = snd_soc_card_jack_new(card, "Headset Jack",
107 SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
108 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
109 SND_JACK_BTN_2 | SND_JACK_BTN_3,
112 dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret);
116 return rt5645_set_jack_detect(asoc_rtd_to_codec(runtime, 0)->component,
122 static const struct snd_soc_ops rk_aif1_ops = {
123 .hw_params = rk_aif1_hw_params,
126 SND_SOC_DAILINK_DEFS(pcm,
127 DAILINK_COMP_ARRAY(COMP_EMPTY()),
128 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "rt5645-aif1")),
129 DAILINK_COMP_ARRAY(COMP_EMPTY()));
131 static struct snd_soc_dai_link rk_dailink = {
133 .stream_name = "rt5645 PCM",
136 /* set rt5645 as slave */
137 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
138 SND_SOC_DAIFMT_CBS_CFS,
139 SND_SOC_DAILINK_REG(pcm),
142 static struct snd_soc_card snd_soc_card_rk = {
143 .name = "I2S-RT5650",
144 .owner = THIS_MODULE,
145 .dai_link = &rk_dailink,
147 .dapm_widgets = rk_dapm_widgets,
148 .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
149 .dapm_routes = rk_audio_map,
150 .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
151 .controls = rk_mc_controls,
152 .num_controls = ARRAY_SIZE(rk_mc_controls),
155 static int snd_rk_mc_probe(struct platform_device *pdev)
158 struct snd_soc_card *card = &snd_soc_card_rk;
159 struct device_node *np = pdev->dev.of_node;
161 /* register the soc card */
162 card->dev = &pdev->dev;
164 rk_dailink.codecs->of_node = of_parse_phandle(np,
165 "rockchip,audio-codec", 0);
166 if (!rk_dailink.codecs->of_node) {
168 "Property 'rockchip,audio-codec' missing or invalid\n");
172 rk_dailink.cpus->of_node = of_parse_phandle(np,
173 "rockchip,i2s-controller", 0);
174 if (!rk_dailink.cpus->of_node) {
176 "Property 'rockchip,i2s-controller' missing or invalid\n");
178 goto put_codec_of_node;
181 rk_dailink.platforms->of_node = rk_dailink.cpus->of_node;
183 ret = snd_soc_of_parse_card_name(card, "rockchip,model");
186 "Soc parse card name failed %d\n", ret);
187 goto put_cpu_of_node;
190 ret = devm_snd_soc_register_card(&pdev->dev, card);
193 "Soc register card failed %d\n", ret);
194 goto put_cpu_of_node;
200 of_node_put(rk_dailink.cpus->of_node);
201 rk_dailink.cpus->of_node = NULL;
203 of_node_put(rk_dailink.codecs->of_node);
204 rk_dailink.codecs->of_node = NULL;
209 static int snd_rk_mc_remove(struct platform_device *pdev)
211 of_node_put(rk_dailink.cpus->of_node);
212 rk_dailink.cpus->of_node = NULL;
213 of_node_put(rk_dailink.codecs->of_node);
214 rk_dailink.codecs->of_node = NULL;
219 static const struct of_device_id rockchip_rt5645_of_match[] = {
220 { .compatible = "rockchip,rockchip-audio-rt5645", },
224 MODULE_DEVICE_TABLE(of, rockchip_rt5645_of_match);
226 static struct platform_driver snd_rk_mc_driver = {
227 .probe = snd_rk_mc_probe,
228 .remove = snd_rk_mc_remove,
231 .pm = &snd_soc_pm_ops,
232 .of_match_table = rockchip_rt5645_of_match,
236 module_platform_driver(snd_rk_mc_driver);
238 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
239 MODULE_DESCRIPTION("Rockchip rt5645 machine ASoC driver");
240 MODULE_LICENSE("GPL v2");
241 MODULE_ALIAS("platform:" DRV_NAME);