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/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
19 #include "rockchip_i2s.h"
20 #include "../codecs/ts3a227e.h"
22 #define DRV_NAME "rockchip-snd-max98090"
24 static struct snd_soc_jack headset_jack
;
26 /* Headset jack detection DAPM pins */
27 static struct snd_soc_jack_pin headset_jack_pins
[] = {
30 .mask
= SND_JACK_HEADPHONE
,
34 .mask
= SND_JACK_MICROPHONE
,
39 static const struct snd_soc_dapm_widget rk_dapm_widgets
[] = {
40 SND_SOC_DAPM_HP("Headphone", NULL
),
41 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
42 SND_SOC_DAPM_MIC("Int Mic", NULL
),
43 SND_SOC_DAPM_SPK("Speaker", NULL
),
46 static const struct snd_soc_dapm_route rk_audio_map
[] = {
47 {"IN34", NULL
, "Headset Mic"},
48 {"Headset Mic", NULL
, "MICBIAS"},
49 {"DMICL", NULL
, "Int Mic"},
50 {"Headphone", NULL
, "HPL"},
51 {"Headphone", NULL
, "HPR"},
52 {"Speaker", NULL
, "SPKL"},
53 {"Speaker", NULL
, "SPKR"},
56 static const struct snd_kcontrol_new rk_mc_controls
[] = {
57 SOC_DAPM_PIN_SWITCH("Headphone"),
58 SOC_DAPM_PIN_SWITCH("Headset Mic"),
59 SOC_DAPM_PIN_SWITCH("Int Mic"),
60 SOC_DAPM_PIN_SWITCH("Speaker"),
63 static int rk_jack_event(struct notifier_block
*nb
, unsigned long event
,
66 struct snd_soc_jack
*jack
= (struct snd_soc_jack
*)data
;
67 struct snd_soc_dapm_context
*dapm
= &jack
->card
->dapm
;
69 if (event
& SND_JACK_MICROPHONE
) {
70 snd_soc_dapm_force_enable_pin(dapm
, "MICBIAS");
71 snd_soc_dapm_force_enable_pin(dapm
, "SHDN");
73 snd_soc_dapm_disable_pin(dapm
, "MICBIAS");
74 snd_soc_dapm_disable_pin(dapm
, "SHDN");
77 snd_soc_dapm_sync(dapm
);
82 static struct notifier_block rk_jack_nb
= {
83 .notifier_call
= rk_jack_event
,
86 static int rk_init(struct snd_soc_pcm_runtime
*runtime
)
89 * The jack has already been created in the rk_98090_headset_init()
92 snd_soc_jack_notifier_register(&headset_jack
, &rk_jack_nb
);
97 static int rk_aif1_hw_params(struct snd_pcm_substream
*substream
,
98 struct snd_pcm_hw_params
*params
)
101 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
102 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
103 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
106 switch (params_rate(params
)) {
126 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
129 dev_err(codec_dai
->dev
, "Can't set codec clock %d\n", ret
);
133 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
136 dev_err(codec_dai
->dev
, "Can't set codec clock %d\n", ret
);
143 static int rk_aif1_startup(struct snd_pcm_substream
*substream
)
146 * Set period size to 240 because pl330 has issue
147 * dealing with larger period in stress testing.
149 return snd_pcm_hw_constraint_minmax(substream
->runtime
,
150 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, 240, 240);
153 static const struct snd_soc_ops rk_aif1_ops
= {
154 .hw_params
= rk_aif1_hw_params
,
155 .startup
= rk_aif1_startup
,
158 SND_SOC_DAILINK_DEFS(hifi
,
159 DAILINK_COMP_ARRAY(COMP_EMPTY()),
160 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "HiFi")),
161 DAILINK_COMP_ARRAY(COMP_EMPTY()));
163 static struct snd_soc_dai_link rk_dailink
= {
165 .stream_name
= "Audio",
168 /* set max98090 as slave */
169 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
170 SND_SOC_DAIFMT_CBS_CFS
,
171 SND_SOC_DAILINK_REG(hifi
),
174 static int rk_98090_headset_init(struct snd_soc_component
*component
);
176 static struct snd_soc_aux_dev rk_98090_headset_dev
= {
178 .init
= rk_98090_headset_init
,
181 static struct snd_soc_card snd_soc_card_rk
= {
182 .name
= "ROCKCHIP-I2S",
183 .owner
= THIS_MODULE
,
184 .dai_link
= &rk_dailink
,
186 .aux_dev
= &rk_98090_headset_dev
,
188 .dapm_widgets
= rk_dapm_widgets
,
189 .num_dapm_widgets
= ARRAY_SIZE(rk_dapm_widgets
),
190 .dapm_routes
= rk_audio_map
,
191 .num_dapm_routes
= ARRAY_SIZE(rk_audio_map
),
192 .controls
= rk_mc_controls
,
193 .num_controls
= ARRAY_SIZE(rk_mc_controls
),
196 static int rk_98090_headset_init(struct snd_soc_component
*component
)
200 /* Enable Headset and 4 Buttons Jack detection */
201 ret
= snd_soc_card_jack_new(&snd_soc_card_rk
, "Headset Jack",
203 SND_JACK_BTN_0
| SND_JACK_BTN_1
|
204 SND_JACK_BTN_2
| SND_JACK_BTN_3
,
207 ARRAY_SIZE(headset_jack_pins
));
211 ret
= ts3a227e_enable_jack_detect(component
, &headset_jack
);
216 static int snd_rk_mc_probe(struct platform_device
*pdev
)
219 struct snd_soc_card
*card
= &snd_soc_card_rk
;
220 struct device_node
*np
= pdev
->dev
.of_node
;
222 /* register the soc card */
223 card
->dev
= &pdev
->dev
;
225 rk_dailink
.codecs
->of_node
= of_parse_phandle(np
,
226 "rockchip,audio-codec", 0);
227 if (!rk_dailink
.codecs
->of_node
) {
229 "Property 'rockchip,audio-codec' missing or invalid\n");
233 rk_dailink
.cpus
->of_node
= of_parse_phandle(np
,
234 "rockchip,i2s-controller", 0);
235 if (!rk_dailink
.cpus
->of_node
) {
237 "Property 'rockchip,i2s-controller' missing or invalid\n");
241 rk_dailink
.platforms
->of_node
= rk_dailink
.cpus
->of_node
;
243 rk_98090_headset_dev
.dlc
.of_node
= of_parse_phandle(np
,
244 "rockchip,headset-codec", 0);
245 if (!rk_98090_headset_dev
.dlc
.of_node
) {
247 "Property 'rockchip,headset-codec' missing/invalid\n");
251 ret
= snd_soc_of_parse_card_name(card
, "rockchip,model");
254 "Soc parse card name failed %d\n", ret
);
258 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
261 "Soc register card failed %d\n", ret
);
268 static const struct of_device_id rockchip_max98090_of_match
[] = {
269 { .compatible
= "rockchip,rockchip-audio-max98090", },
273 MODULE_DEVICE_TABLE(of
, rockchip_max98090_of_match
);
275 static struct platform_driver snd_rk_mc_driver
= {
276 .probe
= snd_rk_mc_probe
,
279 .pm
= &snd_soc_pm_ops
,
280 .of_match_table
= rockchip_max98090_of_match
,
284 module_platform_driver(snd_rk_mc_driver
);
286 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
287 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
288 MODULE_LICENSE("GPL v2");
289 MODULE_ALIAS("platform:" DRV_NAME
);