1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip machine ASoC driver for RK3288 boards that have an HDMI and analog
6 * Copyright (c) 2016, Collabora Ltd.
8 * Authors: Sjoerd Simons <sjoerd.simons@collabora.com>,
9 * Romain Perier <romain.perier@collabora.com>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/gpio.h>
16 #include <linux/of_gpio.h>
17 #include <sound/core.h>
18 #include <sound/jack.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
24 #include "rockchip_i2s.h"
26 #define DRV_NAME "rk3288-snd-hdmi-analog"
33 static int rk_hp_power(struct snd_soc_dapm_widget
*w
,
34 struct snd_kcontrol
*k
, int event
)
36 struct rk_drvdata
*machine
= snd_soc_card_get_drvdata(w
->dapm
->card
);
38 if (!gpio_is_valid(machine
->gpio_hp_en
))
41 gpio_set_value_cansleep(machine
->gpio_hp_en
,
42 SND_SOC_DAPM_EVENT_ON(event
));
47 static struct snd_soc_jack headphone_jack
;
48 static struct snd_soc_jack_pin headphone_jack_pins
[] = {
51 .mask
= SND_JACK_HEADPHONE
55 static const struct snd_soc_dapm_widget rk_dapm_widgets
[] = {
56 SND_SOC_DAPM_HP("Analog", rk_hp_power
),
57 SND_SOC_DAPM_LINE("HDMI", NULL
),
60 static const struct snd_kcontrol_new rk_mc_controls
[] = {
61 SOC_DAPM_PIN_SWITCH("Analog"),
62 SOC_DAPM_PIN_SWITCH("HDMI"),
65 static int rk_hw_params(struct snd_pcm_substream
*substream
,
66 struct snd_pcm_hw_params
*params
)
69 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
70 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
71 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
74 switch (params_rate(params
)) {
97 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
100 if (ret
&& ret
!= -ENOTSUPP
) {
101 dev_err(codec_dai
->dev
, "Can't set cpu clock %d\n", ret
);
105 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
107 if (ret
&& ret
!= -ENOTSUPP
) {
108 dev_err(codec_dai
->dev
, "Can't set codec clock %d\n", ret
);
115 static struct snd_soc_jack_gpio rk_hp_jack_gpio
= {
116 .name
= "Headphone detection",
117 .report
= SND_JACK_HEADPHONE
,
121 static int rk_init(struct snd_soc_pcm_runtime
*runtime
)
123 struct rk_drvdata
*machine
= snd_soc_card_get_drvdata(runtime
->card
);
125 /* Enable Headset Jack detection */
126 if (gpio_is_valid(machine
->gpio_hp_det
)) {
127 snd_soc_card_jack_new(runtime
->card
, "Headphone Jack",
128 SND_JACK_HEADPHONE
, &headphone_jack
,
130 ARRAY_SIZE(headphone_jack_pins
));
131 rk_hp_jack_gpio
.gpio
= machine
->gpio_hp_det
;
132 snd_soc_jack_add_gpios(&headphone_jack
, 1, &rk_hp_jack_gpio
);
138 static const struct snd_soc_ops rk_ops
= {
139 .hw_params
= rk_hw_params
,
142 SND_SOC_DAILINK_DEFS(audio
,
143 DAILINK_COMP_ARRAY(COMP_EMPTY()),
144 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, NULL
),
145 COMP_CODEC("hdmi-audio-codec.2.auto", "i2s-hifi")),
146 DAILINK_COMP_ARRAY(COMP_EMPTY()));
148 static struct snd_soc_dai_link rk_dailink
= {
150 .stream_name
= "Audio",
153 /* Set codecs as slave */
154 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
155 SND_SOC_DAIFMT_CBS_CFS
,
156 SND_SOC_DAILINK_REG(audio
),
159 static struct snd_soc_card snd_soc_card_rk
= {
160 .name
= "ROCKCHIP-I2S",
161 .dai_link
= &rk_dailink
,
164 .dapm_widgets
= rk_dapm_widgets
,
165 .num_dapm_widgets
= ARRAY_SIZE(rk_dapm_widgets
),
166 .controls
= rk_mc_controls
,
167 .num_controls
= ARRAY_SIZE(rk_mc_controls
),
170 static int snd_rk_mc_probe(struct platform_device
*pdev
)
173 struct snd_soc_card
*card
= &snd_soc_card_rk
;
174 struct device_node
*np
= pdev
->dev
.of_node
;
175 struct rk_drvdata
*machine
;
176 struct of_phandle_args args
;
178 machine
= devm_kzalloc(&pdev
->dev
, sizeof(struct rk_drvdata
),
183 card
->dev
= &pdev
->dev
;
185 machine
->gpio_hp_det
= of_get_named_gpio(np
,
186 "rockchip,hp-det-gpios", 0);
187 if (!gpio_is_valid(machine
->gpio_hp_det
) && machine
->gpio_hp_det
!= -ENODEV
)
188 return machine
->gpio_hp_det
;
190 machine
->gpio_hp_en
= of_get_named_gpio(np
,
191 "rockchip,hp-en-gpios", 0);
192 if (!gpio_is_valid(machine
->gpio_hp_en
) && machine
->gpio_hp_en
!= -ENODEV
)
193 return machine
->gpio_hp_en
;
195 if (gpio_is_valid(machine
->gpio_hp_en
)) {
196 ret
= devm_gpio_request_one(&pdev
->dev
, machine
->gpio_hp_en
,
197 GPIOF_OUT_INIT_LOW
, "hp_en");
199 dev_err(card
->dev
, "cannot get hp_en gpio\n");
204 ret
= snd_soc_of_parse_card_name(card
, "rockchip,model");
206 dev_err(card
->dev
, "SoC parse card name failed %d\n", ret
);
210 rk_dailink
.codecs
[0].of_node
= of_parse_phandle(np
,
211 "rockchip,audio-codec",
213 if (!rk_dailink
.codecs
[0].of_node
) {
215 "Property 'rockchip,audio-codec' missing or invalid\n");
218 ret
= of_parse_phandle_with_fixed_args(np
, "rockchip,audio-codec",
222 "Unable to parse property 'rockchip,audio-codec'\n");
226 ret
= snd_soc_get_dai_name(&args
, &rk_dailink
.codecs
[0].dai_name
);
228 dev_err(&pdev
->dev
, "Unable to get codec_dai_name\n");
232 rk_dailink
.cpus
->of_node
= of_parse_phandle(np
, "rockchip,i2s-controller",
234 if (!rk_dailink
.cpus
->of_node
) {
236 "Property 'rockchip,i2s-controller' missing or invalid\n");
240 rk_dailink
.platforms
->of_node
= rk_dailink
.cpus
->of_node
;
242 ret
= snd_soc_of_parse_audio_routing(card
, "rockchip,routing");
245 "Unable to parse 'rockchip,routing' property\n");
249 snd_soc_card_set_drvdata(card
, machine
);
251 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
252 if (ret
== -EPROBE_DEFER
)
253 return -EPROBE_DEFER
;
256 "Soc register card failed %d\n", ret
);
263 static const struct of_device_id rockchip_sound_of_match
[] = {
264 { .compatible
= "rockchip,rk3288-hdmi-analog", },
268 MODULE_DEVICE_TABLE(of
, rockchip_sound_of_match
);
270 static struct platform_driver rockchip_sound_driver
= {
271 .probe
= snd_rk_mc_probe
,
274 .pm
= &snd_soc_pm_ops
,
275 .of_match_table
= rockchip_sound_of_match
,
279 module_platform_driver(rockchip_sound_driver
);
281 MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.com>");
282 MODULE_DESCRIPTION("Rockchip RK3288 machine ASoC driver");
283 MODULE_LICENSE("GPL v2");
284 MODULE_ALIAS("platform:" DRV_NAME
);