]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - sound/soc/tegra/tegra_wm8903.c
Merge branch 'timers-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / sound / soc / tegra / tegra_wm8903.c
CommitLineData
2b27bdcc 1// SPDX-License-Identifier: GPL-2.0-only
a8bf1ba1 2/*
dc0a50af 3 * tegra_wm8903.c - Tegra machine ASoC driver for boards using WM8903 codec.
a8bf1ba1
SW
4 *
5 * Author: Stephen Warren <swarren@nvidia.com>
518de86b 6 * Copyright (C) 2010-2012 - NVIDIA, Inc.
a8bf1ba1
SW
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
a8bf1ba1
SW
15 */
16
a8bf1ba1 17#include <linux/module.h>
72de2b1a
SW
18#include <linux/platform_device.h>
19#include <linux/slab.h>
6e267645 20#include <linux/gpio.h>
07cdf36d 21#include <linux/of_gpio.h>
6e267645 22
a8bf1ba1 23#include <sound/core.h>
f7d3e403 24#include <sound/jack.h>
a8bf1ba1
SW
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28
41b5f9b3
SW
29#include "../codecs/wm8903.h"
30
a8bf1ba1
SW
31#include "tegra_asoc_utils.h"
32
7b33af25 33#define DRV_NAME "tegra-snd-wm8903"
a8bf1ba1 34
dc0a50af 35struct tegra_wm8903 {
8f5f5e0f
SW
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;
d64e57ce 41 struct tegra_asoc_utils_data util_data;
72de2b1a 42};
a8bf1ba1 43
dc0a50af 44static int tegra_wm8903_hw_params(struct snd_pcm_substream *substream,
a8bf1ba1
SW
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 = rtd->codec_dai;
093c4e5c 49 struct snd_soc_card *card = rtd->card;
dc0a50af 50 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
07541396 51 int srate, mclk;
a8bf1ba1
SW
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
07541396 69 err = tegra_asoc_utils_set_rate(&machine->util_data, srate, mclk);
a8bf1ba1 70 if (err < 0) {
c244d477 71 dev_err(card->dev, "Can't configure clocks\n");
a8bf1ba1
SW
72 return err;
73 }
74
07541396
SW
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;
a8bf1ba1
SW
80 }
81
82 return 0;
83}
84
8a7a282b 85static const struct snd_soc_ops tegra_wm8903_ops = {
dc0a50af 86 .hw_params = tegra_wm8903_hw_params,
a8bf1ba1
SW
87};
88
dc0a50af 89static struct snd_soc_jack tegra_wm8903_hp_jack;
f7d3e403 90
dc0a50af 91static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
f7d3e403
SW
92 {
93 .pin = "Headphone Jack",
94 .mask = SND_JACK_HEADPHONE,
95 },
96};
97
3eb25f99
SW
98static 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,
f7d3e403
SW
103};
104
dc0a50af 105static struct snd_soc_jack tegra_wm8903_mic_jack;
41b5f9b3 106
dc0a50af 107static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
41b5f9b3
SW
108 {
109 .pin = "Mic Jack",
110 .mask = SND_JACK_MICROPHONE,
111 },
112};
113
dc0a50af 114static int tegra_wm8903_event_int_spk(struct snd_soc_dapm_widget *w,
6e267645
SW
115 struct snd_kcontrol *k, int event)
116{
a32955db
SW
117 struct snd_soc_dapm_context *dapm = w->dapm;
118 struct snd_soc_card *card = dapm->card;
dc0a50af 119 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
6e267645 120
8f5f5e0f 121 if (!gpio_is_valid(machine->gpio_spkr_en))
773b1d3d
SW
122 return 0;
123
8f5f5e0f 124 gpio_set_value_cansleep(machine->gpio_spkr_en,
f9eabc3d 125 SND_SOC_DAPM_EVENT_ON(event));
6e267645
SW
126
127 return 0;
128}
129
773b1d3d
SW
130static int tegra_wm8903_event_hp(struct snd_soc_dapm_widget *w,
131 struct snd_kcontrol *k, int event)
132{
a32955db
SW
133 struct snd_soc_dapm_context *dapm = w->dapm;
134 struct snd_soc_card *card = dapm->card;
773b1d3d 135 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
773b1d3d 136
8f5f5e0f 137 if (!gpio_is_valid(machine->gpio_hp_mute))
773b1d3d
SW
138 return 0;
139
8f5f5e0f 140 gpio_set_value_cansleep(machine->gpio_hp_mute,
773b1d3d
SW
141 !SND_SOC_DAPM_EVENT_ON(event));
142
143 return 0;
144}
145
dc0a50af
SW
146static const struct snd_soc_dapm_widget tegra_wm8903_dapm_widgets[] = {
147 SND_SOC_DAPM_SPK("Int Spk", tegra_wm8903_event_int_spk),
773b1d3d 148 SND_SOC_DAPM_HP("Headphone Jack", tegra_wm8903_event_hp),
62ffac4d
SW
149 SND_SOC_DAPM_MIC("Mic Jack", NULL),
150};
151
dc0a50af 152static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
3d8bc390
SW
153 SOC_DAPM_PIN_SWITCH("Int Spk"),
154};
155
dc0a50af 156static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
62ffac4d 157{
40db77a0 158 struct snd_soc_dai *codec_dai = rtd->codec_dai;
58bd2934 159 struct snd_soc_component *component = codec_dai->component;
093c4e5c 160 struct snd_soc_card *card = rtd->card;
dc0a50af 161 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
bf1b1328 162
8f5f5e0f
SW
163 if (gpio_is_valid(machine->gpio_hp_det)) {
164 tegra_wm8903_hp_jack_gpio.gpio = machine->gpio_hp_det;
7ba8cbb2
LPC
165 snd_soc_card_jack_new(rtd->card, "Headphone Jack",
166 SND_JACK_HEADPHONE, &tegra_wm8903_hp_jack,
167 tegra_wm8903_hp_jack_pins,
168 ARRAY_SIZE(tegra_wm8903_hp_jack_pins));
773b1d3d
SW
169 snd_soc_jack_add_gpios(&tegra_wm8903_hp_jack,
170 1,
171 &tegra_wm8903_hp_jack_gpio);
172 }
f7d3e403 173
7ba8cbb2
LPC
174 snd_soc_card_jack_new(rtd->card, "Mic Jack", SND_JACK_MICROPHONE,
175 &tegra_wm8903_mic_jack,
176 tegra_wm8903_mic_jack_pins,
177 ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
58bd2934 178 wm8903_mic_detect(component, &tegra_wm8903_mic_jack, SND_JACK_MICROPHONE,
dc0a50af 179 0);
41b5f9b3 180
14e954f5 181 snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
41b5f9b3 182
62ffac4d
SW
183 return 0;
184}
185
3419ae78
SW
186static int tegra_wm8903_remove(struct snd_soc_card *card)
187{
5015920a
ML
188 struct snd_soc_pcm_runtime *rtd =
189 snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
3419ae78 190 struct snd_soc_dai *codec_dai = rtd->codec_dai;
58bd2934 191 struct snd_soc_component *component = codec_dai->component;
fb6b8e71 192
58bd2934 193 wm8903_mic_detect(component, NULL, 0, 0);
3419ae78
SW
194
195 return 0;
196}
197
dc0a50af 198static struct snd_soc_dai_link tegra_wm8903_dai = {
a8bf1ba1
SW
199 .name = "WM8903",
200 .stream_name = "WM8903 PCM",
a8bf1ba1 201 .codec_dai_name = "wm8903-hifi",
dc0a50af
SW
202 .init = tegra_wm8903_init,
203 .ops = &tegra_wm8903_ops,
408dafc4
SW
204 .dai_fmt = SND_SOC_DAIFMT_I2S |
205 SND_SOC_DAIFMT_NB_NF |
206 SND_SOC_DAIFMT_CBS_CFS,
a8bf1ba1
SW
207};
208
dc0a50af
SW
209static struct snd_soc_card snd_soc_tegra_wm8903 = {
210 .name = "tegra-wm8903",
b16eaf9f 211 .owner = THIS_MODULE,
dc0a50af 212 .dai_link = &tegra_wm8903_dai,
a8bf1ba1 213 .num_links = 1,
3419ae78 214 .remove = tegra_wm8903_remove,
dea8b6ee
SW
215 .controls = tegra_wm8903_controls,
216 .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
217 .dapm_widgets = tegra_wm8903_dapm_widgets,
218 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
6e5fdba9 219 .fully_routed = true,
a8bf1ba1
SW
220};
221
4652a0d0 222static int tegra_wm8903_driver_probe(struct platform_device *pdev)
a8bf1ba1 223{
f51022f1 224 struct device_node *np = pdev->dev.of_node;
dc0a50af
SW
225 struct snd_soc_card *card = &snd_soc_tegra_wm8903;
226 struct tegra_wm8903 *machine;
a8bf1ba1
SW
227 int ret;
228
e4e4c18a
SW
229 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
230 GFP_KERNEL);
e2c187a6 231 if (!machine)
8f5f5e0f 232 return -ENOMEM;
a8bf1ba1 233
72de2b1a 234 card->dev = &pdev->dev;
dc0a50af 235 snd_soc_card_set_drvdata(card, machine);
a8bf1ba1 236
8f5f5e0f
SW
237 machine->gpio_spkr_en = of_get_named_gpio(np, "nvidia,spkr-en-gpios",
238 0);
239 if (machine->gpio_spkr_en == -EPROBE_DEFER)
240 return -EPROBE_DEFER;
241 if (gpio_is_valid(machine->gpio_spkr_en)) {
242 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_spkr_en,
e2d287c1 243 GPIOF_OUT_INIT_LOW, "spkr_en");
f51022f1
SW
244 if (ret) {
245 dev_err(card->dev, "cannot get spkr_en gpio\n");
246 return ret;
247 }
f51022f1
SW
248 }
249
8f5f5e0f
SW
250 machine->gpio_hp_mute = of_get_named_gpio(np, "nvidia,hp-mute-gpios",
251 0);
252 if (machine->gpio_hp_mute == -EPROBE_DEFER)
253 return -EPROBE_DEFER;
254 if (gpio_is_valid(machine->gpio_hp_mute)) {
255 ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_mute,
e2d287c1 256 GPIOF_OUT_INIT_HIGH, "hp_mute");
f51022f1
SW
257 if (ret) {
258 dev_err(card->dev, "cannot get hp_mute gpio\n");
259 return ret;
260 }
f51022f1
SW
261 }
262
8f5f5e0f
SW
263 machine->gpio_hp_det = of_get_named_gpio(np, "nvidia,hp-det-gpios", 0);
264 if (machine->gpio_hp_det == -EPROBE_DEFER)
265 return -EPROBE_DEFER;
266
267 machine->gpio_int_mic_en = of_get_named_gpio(np,
268 "nvidia,int-mic-en-gpios", 0);
269 if (machine->gpio_int_mic_en == -EPROBE_DEFER)
270 return -EPROBE_DEFER;
271 if (gpio_is_valid(machine->gpio_int_mic_en)) {
e2d287c1 272 /* Disable int mic; enable signal is active-high */
8f5f5e0f
SW
273 ret = devm_gpio_request_one(&pdev->dev,
274 machine->gpio_int_mic_en,
e2d287c1 275 GPIOF_OUT_INIT_LOW, "int_mic_en");
f51022f1
SW
276 if (ret) {
277 dev_err(card->dev, "cannot get int_mic_en gpio\n");
278 return ret;
279 }
f51022f1
SW
280 }
281
8f5f5e0f
SW
282 machine->gpio_ext_mic_en = of_get_named_gpio(np,
283 "nvidia,ext-mic-en-gpios", 0);
284 if (machine->gpio_ext_mic_en == -EPROBE_DEFER)
285 return -EPROBE_DEFER;
286 if (gpio_is_valid(machine->gpio_ext_mic_en)) {
e2d287c1 287 /* Enable ext mic; enable signal is active-low */
8f5f5e0f
SW
288 ret = devm_gpio_request_one(&pdev->dev,
289 machine->gpio_ext_mic_en,
e2d287c1 290 GPIOF_OUT_INIT_LOW, "ext_mic_en");
f51022f1
SW
291 if (ret) {
292 dev_err(card->dev, "cannot get ext_mic_en gpio\n");
293 return ret;
294 }
f51022f1
SW
295 }
296
8f5f5e0f
SW
297 ret = snd_soc_of_parse_card_name(card, "nvidia,model");
298 if (ret)
299 goto err;
300
301 ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
302 if (ret)
303 goto err;
304
305 tegra_wm8903_dai.codec_of_node = of_parse_phandle(np,
306 "nvidia,audio-codec", 0);
307 if (!tegra_wm8903_dai.codec_of_node) {
308 dev_err(&pdev->dev,
309 "Property 'nvidia,audio-codec' missing or invalid\n");
310 ret = -EINVAL;
311 goto err;
312 }
313
314 tegra_wm8903_dai.cpu_of_node = of_parse_phandle(np,
315 "nvidia,i2s-controller", 0);
316 if (!tegra_wm8903_dai.cpu_of_node) {
317 dev_err(&pdev->dev,
318 "Property 'nvidia,i2s-controller' missing or invalid\n");
319 ret = -EINVAL;
320 goto err;
321 }
322
323 tegra_wm8903_dai.platform_of_node = tegra_wm8903_dai.cpu_of_node;
324
07cdf36d
SW
325 ret = tegra_asoc_utils_init(&machine->util_data, &pdev->dev);
326 if (ret)
518de86b 327 goto err;
07cdf36d 328
72de2b1a 329 ret = snd_soc_register_card(card);
a8bf1ba1 330 if (ret) {
72de2b1a 331 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
a8bf1ba1 332 ret);
acb8303f 333 goto err_fini_utils;
a8bf1ba1
SW
334 }
335
336 return 0;
337
acb8303f 338err_fini_utils:
dc0a50af 339 tegra_asoc_utils_fini(&machine->util_data);
e4e4c18a 340err:
a8bf1ba1
SW
341 return ret;
342}
a8bf1ba1 343
4652a0d0 344static int tegra_wm8903_driver_remove(struct platform_device *pdev)
a8bf1ba1 345{
72de2b1a 346 struct snd_soc_card *card = platform_get_drvdata(pdev);
dc0a50af 347 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
72de2b1a 348
29591ed4
SW
349 snd_soc_unregister_card(card);
350
351 tegra_asoc_utils_fini(&machine->util_data);
6e267645 352
72de2b1a
SW
353 return 0;
354}
355
f6e65744 356static const struct of_device_id tegra_wm8903_of_match[] = {
07cdf36d
SW
357 { .compatible = "nvidia,tegra-audio-wm8903", },
358 {},
359};
360
dc0a50af 361static struct platform_driver tegra_wm8903_driver = {
72de2b1a
SW
362 .driver = {
363 .name = DRV_NAME,
deb2607e 364 .pm = &snd_soc_pm_ops,
07cdf36d 365 .of_match_table = tegra_wm8903_of_match,
72de2b1a 366 },
dc0a50af 367 .probe = tegra_wm8903_driver_probe,
4652a0d0 368 .remove = tegra_wm8903_driver_remove,
72de2b1a 369};
e4e4c18a 370module_platform_driver(tegra_wm8903_driver);
a8bf1ba1
SW
371
372MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
dc0a50af 373MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
a8bf1ba1 374MODULE_LICENSE("GPL");
8eb34207 375MODULE_ALIAS("platform:" DRV_NAME);
07cdf36d 376MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);