xref: /openbmc/linux/sound/soc/tegra/tegra_wm8903.c (revision c6fddb28)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
4  *
5  * Author: Stephen Warren <swarren@nvidia.com>
6  * Copyright (C) 2010-2012 - NVIDIA, Inc.
7  *
8  * Based on code copyright/by:
9  *
10  * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
11  *
12  * Copyright 2007 Wolfson Microelectronics PLC.
13  * Author: Graeme Gregory
14  *         graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
15  */
16 
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/gpio.h>
21 #include <linux/of_gpio.h>
22 
23 #include <sound/core.h>
24 #include <sound/jack.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 
29 #include "../codecs/wm8903.h"
30 
31 #include "tegra_asoc_utils.h"
32 
33 #define DRV_NAME "tegra-snd-wm8903"
34 
35 struct tegra_wm8903 {
36 	int gpio_spkr_en;
37 	int gpio_hp_det;
38 	int gpio_hp_mute;
39 	int gpio_int_mic_en;
40 	int gpio_ext_mic_en;
41 	struct tegra_asoc_utils_data util_data;
42 };
43 
44 static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
45 					struct snd_pcm_hw_params *params)
46 {
47 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
48 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
49 	struct snd_soc_card *card = rtd->card;
50 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
51 	int srate, mclk;
52 	int err;
53 
54 	srate = params_rate(params);
55 	switch (srate) {
56 	case 64000:
57 	case 88200:
58 	case 96000:
59 		mclk = 128 * srate;
60 		break;
61 	default:
62 		mclk = 256 * srate;
63 		break;
64 	}
65 	/* FIXME: Codec only requires >= 3MHz if OSR==0 */
66 	while (mclk < 6000000)
67 		mclk *= 2;
68 
69 	err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
70 	if (err < 0) {
71 		dev_err(card->dev, "Can't configure clocks\n");
72 		return err;
73 	}
74 
75 	err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
76 					SND_SOC_CLOCK_IN);
77 	if (err < 0) {
78 		dev_err(card->dev, "codec_dai clock not set\n");
79 		return err;
80 	}
81 
82 	return 0;
83 }
84 
85 static const struct snd_soc_ops tegra_wm8903_ops = {
86 	.hw_params = tegra_wm8903_hw_params,
87 };
88 
89 static struct snd_soc_jack tegra_wm8903_hp_jack;
90 
91 static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
92 	{
93 		.pin = "Headphone Jack",
94 		.mask = SND_JACK_HEADPHONE,
95 	},
96 };
97 
98 static struct snd_soc_jack_gpio tegra_wm8903_hp_jack_gpio = {
99 	.name = "headphone detect",
100 	.report = SND_JACK_HEADPHONE,
101 	.debounce_time = 150,
102 	.invert = 1,
103 };
104 
105 static struct snd_soc_jack tegra_wm8903_mic_jack;
106 
107 static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
108 	{
109 		.pin = "Mic Jack",
110 		.mask = SND_JACK_MICROPHONE,
111 	},
112 };
113 
114 static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
115 					struct snd_kcontrol *k, int event)
116 {
117 	struct snd_soc_dapm_context *dapm = w->dapm;
118 	struct snd_soc_card *card = dapm->card;
119 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
120 
121 	if (!gpio_is_valid(machine->gpio_spkr_en))
122 		return 0;
123 
124 	gpio_set_value_cansleep(machine->gpio_spkr_en,
125 				SND_SOC_DAPM_EVENT_ON(event));
126 
127 	return 0;
128 }
129 
130 static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
131 					struct snd_kcontrol *k, int event)
132 {
133 	struct snd_soc_dapm_context *dapm = w->dapm;
134 	struct snd_soc_card *card = dapm->card;
135 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
136 
137 	if (!gpio_is_valid(machine->gpio_hp_mute))
138 		return 0;
139 
140 	gpio_set_value_cansleep(machine->gpio_hp_mute,
141 				!SND_SOC_DAPM_EVENT_ON(event));
142 
143 	return 0;
144 }
145 
146 static int tegra_wm8903_event_int_mic(struct snd_soc_dapm_widget *w,
147 				      struct snd_kcontrol *k, int event)
148 {
149 	struct snd_soc_dapm_context *dapm = w->dapm;
150 	struct snd_soc_card *card = dapm->card;
151 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
152 
153 	if (!gpio_is_valid(machine->gpio_int_mic_en))
154 		return 0;
155 
156 	gpio_set_value_cansleep(machine->gpio_int_mic_en,
157 				SND_SOC_DAPM_EVENT_ON(event));
158 
159 	return 0;
160 }
161 
162 static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
163 	SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
164 	SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
165 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
166 	SND_SOC_DAPM_MIC("Int Mic", tegra_wm8903_event_int_mic),
167 };
168 
169 static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
170 	SOC_DAPM_PIN_SWITCH("Int Spk"),
171 	SOC_DAPM_PIN_SWITCH("Int Mic"),
172 };
173 
174 static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
175 {
176 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
177 	struct snd_soc_component *component = codec_dai->component;
178 	struct snd_soc_card *card = rtd->card;
179 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
180 
181 	if (gpio_is_valid(machine->gpio_hp_det)) {
182 		tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
183 		snd_soc_card_jack_new(rtd->card, "Headphone Jack",
184 				      SND_JACK_HEADPHONE, &tegra_wm8903_hp_jack,
185 				      tegra_wm8903_hp_jack_pins,
186 				      ARRAY_SIZE(tegra_wm8903_hp_jack_pins));
187 		snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
188 					1,
189 					&tegra_wm8903_hp_jack_gpio);
190 	}
191 
192 	snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
193 			      &tegra_wm8903_mic_jack,
194 			      tegra_wm8903_mic_jack_pins,
195 			      ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
196 	wm8903_mic_detect(component, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
197 				0);
198 
199 	snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
200 
201 	return 0;
202 }
203 
204 static int tegra_wm8903_remove(struct snd_soc_card *card)
205 {
206 	struct snd_soc_pcm_runtime *rtd =
207 		snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
208 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
209 	struct snd_soc_component *component = codec_dai->component;
210 
211 	wm8903_mic_detect(component, NULL, 0, 0);
212 
213 	return 0;
214 }
215 
216 SND_SOC_DAILINK_DEFS(hifi,
217 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
218 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8903-hifi")),
219 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
220 
221 static struct snd_soc_dai_link tegra_wm8903_dai = {
222 	.name = "WM8903",
223 	.stream_name = "WM8903 PCM",
224 	.init = tegra_wm8903_init,
225 	.ops = &tegra_wm8903_ops,
226 	.dai_fmt = SND_SOC_DAIFMT_I2S |
227 		   SND_SOC_DAIFMT_NB_NF |
228 		   SND_SOC_DAIFMT_CBS_CFS,
229 	SND_SOC_DAILINK_REG(hifi),
230 };
231 
232 static struct snd_soc_card snd_soc_tegra_wm8903 = {
233 	.name = "tegra-wm8903",
234 	.owner = THIS_MODULE,
235 	.dai_link = &tegra_wm8903_dai,
236 	.num_links = 1,
237 	.remove = tegra_wm8903_remove,
238 	.controls = tegra_wm8903_controls,
239 	.num_controls = ARRAY_SIZE(tegra_wm8903_controls),
240 	.dapm_widgets = tegra_wm8903_dapm_widgets,
241 	.num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
242 	.fully_routed = true,
243 };
244 
245 static int tegra_wm8903_driver_probe(struct platform_device *pdev)
246 {
247 	struct device_node *np = pdev->dev.of_node;
248 	struct snd_soc_card *card = &snd_soc_tegra_wm8903;
249 	struct tegra_wm8903 *machine;
250 	int ret;
251 
252 	machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
253 			       GFP_KERNEL);
254 	if (!machine)
255 		return -ENOMEM;
256 
257 	card->dev = &pdev->dev;
258 	snd_soc_card_set_drvdata(card, machine);
259 
260 	machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
261 						  0);
262 	if (machine->gpio_spkr_en == -EPROBE_DEFER)
263 		return -EPROBE_DEFER;
264 	if (gpio_is_valid(machine->gpio_spkr_en)) {
265 		ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
266 					    GPIOF_OUT_INIT_LOW, "spkr_en");
267 		if (ret) {
268 			dev_err(card->dev, "cannot get spkr_en gpio\n");
269 			return ret;
270 		}
271 	}
272 
273 	machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
274 						  0);
275 	if (machine->gpio_hp_mute == -EPROBE_DEFER)
276 		return -EPROBE_DEFER;
277 	if (gpio_is_valid(machine->gpio_hp_mute)) {
278 		ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
279 					    GPIOF_OUT_INIT_HIGH, "hp_mute");
280 		if (ret) {
281 			dev_err(card->dev, "cannot get hp_mute gpio\n");
282 			return ret;
283 		}
284 	}
285 
286 	machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
287 	if (machine->gpio_hp_det == -EPROBE_DEFER)
288 		return -EPROBE_DEFER;
289 
290 	machine->gpio_int_mic_en = of_get_named_gpio(np,
291 						"nvidia,int-mic-en-gpios", 0);
292 	if (machine->gpio_int_mic_en == -EPROBE_DEFER)
293 		return -EPROBE_DEFER;
294 	if (gpio_is_valid(machine->gpio_int_mic_en)) {
295 		/* Disable int mic; enable signal is active-high */
296 		ret = devm_gpio_request_one(&pdev->dev,
297 					    machine->gpio_int_mic_en,
298 					    GPIOF_OUT_INIT_LOW, "int_mic_en");
299 		if (ret) {
300 			dev_err(card->dev, "cannot get int_mic_en gpio\n");
301 			return ret;
302 		}
303 	}
304 
305 	machine->gpio_ext_mic_en = of_get_named_gpio(np,
306 						"nvidia,ext-mic-en-gpios", 0);
307 	if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
308 		return -EPROBE_DEFER;
309 	if (gpio_is_valid(machine->gpio_ext_mic_en)) {
310 		/* Enable ext mic; enable signal is active-low */
311 		ret = devm_gpio_request_one(&pdev->dev,
312 					    machine->gpio_ext_mic_en,
313 					    GPIOF_OUT_INIT_LOW, "ext_mic_en");
314 		if (ret) {
315 			dev_err(card->dev, "cannot get ext_mic_en gpio\n");
316 			return ret;
317 		}
318 	}
319 
320 	ret = snd_soc_of_parse_card_name(card, "nvidia,model");
321 	if (ret)
322 		goto err;
323 
324 	ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
325 	if (ret)
326 		goto err;
327 
328 	tegra_wm8903_dai.codecs->of_node = of_parse_phandle(np,
329 						"nvidia,audio-codec", 0);
330 	if (!tegra_wm8903_dai.codecs->of_node) {
331 		dev_err(&pdev->dev,
332 			"Property 'nvidia,audio-codec' missing or invalid\n");
333 		ret = -EINVAL;
334 		goto err;
335 	}
336 
337 	tegra_wm8903_dai.cpus->of_node = of_parse_phandle(np,
338 			"nvidia,i2s-controller", 0);
339 	if (!tegra_wm8903_dai.cpus->of_node) {
340 		dev_err(&pdev->dev,
341 			"Property 'nvidia,i2s-controller' missing or invalid\n");
342 		ret = -EINVAL;
343 		goto err;
344 	}
345 
346 	tegra_wm8903_dai.platforms->of_node = tegra_wm8903_dai.cpus->of_node;
347 
348 	ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
349 	if (ret)
350 		goto err;
351 
352 	ret = snd_soc_register_card(card);
353 	if (ret) {
354 		dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
355 			ret);
356 		goto err_fini_utils;
357 	}
358 
359 	return 0;
360 
361 err_fini_utils:
362 	tegra_asoc_utils_fini(&machine->util_data);
363 err:
364 	return ret;
365 }
366 
367 static int tegra_wm8903_driver_remove(struct platform_device *pdev)
368 {
369 	struct snd_soc_card *card = platform_get_drvdata(pdev);
370 	struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
371 
372 	snd_soc_unregister_card(card);
373 
374 	tegra_asoc_utils_fini(&machine->util_data);
375 
376 	return 0;
377 }
378 
379 static const struct of_device_id tegra_wm8903_of_match[] = {
380 	{ .compatible = "nvidia,tegra-audio-wm8903", },
381 	{},
382 };
383 
384 static struct platform_driver tegra_wm8903_driver = {
385 	.driver = {
386 		.name = DRV_NAME,
387 		.pm = &snd_soc_pm_ops,
388 		.of_match_table = tegra_wm8903_of_match,
389 	},
390 	.probe = tegra_wm8903_driver_probe,
391 	.remove = tegra_wm8903_driver_remove,
392 };
393 module_platform_driver(tegra_wm8903_driver);
394 
395 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
396 MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
397 MODULE_LICENSE("GPL");
398 MODULE_ALIAS("platform:" DRV_NAME);
399 MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);
400