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/hdmi-codec.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
21 #include "rockchip_i2s.h"
22 #include "../codecs/ts3a227e.h"
24 #define DRV_NAME "rockchip-snd-max98090"
26 static struct snd_soc_jack headset_jack
;
28 /* Headset jack detection DAPM pins */
29 static struct snd_soc_jack_pin headset_jack_pins
[] = {
32 .mask
= SND_JACK_HEADPHONE
,
36 .mask
= SND_JACK_MICROPHONE
,
41 #define RK_MAX98090_WIDGETS \
42 SND_SOC_DAPM_HP("Headphone", NULL), \
43 SND_SOC_DAPM_MIC("Headset Mic", NULL), \
44 SND_SOC_DAPM_MIC("Int Mic", NULL), \
45 SND_SOC_DAPM_SPK("Speaker", NULL)
47 #define RK_HDMI_WIDGETS \
48 SND_SOC_DAPM_LINE("HDMI", NULL)
50 static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets
[] = {
54 static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets
[] = {
58 static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets
[] = {
63 #define RK_MAX98090_AUDIO_MAP \
64 {"IN34", NULL, "Headset Mic"}, \
65 {"Headset Mic", NULL, "MICBIAS"}, \
66 {"DMICL", NULL, "Int Mic"}, \
67 {"Headphone", NULL, "HPL"}, \
68 {"Headphone", NULL, "HPR"}, \
69 {"Speaker", NULL, "SPKL"}, \
70 {"Speaker", NULL, "SPKR"}
72 #define RK_HDMI_AUDIO_MAP \
75 static const struct snd_soc_dapm_route rk_max98090_audio_map
[] = {
76 RK_MAX98090_AUDIO_MAP
,
79 static const struct snd_soc_dapm_route rk_hdmi_audio_map
[] = {
83 static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map
[] = {
84 RK_MAX98090_AUDIO_MAP
,
88 #define RK_MAX98090_CONTROLS \
89 SOC_DAPM_PIN_SWITCH("Headphone"), \
90 SOC_DAPM_PIN_SWITCH("Headset Mic"), \
91 SOC_DAPM_PIN_SWITCH("Int Mic"), \
92 SOC_DAPM_PIN_SWITCH("Speaker")
94 #define RK_HDMI_CONTROLS \
95 SOC_DAPM_PIN_SWITCH("HDMI")
97 static const struct snd_kcontrol_new rk_max98090_controls
[] = {
101 static const struct snd_kcontrol_new rk_hdmi_controls
[] = {
105 static const struct snd_kcontrol_new rk_max98090_hdmi_controls
[] = {
106 RK_MAX98090_CONTROLS
,
110 static int rk_jack_event(struct notifier_block
*nb
, unsigned long event
,
113 struct snd_soc_jack
*jack
= (struct snd_soc_jack
*)data
;
114 struct snd_soc_dapm_context
*dapm
= &jack
->card
->dapm
;
116 if (event
& SND_JACK_MICROPHONE
) {
117 snd_soc_dapm_force_enable_pin(dapm
, "MICBIAS");
118 snd_soc_dapm_force_enable_pin(dapm
, "SHDN");
120 snd_soc_dapm_disable_pin(dapm
, "MICBIAS");
121 snd_soc_dapm_disable_pin(dapm
, "SHDN");
124 snd_soc_dapm_sync(dapm
);
129 static struct notifier_block rk_jack_nb
= {
130 .notifier_call
= rk_jack_event
,
133 static int rk_init(struct snd_soc_pcm_runtime
*runtime
)
136 * The jack has already been created in the rk_98090_headset_init()
139 snd_soc_jack_notifier_register(&headset_jack
, &rk_jack_nb
);
144 static int rk_aif1_hw_params(struct snd_pcm_substream
*substream
,
145 struct snd_pcm_hw_params
*params
)
148 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
149 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
150 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
153 switch (params_rate(params
)) {
173 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
176 dev_err(cpu_dai
->dev
, "Can't set cpu dai clock %d\n", ret
);
180 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
183 /* HDMI codec dai does not need to set sysclk. */
184 if (!strcmp(rtd
->dai_link
->name
, "HDMI"))
188 dev_err(codec_dai
->dev
, "Can't set codec dai clock %d\n", ret
);
195 static int rk_aif1_startup(struct snd_pcm_substream
*substream
)
198 * Set period size to 240 because pl330 has issue
199 * dealing with larger period in stress testing.
201 return snd_pcm_hw_constraint_minmax(substream
->runtime
,
202 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, 240, 240);
205 static const struct snd_soc_ops rk_aif1_ops
= {
206 .hw_params
= rk_aif1_hw_params
,
207 .startup
= rk_aif1_startup
,
210 SND_SOC_DAILINK_DEFS(analog
,
211 DAILINK_COMP_ARRAY(COMP_EMPTY()),
212 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "HiFi")),
213 DAILINK_COMP_ARRAY(COMP_EMPTY()));
215 SND_SOC_DAILINK_DEFS(hdmi
,
216 DAILINK_COMP_ARRAY(COMP_EMPTY()),
217 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "i2s-hifi")),
218 DAILINK_COMP_ARRAY(COMP_EMPTY()));
225 static struct snd_soc_jack rk_hdmi_jack
;
227 static int rk_hdmi_init(struct snd_soc_pcm_runtime
*runtime
)
229 struct snd_soc_card
*card
= runtime
->card
;
230 struct snd_soc_component
*component
= runtime
->codec_dai
->component
;
233 /* enable jack detection */
234 ret
= snd_soc_card_jack_new(card
, "HDMI Jack", SND_JACK_LINEOUT
,
235 &rk_hdmi_jack
, NULL
, 0);
237 dev_err(card
->dev
, "Can't new HDMI Jack %d\n", ret
);
241 return hdmi_codec_set_jack_detect(component
, &rk_hdmi_jack
);
244 /* max98090 dai_link */
245 static struct snd_soc_dai_link rk_max98090_dailinks
[] = {
248 .stream_name
= "Analog",
251 /* set max98090 as slave */
252 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
253 SND_SOC_DAIFMT_CBS_CFS
,
254 SND_SOC_DAILINK_REG(analog
),
258 /* HDMI codec dai_link */
259 static struct snd_soc_dai_link rk_hdmi_dailinks
[] = {
262 .stream_name
= "HDMI",
263 .init
= rk_hdmi_init
,
265 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
266 SND_SOC_DAIFMT_CBS_CFS
,
267 SND_SOC_DAILINK_REG(hdmi
),
271 /* max98090 and HDMI codec dai_link */
272 static struct snd_soc_dai_link rk_max98090_hdmi_dailinks
[] = {
273 [DAILINK_MAX98090
] = {
275 .stream_name
= "Analog",
278 /* set max98090 as slave */
279 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
280 SND_SOC_DAIFMT_CBS_CFS
,
281 SND_SOC_DAILINK_REG(analog
),
285 .stream_name
= "HDMI",
286 .init
= rk_hdmi_init
,
288 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
289 SND_SOC_DAIFMT_CBS_CFS
,
290 SND_SOC_DAILINK_REG(hdmi
),
294 static int rk_98090_headset_init(struct snd_soc_component
*component
);
296 static struct snd_soc_aux_dev rk_98090_headset_dev
= {
298 .init
= rk_98090_headset_init
,
301 static struct snd_soc_card rockchip_max98090_card
= {
302 .name
= "ROCKCHIP-I2S",
303 .owner
= THIS_MODULE
,
304 .dai_link
= rk_max98090_dailinks
,
305 .num_links
= ARRAY_SIZE(rk_max98090_dailinks
),
306 .aux_dev
= &rk_98090_headset_dev
,
308 .dapm_widgets
= rk_max98090_dapm_widgets
,
309 .num_dapm_widgets
= ARRAY_SIZE(rk_max98090_dapm_widgets
),
310 .dapm_routes
= rk_max98090_audio_map
,
311 .num_dapm_routes
= ARRAY_SIZE(rk_max98090_audio_map
),
312 .controls
= rk_max98090_controls
,
313 .num_controls
= ARRAY_SIZE(rk_max98090_controls
),
316 static struct snd_soc_card rockchip_hdmi_card
= {
317 .name
= "ROCKCHIP-HDMI",
318 .owner
= THIS_MODULE
,
319 .dai_link
= rk_hdmi_dailinks
,
320 .num_links
= ARRAY_SIZE(rk_hdmi_dailinks
),
321 .dapm_widgets
= rk_hdmi_dapm_widgets
,
322 .num_dapm_widgets
= ARRAY_SIZE(rk_hdmi_dapm_widgets
),
323 .dapm_routes
= rk_hdmi_audio_map
,
324 .num_dapm_routes
= ARRAY_SIZE(rk_hdmi_audio_map
),
325 .controls
= rk_hdmi_controls
,
326 .num_controls
= ARRAY_SIZE(rk_hdmi_controls
),
329 static struct snd_soc_card rockchip_max98090_hdmi_card
= {
330 .name
= "ROCKCHIP-MAX98090-HDMI",
331 .owner
= THIS_MODULE
,
332 .dai_link
= rk_max98090_hdmi_dailinks
,
333 .num_links
= ARRAY_SIZE(rk_max98090_hdmi_dailinks
),
334 .aux_dev
= &rk_98090_headset_dev
,
336 .dapm_widgets
= rk_max98090_hdmi_dapm_widgets
,
337 .num_dapm_widgets
= ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets
),
338 .dapm_routes
= rk_max98090_hdmi_audio_map
,
339 .num_dapm_routes
= ARRAY_SIZE(rk_max98090_hdmi_audio_map
),
340 .controls
= rk_max98090_hdmi_controls
,
341 .num_controls
= ARRAY_SIZE(rk_max98090_hdmi_controls
),
344 static int rk_98090_headset_init(struct snd_soc_component
*component
)
348 /* Enable Headset and 4 Buttons Jack detection */
349 ret
= snd_soc_card_jack_new(component
->card
, "Headset Jack",
351 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
352 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
355 ARRAY_SIZE(headset_jack_pins
));
359 ret
= ts3a227e_enable_jack_detect(component
, &headset_jack
);
364 static int rk_parse_headset_from_of(struct device
*dev
, struct device_node
*np
)
366 rk_98090_headset_dev
.dlc
.of_node
= of_parse_phandle(
367 np
, "rockchip,headset-codec", 0);
368 if (!rk_98090_headset_dev
.dlc
.of_node
) {
370 "Property 'rockchip,headset-codec' missing/invalid\n");
376 static int snd_rk_mc_probe(struct platform_device
*pdev
)
379 struct snd_soc_card
*card
;
380 struct device
*dev
= &pdev
->dev
;
381 struct device_node
*np
= pdev
->dev
.of_node
;
382 struct device_node
*np_cpu
;
383 struct device_node
*np_audio
, *np_hdmi
;
385 /* Parse DTS for I2S controller. */
386 np_cpu
= of_parse_phandle(np
, "rockchip,i2s-controller", 0);
390 "Property 'rockchip,i2s-controller missing or invalid\n");
395 * Find the card to use based on the presences of audio codec
396 * and hdmi codec in device property. Set their of_node accordingly.
398 np_audio
= of_parse_phandle(np
, "rockchip,audio-codec", 0);
399 np_hdmi
= of_parse_phandle(np
, "rockchip,hdmi-codec", 0);
400 if (np_audio
&& np_hdmi
) {
401 card
= &rockchip_max98090_hdmi_card
;
402 card
->dai_link
[DAILINK_MAX98090
].codecs
->of_node
= np_audio
;
403 card
->dai_link
[DAILINK_HDMI
].codecs
->of_node
= np_hdmi
;
404 card
->dai_link
[DAILINK_MAX98090
].cpus
->of_node
= np_cpu
;
405 card
->dai_link
[DAILINK_MAX98090
].platforms
->of_node
= np_cpu
;
406 card
->dai_link
[DAILINK_HDMI
].cpus
->of_node
= np_cpu
;
407 card
->dai_link
[DAILINK_HDMI
].platforms
->of_node
= np_cpu
;
408 } else if (np_audio
) {
409 card
= &rockchip_max98090_card
;
410 card
->dai_link
[0].codecs
->of_node
= np_audio
;
411 card
->dai_link
[0].cpus
->of_node
= np_cpu
;
412 card
->dai_link
[0].platforms
->of_node
= np_cpu
;
413 } else if (np_hdmi
) {
414 card
= &rockchip_hdmi_card
;
415 card
->dai_link
[0].codecs
->of_node
= np_hdmi
;
416 card
->dai_link
[0].cpus
->of_node
= np_cpu
;
417 card
->dai_link
[0].platforms
->of_node
= np_cpu
;
419 dev_err(dev
, "At least one of codecs should be specified\n");
425 /* Parse headset detection codec. */
427 ret
= rk_parse_headset_from_of(dev
, np
);
432 /* Parse card name. */
433 ret
= snd_soc_of_parse_card_name(card
, "rockchip,model");
436 "Soc parse card name failed %d\n", ret
);
440 /* register the soc card */
441 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
444 "Soc register card failed %d\n", ret
);
451 static const struct of_device_id rockchip_max98090_of_match
[] = {
452 { .compatible
= "rockchip,rockchip-audio-max98090", },
456 MODULE_DEVICE_TABLE(of
, rockchip_max98090_of_match
);
458 static struct platform_driver snd_rk_mc_driver
= {
459 .probe
= snd_rk_mc_probe
,
462 .pm
= &snd_soc_pm_ops
,
463 .of_match_table
= rockchip_max98090_of_match
,
467 module_platform_driver(snd_rk_mc_driver
);
469 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
470 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
471 MODULE_LICENSE("GPL v2");
472 MODULE_ALIAS("platform:" DRV_NAME
);