xref: /linux/sound/soc/rockchip/rk3288_hdmi_analog.c (revision 52a9dab6d892763b2a8334a568bd4e2c1a6fde66)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Rockchip machine ASoC driver for RK3288 boards that have an HDMI and analog
4  * audio output
5  *
6  * Copyright (c) 2016, Collabora Ltd.
7  *
8  * Authors: Sjoerd Simons <sjoerd.simons@collabora.com>,
9  *	    Romain Perier <romain.perier@collabora.com>
10  */
11 
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>
23 
24 #include "rockchip_i2s.h"
25 
26 #define DRV_NAME "rk3288-snd-hdmi-analog"
27 
28 struct rk_drvdata {
29 	int gpio_hp_en;
30 	int gpio_hp_det;
31 };
32 
33 static int rk_hp_power(struct snd_soc_dapm_widget *w,
34 		       struct snd_kcontrol *k, int event)
35 {
36 	struct rk_drvdata *machine = snd_soc_card_get_drvdata(w->dapm->card);
37 
38 	if (!gpio_is_valid(machine->gpio_hp_en))
39 		return 0;
40 
41 	gpio_set_value_cansleep(machine->gpio_hp_en,
42 				SND_SOC_DAPM_EVENT_ON(event));
43 
44 	return 0;
45 }
46 
47 static struct snd_soc_jack headphone_jack;
48 static struct snd_soc_jack_pin headphone_jack_pins[] = {
49 	{
50 		.pin = "Analog",
51 		.mask = SND_JACK_HEADPHONE
52 	},
53 };
54 
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),
58 };
59 
60 static const struct snd_kcontrol_new rk_mc_controls[] = {
61 	SOC_DAPM_PIN_SWITCH("Analog"),
62 	SOC_DAPM_PIN_SWITCH("HDMI"),
63 };
64 
65 static int rk_hw_params(struct snd_pcm_substream *substream,
66 			struct snd_pcm_hw_params *params)
67 {
68 	int ret = 0;
69 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
70 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
71 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
72 	int mclk;
73 
74 	switch (params_rate(params)) {
75 	case 8000:
76 	case 16000:
77 	case 24000:
78 	case 32000:
79 	case 48000:
80 	case 64000:
81 	case 96000:
82 		mclk = 12288000;
83 		break;
84 	case 192000:
85 		mclk = 24576000;
86 		break;
87 	case 11025:
88 	case 22050:
89 	case 44100:
90 	case 88200:
91 		mclk = 11289600;
92 		break;
93 	default:
94 		return -EINVAL;
95 	}
96 
97 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
98 				     SND_SOC_CLOCK_OUT);
99 
100 	if (ret && ret != -ENOTSUPP) {
101 		dev_err(codec_dai->dev, "Can't set cpu clock %d\n", ret);
102 		return ret;
103 	}
104 
105 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
106 				     SND_SOC_CLOCK_IN);
107 	if (ret && ret != -ENOTSUPP) {
108 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 static struct snd_soc_jack_gpio rk_hp_jack_gpio = {
116 	.name = "Headphone detection",
117 	.report = SND_JACK_HEADPHONE,
118 	.debounce_time = 150
119 };
120 
121 static int rk_init(struct snd_soc_pcm_runtime *runtime)
122 {
123 	struct rk_drvdata *machine = snd_soc_card_get_drvdata(runtime->card);
124 
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,
129 				      headphone_jack_pins,
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);
133 	}
134 
135 	return 0;
136 }
137 
138 static const struct snd_soc_ops rk_ops = {
139 	.hw_params = rk_hw_params,
140 };
141 
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()));
147 
148 static struct snd_soc_dai_link rk_dailink = {
149 	.name = "Codecs",
150 	.stream_name = "Audio",
151 	.init = rk_init,
152 	.ops = &rk_ops,
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),
157 };
158 
159 static struct snd_soc_card snd_soc_card_rk = {
160 	.name = "ROCKCHIP-I2S",
161 	.dai_link = &rk_dailink,
162 	.num_links = 1,
163 	.num_aux_devs = 0,
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),
168 };
169 
170 static int snd_rk_mc_probe(struct platform_device *pdev)
171 {
172 	int ret = 0;
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;
177 
178 	machine = devm_kzalloc(&pdev->dev, sizeof(struct rk_drvdata),
179 			       GFP_KERNEL);
180 	if (!machine)
181 		return -ENOMEM;
182 
183 	card->dev = &pdev->dev;
184 
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;
189 
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;
194 
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");
198 		if (ret) {
199 			dev_err(card->dev, "cannot get hp_en gpio\n");
200 			return ret;
201 		}
202 	}
203 
204 	ret = snd_soc_of_parse_card_name(card, "rockchip,model");
205 	if (ret) {
206 		dev_err(card->dev, "SoC parse card name failed %d\n", ret);
207 		return ret;
208 	}
209 
210 	rk_dailink.codecs[0].of_node = of_parse_phandle(np,
211 							"rockchip,audio-codec",
212 							0);
213 	if (!rk_dailink.codecs[0].of_node) {
214 		dev_err(&pdev->dev,
215 			"Property 'rockchip,audio-codec' missing or invalid\n");
216 		return -EINVAL;
217 	}
218 	ret = of_parse_phandle_with_fixed_args(np, "rockchip,audio-codec",
219 					       0, 0, &args);
220 	if (ret) {
221 		dev_err(&pdev->dev,
222 			"Unable to parse property 'rockchip,audio-codec'\n");
223 		return ret;
224 	}
225 
226 	ret = snd_soc_get_dai_name(&args, &rk_dailink.codecs[0].dai_name);
227 	if (ret) {
228 		dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
229 		return ret;
230 	}
231 
232 	rk_dailink.cpus->of_node = of_parse_phandle(np, "rockchip,i2s-controller",
233 						  0);
234 	if (!rk_dailink.cpus->of_node) {
235 		dev_err(&pdev->dev,
236 			"Property 'rockchip,i2s-controller' missing or invalid\n");
237 		return -EINVAL;
238 	}
239 
240 	rk_dailink.platforms->of_node = rk_dailink.cpus->of_node;
241 
242 	ret = snd_soc_of_parse_audio_routing(card, "rockchip,routing");
243 	if (ret) {
244 		dev_err(&pdev->dev,
245 			"Unable to parse 'rockchip,routing' property\n");
246 		return ret;
247 	}
248 
249 	snd_soc_card_set_drvdata(card, machine);
250 
251 	ret = devm_snd_soc_register_card(&pdev->dev, card);
252 	if (ret)
253 		return dev_err_probe(&pdev->dev, ret,
254 				     "Soc register card failed\n");
255 
256 	return ret;
257 }
258 
259 static const struct of_device_id rockchip_sound_of_match[] = {
260 	{ .compatible = "rockchip,rk3288-hdmi-analog", },
261 	{},
262 };
263 
264 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
265 
266 static struct platform_driver rockchip_sound_driver = {
267 	.probe = snd_rk_mc_probe,
268 	.driver = {
269 		.name = DRV_NAME,
270 		.pm = &snd_soc_pm_ops,
271 		.of_match_table = rockchip_sound_of_match,
272 	},
273 };
274 
275 module_platform_driver(rockchip_sound_driver);
276 
277 MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.com>");
278 MODULE_DESCRIPTION("Rockchip RK3288 machine ASoC driver");
279 MODULE_LICENSE("GPL v2");
280 MODULE_ALIAS("platform:" DRV_NAME);
281