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>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <sound/core.h>
13 #include <sound/jack.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
18 #include "rockchip_i2s.h"
19 #include "../codecs/ts3a227e.h"
21 #define DRV_NAME "rockchip-snd-max98090"
23 static struct snd_soc_jack headset_jack
;
25 /* Headset jack detection DAPM pins */
26 static struct snd_soc_jack_pin headset_jack_pins
[] = {
29 .mask
= SND_JACK_HEADPHONE
,
33 .mask
= SND_JACK_MICROPHONE
,
38 #define RK_MAX98090_WIDGETS \
39 SND_SOC_DAPM_HP("Headphone", NULL), \
40 SND_SOC_DAPM_MIC("Headset Mic", NULL), \
41 SND_SOC_DAPM_MIC("Int Mic", NULL), \
42 SND_SOC_DAPM_SPK("Speaker", NULL)
44 #define RK_HDMI_WIDGETS \
45 SND_SOC_DAPM_LINE("HDMI", NULL)
47 static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets
[] = {
51 static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets
[] = {
55 static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets
[] = {
60 #define RK_MAX98090_AUDIO_MAP \
61 {"IN34", NULL, "Headset Mic"}, \
62 {"Headset Mic", NULL, "MICBIAS"}, \
63 {"DMICL", NULL, "Int Mic"}, \
64 {"Headphone", NULL, "HPL"}, \
65 {"Headphone", NULL, "HPR"}, \
66 {"Speaker", NULL, "SPKL"}, \
67 {"Speaker", NULL, "SPKR"}
69 #define RK_HDMI_AUDIO_MAP \
72 static const struct snd_soc_dapm_route rk_max98090_audio_map
[] = {
73 RK_MAX98090_AUDIO_MAP
,
76 static const struct snd_soc_dapm_route rk_hdmi_audio_map
[] = {
80 static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map
[] = {
81 RK_MAX98090_AUDIO_MAP
,
85 #define RK_MAX98090_CONTROLS \
86 SOC_DAPM_PIN_SWITCH("Headphone"), \
87 SOC_DAPM_PIN_SWITCH("Headset Mic"), \
88 SOC_DAPM_PIN_SWITCH("Int Mic"), \
89 SOC_DAPM_PIN_SWITCH("Speaker")
91 #define RK_HDMI_CONTROLS \
92 SOC_DAPM_PIN_SWITCH("HDMI")
94 static const struct snd_kcontrol_new rk_max98090_controls
[] = {
98 static const struct snd_kcontrol_new rk_hdmi_controls
[] = {
102 static const struct snd_kcontrol_new rk_max98090_hdmi_controls
[] = {
103 RK_MAX98090_CONTROLS
,
107 static int rk_jack_event(struct notifier_block
*nb
, unsigned long event
,
110 struct snd_soc_jack
*jack
= (struct snd_soc_jack
*)data
;
111 struct snd_soc_dapm_context
*dapm
= &jack
->card
->dapm
;
113 if (event
& SND_JACK_MICROPHONE
) {
114 snd_soc_dapm_force_enable_pin(dapm
, "MICBIAS");
115 snd_soc_dapm_force_enable_pin(dapm
, "SHDN");
117 snd_soc_dapm_disable_pin(dapm
, "MICBIAS");
118 snd_soc_dapm_disable_pin(dapm
, "SHDN");
121 snd_soc_dapm_sync(dapm
);
126 static struct notifier_block rk_jack_nb
= {
127 .notifier_call
= rk_jack_event
,
130 static int rk_init(struct snd_soc_pcm_runtime
*runtime
)
133 * The jack has already been created in the rk_98090_headset_init()
136 snd_soc_jack_notifier_register(&headset_jack
, &rk_jack_nb
);
141 static int rk_aif1_hw_params(struct snd_pcm_substream
*substream
,
142 struct snd_pcm_hw_params
*params
)
145 struct snd_soc_pcm_runtime
*rtd
= snd_soc_substream_to_rtd(substream
);
146 struct snd_soc_dai
*cpu_dai
= snd_soc_rtd_to_cpu(rtd
, 0);
147 struct snd_soc_dai
*codec_dai
= snd_soc_rtd_to_codec(rtd
, 0);
150 switch (params_rate(params
)) {
170 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
173 dev_err(cpu_dai
->dev
, "Can't set cpu dai clock %d\n", ret
);
177 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
180 /* HDMI codec dai does not need to set sysclk. */
181 if (!strcmp(rtd
->dai_link
->name
, "HDMI"))
185 dev_err(codec_dai
->dev
, "Can't set codec dai clock %d\n", ret
);
192 static int rk_aif1_startup(struct snd_pcm_substream
*substream
)
195 * Set period size to 240 because pl330 has issue
196 * dealing with larger period in stress testing.
198 return snd_pcm_hw_constraint_minmax(substream
->runtime
,
199 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, 240, 240);
202 static const struct snd_soc_ops rk_aif1_ops
= {
203 .hw_params
= rk_aif1_hw_params
,
204 .startup
= rk_aif1_startup
,
207 SND_SOC_DAILINK_DEFS(analog
,
208 DAILINK_COMP_ARRAY(COMP_EMPTY()),
209 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "HiFi")),
210 DAILINK_COMP_ARRAY(COMP_EMPTY()));
212 SND_SOC_DAILINK_DEFS(hdmi
,
213 DAILINK_COMP_ARRAY(COMP_EMPTY()),
214 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "i2s-hifi")),
215 DAILINK_COMP_ARRAY(COMP_EMPTY()));
222 static struct snd_soc_jack rk_hdmi_jack
;
224 static int rk_hdmi_init(struct snd_soc_pcm_runtime
*runtime
)
226 struct snd_soc_card
*card
= runtime
->card
;
227 struct snd_soc_component
*component
= snd_soc_rtd_to_codec(runtime
, 0)->component
;
230 /* enable jack detection */
231 ret
= snd_soc_card_jack_new(card
, "HDMI Jack", SND_JACK_LINEOUT
,
234 dev_err(card
->dev
, "Can't new HDMI Jack %d\n", ret
);
238 return snd_soc_component_set_jack(component
, &rk_hdmi_jack
, NULL
);
241 /* max98090 dai_link */
242 static struct snd_soc_dai_link rk_max98090_dailinks
[] = {
245 .stream_name
= "Analog",
248 /* set max98090 as slave */
249 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
250 SND_SOC_DAIFMT_CBS_CFS
,
251 SND_SOC_DAILINK_REG(analog
),
255 /* HDMI codec dai_link */
256 static struct snd_soc_dai_link rk_hdmi_dailinks
[] = {
259 .stream_name
= "HDMI",
260 .init
= rk_hdmi_init
,
262 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
263 SND_SOC_DAIFMT_CBS_CFS
,
264 SND_SOC_DAILINK_REG(hdmi
),
268 /* max98090 and HDMI codec dai_link */
269 static struct snd_soc_dai_link rk_max98090_hdmi_dailinks
[] = {
270 [DAILINK_MAX98090
] = {
272 .stream_name
= "Analog",
275 /* set max98090 as slave */
276 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
277 SND_SOC_DAIFMT_CBS_CFS
,
278 SND_SOC_DAILINK_REG(analog
),
282 .stream_name
= "HDMI",
283 .init
= rk_hdmi_init
,
285 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
286 SND_SOC_DAIFMT_CBS_CFS
,
287 SND_SOC_DAILINK_REG(hdmi
),
291 static int rk_98090_headset_init(struct snd_soc_component
*component
);
293 static struct snd_soc_aux_dev rk_98090_headset_dev
= {
295 .init
= rk_98090_headset_init
,
298 static struct snd_soc_card rockchip_max98090_card
= {
299 .name
= "ROCKCHIP-I2S",
300 .owner
= THIS_MODULE
,
301 .dai_link
= rk_max98090_dailinks
,
302 .num_links
= ARRAY_SIZE(rk_max98090_dailinks
),
303 .aux_dev
= &rk_98090_headset_dev
,
305 .dapm_widgets
= rk_max98090_dapm_widgets
,
306 .num_dapm_widgets
= ARRAY_SIZE(rk_max98090_dapm_widgets
),
307 .dapm_routes
= rk_max98090_audio_map
,
308 .num_dapm_routes
= ARRAY_SIZE(rk_max98090_audio_map
),
309 .controls
= rk_max98090_controls
,
310 .num_controls
= ARRAY_SIZE(rk_max98090_controls
),
313 static struct snd_soc_card rockchip_hdmi_card
= {
314 .name
= "ROCKCHIP-HDMI",
315 .owner
= THIS_MODULE
,
316 .dai_link
= rk_hdmi_dailinks
,
317 .num_links
= ARRAY_SIZE(rk_hdmi_dailinks
),
318 .dapm_widgets
= rk_hdmi_dapm_widgets
,
319 .num_dapm_widgets
= ARRAY_SIZE(rk_hdmi_dapm_widgets
),
320 .dapm_routes
= rk_hdmi_audio_map
,
321 .num_dapm_routes
= ARRAY_SIZE(rk_hdmi_audio_map
),
322 .controls
= rk_hdmi_controls
,
323 .num_controls
= ARRAY_SIZE(rk_hdmi_controls
),
326 static struct snd_soc_card rockchip_max98090_hdmi_card
= {
327 .name
= "ROCKCHIP-MAX98090-HDMI",
328 .owner
= THIS_MODULE
,
329 .dai_link
= rk_max98090_hdmi_dailinks
,
330 .num_links
= ARRAY_SIZE(rk_max98090_hdmi_dailinks
),
331 .aux_dev
= &rk_98090_headset_dev
,
333 .dapm_widgets
= rk_max98090_hdmi_dapm_widgets
,
334 .num_dapm_widgets
= ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets
),
335 .dapm_routes
= rk_max98090_hdmi_audio_map
,
336 .num_dapm_routes
= ARRAY_SIZE(rk_max98090_hdmi_audio_map
),
337 .controls
= rk_max98090_hdmi_controls
,
338 .num_controls
= ARRAY_SIZE(rk_max98090_hdmi_controls
),
341 static int rk_98090_headset_init(struct snd_soc_component
*component
)
345 /* Enable Headset and 4 Buttons Jack detection */
346 ret
= snd_soc_card_jack_new_pins(component
->card
, "Headset Jack",
348 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
349 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
352 ARRAY_SIZE(headset_jack_pins
));
356 ret
= ts3a227e_enable_jack_detect(component
, &headset_jack
);
361 static int rk_parse_headset_from_of(struct device
*dev
, struct device_node
*np
)
363 rk_98090_headset_dev
.dlc
.of_node
= of_parse_phandle(
364 np
, "rockchip,headset-codec", 0);
365 if (!rk_98090_headset_dev
.dlc
.of_node
) {
367 "Property 'rockchip,headset-codec' missing/invalid\n");
373 static int snd_rk_mc_probe(struct platform_device
*pdev
)
376 struct snd_soc_card
*card
;
377 struct device
*dev
= &pdev
->dev
;
378 struct device_node
*np
= pdev
->dev
.of_node
;
379 struct device_node
*np_cpu
;
380 struct device_node
*np_audio
, *np_hdmi
;
382 /* Parse DTS for I2S controller. */
383 np_cpu
= of_parse_phandle(np
, "rockchip,i2s-controller", 0);
387 "Property 'rockchip,i2s-controller missing or invalid\n");
392 * Find the card to use based on the presences of audio codec
393 * and hdmi codec in device property. Set their of_node accordingly.
395 np_audio
= of_parse_phandle(np
, "rockchip,audio-codec", 0);
396 np_hdmi
= of_parse_phandle(np
, "rockchip,hdmi-codec", 0);
397 if (np_audio
&& np_hdmi
) {
398 card
= &rockchip_max98090_hdmi_card
;
399 card
->dai_link
[DAILINK_MAX98090
].codecs
->of_node
= np_audio
;
400 card
->dai_link
[DAILINK_HDMI
].codecs
->of_node
= np_hdmi
;
401 card
->dai_link
[DAILINK_MAX98090
].cpus
->of_node
= np_cpu
;
402 card
->dai_link
[DAILINK_MAX98090
].platforms
->of_node
= np_cpu
;
403 card
->dai_link
[DAILINK_HDMI
].cpus
->of_node
= np_cpu
;
404 card
->dai_link
[DAILINK_HDMI
].platforms
->of_node
= np_cpu
;
405 } else if (np_audio
) {
406 card
= &rockchip_max98090_card
;
407 card
->dai_link
[0].codecs
->of_node
= np_audio
;
408 card
->dai_link
[0].cpus
->of_node
= np_cpu
;
409 card
->dai_link
[0].platforms
->of_node
= np_cpu
;
410 } else if (np_hdmi
) {
411 card
= &rockchip_hdmi_card
;
412 card
->dai_link
[0].codecs
->of_node
= np_hdmi
;
413 card
->dai_link
[0].cpus
->of_node
= np_cpu
;
414 card
->dai_link
[0].platforms
->of_node
= np_cpu
;
416 dev_err(dev
, "At least one of codecs should be specified\n");
422 /* Parse headset detection codec. */
424 ret
= rk_parse_headset_from_of(dev
, np
);
429 /* Parse card name. */
430 ret
= snd_soc_of_parse_card_name(card
, "rockchip,model");
433 "Soc parse card name failed %d\n", ret
);
437 /* register the soc card */
438 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
441 "Soc register card failed %d\n", ret
);
448 static const struct of_device_id rockchip_max98090_of_match
[] = {
449 { .compatible
= "rockchip,rockchip-audio-max98090", },
453 MODULE_DEVICE_TABLE(of
, rockchip_max98090_of_match
);
455 static struct platform_driver snd_rk_mc_driver
= {
456 .probe
= snd_rk_mc_probe
,
459 .pm
= &snd_soc_pm_ops
,
460 .of_match_table
= rockchip_max98090_of_match
,
464 module_platform_driver(snd_rk_mc_driver
);
466 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
467 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
468 MODULE_LICENSE("GPL v2");
469 MODULE_ALIAS("platform:" DRV_NAME
);