1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
5 * Copyright (c) 2014, ROCKCHIP CORPORATION. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/gpio.h>
13 #include <linux/of_gpio.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>
20 #include "rockchip_i2s.h"
21 #include "../codecs/ts3a227e.h"
23 #define DRV_NAME "rockchip-snd-max98090"
25 static struct snd_soc_jack headset_jack;
27 /* Headset jack detection DAPM pins */
28 static struct snd_soc_jack_pin headset_jack_pins[] = {
31 .mask = SND_JACK_HEADPHONE,
35 .mask = SND_JACK_MICROPHONE,
40 #define RK_MAX98090_WIDGETS \
41 SND_SOC_DAPM_HP("Headphone", NULL), \
42 SND_SOC_DAPM_MIC("Headset Mic", NULL), \
43 SND_SOC_DAPM_MIC("Int Mic", NULL), \
44 SND_SOC_DAPM_SPK("Speaker", NULL)
46 #define RK_HDMI_WIDGETS \
47 SND_SOC_DAPM_LINE("HDMI", NULL)
49 static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets[] = {
53 static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets[] = {
57 static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets[] = {
62 #define RK_MAX98090_AUDIO_MAP \
63 {"IN34", NULL, "Headset Mic"}, \
64 {"Headset Mic", NULL, "MICBIAS"}, \
65 {"DMICL", NULL, "Int Mic"}, \
66 {"Headphone", NULL, "HPL"}, \
67 {"Headphone", NULL, "HPR"}, \
68 {"Speaker", NULL, "SPKL"}, \
69 {"Speaker", NULL, "SPKR"}
71 #define RK_HDMI_AUDIO_MAP \
74 static const struct snd_soc_dapm_route rk_max98090_audio_map[] = {
75 RK_MAX98090_AUDIO_MAP,
78 static const struct snd_soc_dapm_route rk_hdmi_audio_map[] = {
82 static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map[] = {
83 RK_MAX98090_AUDIO_MAP,
87 #define RK_MAX98090_CONTROLS \
88 SOC_DAPM_PIN_SWITCH("Headphone"), \
89 SOC_DAPM_PIN_SWITCH("Headset Mic"), \
90 SOC_DAPM_PIN_SWITCH("Int Mic"), \
91 SOC_DAPM_PIN_SWITCH("Speaker")
93 #define RK_HDMI_CONTROLS \
94 SOC_DAPM_PIN_SWITCH("HDMI")
96 static const struct snd_kcontrol_new rk_max98090_controls[] = {
100 static const struct snd_kcontrol_new rk_hdmi_controls[] = {
104 static const struct snd_kcontrol_new rk_max98090_hdmi_controls[] = {
105 RK_MAX98090_CONTROLS,
109 static int rk_jack_event(struct notifier_block *nb, unsigned long event,
112 struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
113 struct snd_soc_dapm_context *dapm = &jack->card->dapm;
115 if (event & SND_JACK_MICROPHONE) {
116 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
117 snd_soc_dapm_force_enable_pin(dapm, "SHDN");
119 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
120 snd_soc_dapm_disable_pin(dapm, "SHDN");
123 snd_soc_dapm_sync(dapm);
128 static struct notifier_block rk_jack_nb = {
129 .notifier_call = rk_jack_event,
132 static int rk_init(struct snd_soc_pcm_runtime *runtime)
135 * The jack has already been created in the rk_98090_headset_init()
138 snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb);
143 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
144 struct snd_pcm_hw_params *params)
147 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
149 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
152 switch (params_rate(params)) {
172 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
175 dev_err(cpu_dai->dev, "Can't set cpu dai clock %d\n", ret);
179 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
182 /* HDMI codec dai does not need to set sysclk. */
183 if (!strcmp(rtd->dai_link->name, "HDMI"))
187 dev_err(codec_dai->dev, "Can't set codec dai clock %d\n", ret);
194 static int rk_aif1_startup(struct snd_pcm_substream *substream)
197 * Set period size to 240 because pl330 has issue
198 * dealing with larger period in stress testing.
200 return snd_pcm_hw_constraint_minmax(substream->runtime,
201 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 240, 240);
204 static const struct snd_soc_ops rk_aif1_ops = {
205 .hw_params = rk_aif1_hw_params,
206 .startup = rk_aif1_startup,
209 SND_SOC_DAILINK_DEFS(analog,
210 DAILINK_COMP_ARRAY(COMP_EMPTY()),
211 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
212 DAILINK_COMP_ARRAY(COMP_EMPTY()));
214 SND_SOC_DAILINK_DEFS(hdmi,
215 DAILINK_COMP_ARRAY(COMP_EMPTY()),
216 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
217 DAILINK_COMP_ARRAY(COMP_EMPTY()));
224 static struct snd_soc_jack rk_hdmi_jack;
226 static int rk_hdmi_init(struct snd_soc_pcm_runtime *runtime)
228 struct snd_soc_card *card = runtime->card;
229 struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
232 /* enable jack detection */
233 ret = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
234 &rk_hdmi_jack, NULL, 0);
236 dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
240 return snd_soc_component_set_jack(component, &rk_hdmi_jack, NULL);
243 /* max98090 dai_link */
244 static struct snd_soc_dai_link rk_max98090_dailinks[] = {
247 .stream_name = "Analog",
250 /* set max98090 as slave */
251 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
252 SND_SOC_DAIFMT_CBS_CFS,
253 SND_SOC_DAILINK_REG(analog),
257 /* HDMI codec dai_link */
258 static struct snd_soc_dai_link rk_hdmi_dailinks[] = {
261 .stream_name = "HDMI",
262 .init = rk_hdmi_init,
264 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
265 SND_SOC_DAIFMT_CBS_CFS,
266 SND_SOC_DAILINK_REG(hdmi),
270 /* max98090 and HDMI codec dai_link */
271 static struct snd_soc_dai_link rk_max98090_hdmi_dailinks[] = {
272 [DAILINK_MAX98090] = {
274 .stream_name = "Analog",
277 /* set max98090 as slave */
278 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
279 SND_SOC_DAIFMT_CBS_CFS,
280 SND_SOC_DAILINK_REG(analog),
284 .stream_name = "HDMI",
285 .init = rk_hdmi_init,
287 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
288 SND_SOC_DAIFMT_CBS_CFS,
289 SND_SOC_DAILINK_REG(hdmi),
293 static int rk_98090_headset_init(struct snd_soc_component *component);
295 static struct snd_soc_aux_dev rk_98090_headset_dev = {
297 .init = rk_98090_headset_init,
300 static struct snd_soc_card rockchip_max98090_card = {
301 .name = "ROCKCHIP-I2S",
302 .owner = THIS_MODULE,
303 .dai_link = rk_max98090_dailinks,
304 .num_links = ARRAY_SIZE(rk_max98090_dailinks),
305 .aux_dev = &rk_98090_headset_dev,
307 .dapm_widgets = rk_max98090_dapm_widgets,
308 .num_dapm_widgets = ARRAY_SIZE(rk_max98090_dapm_widgets),
309 .dapm_routes = rk_max98090_audio_map,
310 .num_dapm_routes = ARRAY_SIZE(rk_max98090_audio_map),
311 .controls = rk_max98090_controls,
312 .num_controls = ARRAY_SIZE(rk_max98090_controls),
315 static struct snd_soc_card rockchip_hdmi_card = {
316 .name = "ROCKCHIP-HDMI",
317 .owner = THIS_MODULE,
318 .dai_link = rk_hdmi_dailinks,
319 .num_links = ARRAY_SIZE(rk_hdmi_dailinks),
320 .dapm_widgets = rk_hdmi_dapm_widgets,
321 .num_dapm_widgets = ARRAY_SIZE(rk_hdmi_dapm_widgets),
322 .dapm_routes = rk_hdmi_audio_map,
323 .num_dapm_routes = ARRAY_SIZE(rk_hdmi_audio_map),
324 .controls = rk_hdmi_controls,
325 .num_controls = ARRAY_SIZE(rk_hdmi_controls),
328 static struct snd_soc_card rockchip_max98090_hdmi_card = {
329 .name = "ROCKCHIP-MAX98090-HDMI",
330 .owner = THIS_MODULE,
331 .dai_link = rk_max98090_hdmi_dailinks,
332 .num_links = ARRAY_SIZE(rk_max98090_hdmi_dailinks),
333 .aux_dev = &rk_98090_headset_dev,
335 .dapm_widgets = rk_max98090_hdmi_dapm_widgets,
336 .num_dapm_widgets = ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets),
337 .dapm_routes = rk_max98090_hdmi_audio_map,
338 .num_dapm_routes = ARRAY_SIZE(rk_max98090_hdmi_audio_map),
339 .controls = rk_max98090_hdmi_controls,
340 .num_controls = ARRAY_SIZE(rk_max98090_hdmi_controls),
343 static int rk_98090_headset_init(struct snd_soc_component *component)
347 /* Enable Headset and 4 Buttons Jack detection */
348 ret = snd_soc_card_jack_new(component->card, "Headset Jack",
350 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
351 SND_JACK_BTN_2 | SND_JACK_BTN_3,
354 ARRAY_SIZE(headset_jack_pins));
358 ret = ts3a227e_enable_jack_detect(component, &headset_jack);
363 static int rk_parse_headset_from_of(struct device *dev, struct device_node *np)
365 rk_98090_headset_dev.dlc.of_node = of_parse_phandle(
366 np, "rockchip,headset-codec", 0);
367 if (!rk_98090_headset_dev.dlc.of_node) {
369 "Property 'rockchip,headset-codec' missing/invalid\n");
375 static int snd_rk_mc_probe(struct platform_device *pdev)
378 struct snd_soc_card *card;
379 struct device *dev = &pdev->dev;
380 struct device_node *np = pdev->dev.of_node;
381 struct device_node *np_cpu;
382 struct device_node *np_audio, *np_hdmi;
384 /* Parse DTS for I2S controller. */
385 np_cpu = of_parse_phandle(np, "rockchip,i2s-controller", 0);
389 "Property 'rockchip,i2s-controller missing or invalid\n");
394 * Find the card to use based on the presences of audio codec
395 * and hdmi codec in device property. Set their of_node accordingly.
397 np_audio = of_parse_phandle(np, "rockchip,audio-codec", 0);
398 np_hdmi = of_parse_phandle(np, "rockchip,hdmi-codec", 0);
399 if (np_audio && np_hdmi) {
400 card = &rockchip_max98090_hdmi_card;
401 card->dai_link[DAILINK_MAX98090].codecs->of_node = np_audio;
402 card->dai_link[DAILINK_HDMI].codecs->of_node = np_hdmi;
403 card->dai_link[DAILINK_MAX98090].cpus->of_node = np_cpu;
404 card->dai_link[DAILINK_MAX98090].platforms->of_node = np_cpu;
405 card->dai_link[DAILINK_HDMI].cpus->of_node = np_cpu;
406 card->dai_link[DAILINK_HDMI].platforms->of_node = np_cpu;
407 } else if (np_audio) {
408 card = &rockchip_max98090_card;
409 card->dai_link[0].codecs->of_node = np_audio;
410 card->dai_link[0].cpus->of_node = np_cpu;
411 card->dai_link[0].platforms->of_node = np_cpu;
412 } else if (np_hdmi) {
413 card = &rockchip_hdmi_card;
414 card->dai_link[0].codecs->of_node = np_hdmi;
415 card->dai_link[0].cpus->of_node = np_cpu;
416 card->dai_link[0].platforms->of_node = np_cpu;
418 dev_err(dev, "At least one of codecs should be specified\n");
424 /* Parse headset detection codec. */
426 ret = rk_parse_headset_from_of(dev, np);
431 /* Parse card name. */
432 ret = snd_soc_of_parse_card_name(card, "rockchip,model");
435 "Soc parse card name failed %d\n", ret);
439 /* register the soc card */
440 ret = devm_snd_soc_register_card(&pdev->dev, card);
443 "Soc register card failed %d\n", ret);
450 static const struct of_device_id rockchip_max98090_of_match[] = {
451 { .compatible = "rockchip,rockchip-audio-max98090", },
455 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match);
457 static struct platform_driver snd_rk_mc_driver = {
458 .probe = snd_rk_mc_probe,
461 .pm = &snd_soc_pm_ops,
462 .of_match_table = rockchip_max98090_of_match,
466 module_platform_driver(snd_rk_mc_driver);
468 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
469 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
470 MODULE_LICENSE("GPL v2");
471 MODULE_ALIAS("platform:" DRV_NAME);