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