2 * Rockchip machine ASoC driver for RK3288 boards that have an HDMI and analog
5 * Copyright (c) 2016, Collabora Ltd.
7 * Authors: Sjoerd Simons <sjoerd.simons@collabora.com>,
8 * Romain Perier <romain.perier@collabora.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/of_gpio.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
36 #include "rockchip_i2s.h"
38 #define DRV_NAME "rk3288-snd-hdmi-analog"
45 static int rk_hp_power(struct snd_soc_dapm_widget
*w
,
46 struct snd_kcontrol
*k
, int event
)
48 struct rk_drvdata
*machine
= snd_soc_card_get_drvdata(w
->dapm
->card
);
50 if (!gpio_is_valid(machine
->gpio_hp_en
))
53 gpio_set_value_cansleep(machine
->gpio_hp_en
,
54 SND_SOC_DAPM_EVENT_ON(event
));
59 static struct snd_soc_jack headphone_jack
;
60 static struct snd_soc_jack_pin headphone_jack_pins
[] = {
63 .mask
= SND_JACK_HEADPHONE
67 static const struct snd_soc_dapm_widget rk_dapm_widgets
[] = {
68 SND_SOC_DAPM_HP("Analog", rk_hp_power
),
69 SND_SOC_DAPM_LINE("HDMI", NULL
),
72 static const struct snd_kcontrol_new rk_mc_controls
[] = {
73 SOC_DAPM_PIN_SWITCH("Analog"),
74 SOC_DAPM_PIN_SWITCH("HDMI"),
77 static int rk_hw_params(struct snd_pcm_substream
*substream
,
78 struct snd_pcm_hw_params
*params
)
81 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
82 struct snd_soc_dai
*cpu_dai
= rtd
->cpu_dai
;
83 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
86 switch (params_rate(params
)) {
109 ret
= snd_soc_dai_set_sysclk(cpu_dai
, 0, mclk
,
112 if (ret
&& ret
!= -ENOTSUPP
) {
113 dev_err(codec_dai
->dev
, "Can't set cpu clock %d\n", ret
);
117 ret
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
119 if (ret
&& ret
!= -ENOTSUPP
) {
120 dev_err(codec_dai
->dev
, "Can't set codec clock %d\n", ret
);
127 static struct snd_soc_jack_gpio rk_hp_jack_gpio
= {
128 .name
= "Headphone detection",
129 .report
= SND_JACK_HEADPHONE
,
133 static int rk_init(struct snd_soc_pcm_runtime
*runtime
)
135 struct rk_drvdata
*machine
= snd_soc_card_get_drvdata(runtime
->card
);
137 /* Enable Headset Jack detection */
138 if (gpio_is_valid(machine
->gpio_hp_det
)) {
139 snd_soc_card_jack_new(runtime
->card
, "Headphone Jack",
140 SND_JACK_HEADPHONE
, &headphone_jack
,
142 ARRAY_SIZE(headphone_jack_pins
));
143 rk_hp_jack_gpio
.gpio
= machine
->gpio_hp_det
;
144 snd_soc_jack_add_gpios(&headphone_jack
, 1, &rk_hp_jack_gpio
);
150 static const struct snd_soc_ops rk_ops
= {
151 .hw_params
= rk_hw_params
,
154 static struct snd_soc_dai_link_component rk_codecs
[] = {
157 .name
= "hdmi-audio-codec.2.auto",
158 .dai_name
= "hdmi-hifi.0",
162 static struct snd_soc_dai_link rk_dailink
= {
164 .stream_name
= "Audio",
168 .num_codecs
= ARRAY_SIZE(rk_codecs
),
169 /* Set codecs as slave */
170 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
171 SND_SOC_DAIFMT_CBS_CFS
,
174 static struct snd_soc_card snd_soc_card_rk
= {
175 .name
= "ROCKCHIP-I2S",
176 .dai_link
= &rk_dailink
,
179 .dapm_widgets
= rk_dapm_widgets
,
180 .num_dapm_widgets
= ARRAY_SIZE(rk_dapm_widgets
),
181 .controls
= rk_mc_controls
,
182 .num_controls
= ARRAY_SIZE(rk_mc_controls
),
185 static int snd_rk_mc_probe(struct platform_device
*pdev
)
188 struct snd_soc_card
*card
= &snd_soc_card_rk
;
189 struct device_node
*np
= pdev
->dev
.of_node
;
190 struct rk_drvdata
*machine
;
191 struct of_phandle_args args
;
193 machine
= devm_kzalloc(&pdev
->dev
, sizeof(struct rk_drvdata
),
198 card
->dev
= &pdev
->dev
;
200 machine
->gpio_hp_det
= of_get_named_gpio(np
,
201 "rockchip,hp-det-gpios", 0);
202 if (!gpio_is_valid(machine
->gpio_hp_det
) && machine
->gpio_hp_det
!= -ENODEV
)
203 return machine
->gpio_hp_det
;
205 machine
->gpio_hp_en
= of_get_named_gpio(np
,
206 "rockchip,hp-en-gpios", 0);
207 if (!gpio_is_valid(machine
->gpio_hp_en
) && machine
->gpio_hp_en
!= -ENODEV
)
208 return machine
->gpio_hp_en
;
210 if (gpio_is_valid(machine
->gpio_hp_en
)) {
211 ret
= devm_gpio_request_one(&pdev
->dev
, machine
->gpio_hp_en
,
212 GPIOF_OUT_INIT_LOW
, "hp_en");
214 dev_err(card
->dev
, "cannot get hp_en gpio\n");
219 ret
= snd_soc_of_parse_card_name(card
, "rockchip,model");
221 dev_err(card
->dev
, "SoC parse card name failed %d\n", ret
);
225 rk_dailink
.codecs
[0].of_node
= of_parse_phandle(np
,
226 "rockchip,audio-codec",
228 if (!rk_dailink
.codecs
[0].of_node
) {
230 "Property 'rockchip,audio-codec' missing or invalid\n");
233 ret
= of_parse_phandle_with_fixed_args(np
, "rockchip,audio-codec",
237 "Unable to parse property 'rockchip,audio-codec'\n");
241 ret
= snd_soc_get_dai_name(&args
, &rk_dailink
.codecs
[0].dai_name
);
243 dev_err(&pdev
->dev
, "Unable to get codec_dai_name\n");
247 rk_dailink
.cpu_of_node
= of_parse_phandle(np
, "rockchip,i2s-controller",
249 if (!rk_dailink
.cpu_of_node
) {
251 "Property 'rockchip,i2s-controller' missing or invalid\n");
255 rk_dailink
.platform_of_node
= rk_dailink
.cpu_of_node
;
257 ret
= snd_soc_of_parse_audio_routing(card
, "rockchip,routing");
260 "Unable to parse 'rockchip,routing' property\n");
264 snd_soc_card_set_drvdata(card
, machine
);
266 ret
= devm_snd_soc_register_card(&pdev
->dev
, card
);
267 if (ret
== -EPROBE_DEFER
)
268 return -EPROBE_DEFER
;
271 "Soc register card failed %d\n", ret
);
278 static const struct of_device_id rockchip_sound_of_match
[] = {
279 { .compatible
= "rockchip,rk3288-hdmi-analog", },
283 MODULE_DEVICE_TABLE(of
, rockchip_sound_of_match
);
285 static struct platform_driver rockchip_sound_driver
= {
286 .probe
= snd_rk_mc_probe
,
289 .owner
= THIS_MODULE
,
290 .pm
= &snd_soc_pm_ops
,
291 .of_match_table
= rockchip_sound_of_match
,
295 module_platform_driver(rockchip_sound_driver
);
297 MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.com>");
298 MODULE_DESCRIPTION("Rockchip RK3288 machine ASoC driver");
299 MODULE_LICENSE("GPL v2");
300 MODULE_ALIAS("platform:" DRV_NAME
);