Loading...
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/gpio/consumer.h>
18#include <linux/of.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21
22#include <sound/core.h>
23#include <sound/jack.h>
24#include <sound/soc.h>
25
26#include "../codecs/wm8903.h"
27
28#include "tegra_asoc_machine.h"
29
30static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
31 { .pin = "Mic Jack", .mask = SND_JACK_MICROPHONE },
32};
33
34static unsigned int tegra_wm8903_mclk_rate(unsigned int srate)
35{
36 unsigned int mclk;
37
38 switch (srate) {
39 case 64000:
40 case 88200:
41 case 96000:
42 mclk = 128 * srate;
43 break;
44 default:
45 mclk = 256 * srate;
46 break;
47 }
48 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
49 while (mclk < 6000000)
50 mclk *= 2;
51
52 return mclk;
53}
54
55static int tegra_wm8903_init(struct snd_soc_pcm_runtime *rtd)
56{
57 struct tegra_machine *machine = snd_soc_card_get_drvdata(rtd->card);
58 struct snd_soc_card *card = rtd->card;
59 int err;
60
61 /*
62 * Older version of machine driver was ignoring GPIO polarity,
63 * forcing it to active-low. This means that all older device-trees
64 * which set the polarity to active-high are wrong and we need to fix
65 * them up.
66 */
67 if (machine->asoc->hp_jack_gpio_active_low) {
68 bool active_low = gpiod_is_active_low(machine->gpiod_hp_det);
69
70 machine->hp_jack_gpio->invert = !active_low;
71 }
72
73 err = tegra_asoc_machine_init(rtd);
74 if (err)
75 return err;
76
77 if (!machine->gpiod_mic_det && machine->asoc->add_mic_jack) {
78 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
79 struct snd_soc_component *component = codec_dai->component;
80 int shrt = 0;
81
82 err = snd_soc_card_jack_new_pins(rtd->card, "Mic Jack",
83 SND_JACK_MICROPHONE,
84 machine->mic_jack,
85 tegra_wm8903_mic_jack_pins,
86 ARRAY_SIZE(tegra_wm8903_mic_jack_pins));
87 if (err) {
88 dev_err(rtd->dev, "Mic Jack creation failed: %d\n", err);
89 return err;
90 }
91
92 if (of_property_read_bool(card->dev->of_node, "nvidia,headset"))
93 shrt = SND_JACK_MICROPHONE;
94
95 wm8903_mic_detect(component, machine->mic_jack,
96 SND_JACK_MICROPHONE, shrt);
97 }
98
99 snd_soc_dapm_force_enable_pin(&card->dapm, "MICBIAS");
100
101 return 0;
102}
103
104static int tegra_wm8903_remove(struct snd_soc_card *card)
105{
106 struct snd_soc_dai_link *link = &card->dai_link[0];
107 struct snd_soc_pcm_runtime *rtd = snd_soc_get_pcm_runtime(card, link);
108 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
109 struct snd_soc_component *component = codec_dai->component;
110
111 wm8903_mic_detect(component, NULL, 0, 0);
112
113 return 0;
114}
115
116SND_SOC_DAILINK_DEFS(hifi,
117 DAILINK_COMP_ARRAY(COMP_EMPTY()),
118 DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8903-hifi")),
119 DAILINK_COMP_ARRAY(COMP_EMPTY()));
120
121static struct snd_soc_dai_link tegra_wm8903_dai = {
122 .name = "WM8903",
123 .stream_name = "WM8903 PCM",
124 .init = tegra_wm8903_init,
125 .dai_fmt = SND_SOC_DAIFMT_I2S |
126 SND_SOC_DAIFMT_NB_NF |
127 SND_SOC_DAIFMT_CBS_CFS,
128 SND_SOC_DAILINK_REG(hifi),
129};
130
131static struct snd_soc_card snd_soc_tegra_wm8903 = {
132 .components = "codec:wm8903",
133 .owner = THIS_MODULE,
134 .dai_link = &tegra_wm8903_dai,
135 .num_links = 1,
136 .remove = tegra_wm8903_remove,
137 .fully_routed = true,
138};
139
140/* older device-trees used wrong polarity for the headphones-detection GPIO */
141static const struct tegra_asoc_data tegra_wm8903_data_legacy = {
142 .mclk_rate = tegra_wm8903_mclk_rate,
143 .card = &snd_soc_tegra_wm8903,
144 .hp_jack_gpio_active_low = true,
145 .add_common_dapm_widgets = true,
146 .add_common_controls = true,
147 .add_common_snd_ops = true,
148 .add_mic_jack = true,
149 .add_hp_jack = true,
150};
151
152static const struct tegra_asoc_data tegra_wm8903_data = {
153 .mclk_rate = tegra_wm8903_mclk_rate,
154 .card = &snd_soc_tegra_wm8903,
155 .add_common_dapm_widgets = true,
156 .add_common_controls = true,
157 .add_common_snd_ops = true,
158 .add_mic_jack = true,
159 .add_hp_jack = true,
160};
161
162static const struct of_device_id tegra_wm8903_of_match[] = {
163 { .compatible = "ad,tegra-audio-plutux", .data = &tegra_wm8903_data_legacy },
164 { .compatible = "ad,tegra-audio-wm8903-medcom-wide", .data = &tegra_wm8903_data_legacy },
165 { .compatible = "ad,tegra-audio-wm8903-tec", .data = &tegra_wm8903_data_legacy },
166 { .compatible = "nvidia,tegra-audio-wm8903-cardhu", .data = &tegra_wm8903_data_legacy },
167 { .compatible = "nvidia,tegra-audio-wm8903-harmony", .data = &tegra_wm8903_data_legacy },
168 { .compatible = "nvidia,tegra-audio-wm8903-picasso", .data = &tegra_wm8903_data_legacy },
169 { .compatible = "nvidia,tegra-audio-wm8903-seaboard", .data = &tegra_wm8903_data_legacy },
170 { .compatible = "nvidia,tegra-audio-wm8903-ventana", .data = &tegra_wm8903_data_legacy },
171 { .compatible = "nvidia,tegra-audio-wm8903", .data = &tegra_wm8903_data },
172 {},
173};
174MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);
175
176static struct platform_driver tegra_wm8903_driver = {
177 .driver = {
178 .name = "tegra-wm8903",
179 .of_match_table = tegra_wm8903_of_match,
180 .pm = &snd_soc_pm_ops,
181 },
182 .probe = tegra_asoc_machine_probe,
183};
184module_platform_driver(tegra_wm8903_driver);
185
186MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
187MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
188MODULE_LICENSE("GPL");
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
49struct 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
58static 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
99static struct snd_soc_ops tegra_wm8903_ops = {
100 .hw_params = tegra_wm8903_hw_params,
101};
102
103static struct snd_soc_jack tegra_wm8903_hp_jack;
104
105static struct snd_soc_jack_pin tegra_wm8903_hp_jack_pins[] = {
106 {
107 .pin = "Headphone Jack",
108 .mask = SND_JACK_HEADPHONE,
109 },
110};
111
112static 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
119static struct snd_soc_jack tegra_wm8903_mic_jack;
120
121static struct snd_soc_jack_pin tegra_wm8903_mic_jack_pins[] = {
122 {
123 .pin = "Mic Jack",
124 .mask = SND_JACK_MICROPHONE,
125 },
126};
127
128static 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
144static 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
160static 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
166static const struct snd_kcontrol_new tegra_wm8903_controls[] = {
167 SOC_DAPM_PIN_SWITCH("Int Spk"),
168};
169
170static 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_codec *codec = codec_dai->codec;
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(codec, &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
200static 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_codec *codec = codec_dai->codec;
206 struct tegra_wm8903 *machine = snd_soc_card_get_drvdata(card);
207
208 if (gpio_is_valid(machine->gpio_hp_det)) {
209 snd_soc_jack_free_gpios(&tegra_wm8903_hp_jack, 1,
210 &tegra_wm8903_hp_jack_gpio);
211 }
212
213 wm8903_mic_detect(codec, NULL, 0, 0);
214
215 return 0;
216}
217
218static struct snd_soc_dai_link tegra_wm8903_dai = {
219 .name = "WM8903",
220 .stream_name = "WM8903 PCM",
221 .codec_dai_name = "wm8903-hifi",
222 .init = tegra_wm8903_init,
223 .ops = &tegra_wm8903_ops,
224 .dai_fmt = SND_SOC_DAIFMT_I2S |
225 SND_SOC_DAIFMT_NB_NF |
226 SND_SOC_DAIFMT_CBS_CFS,
227};
228
229static struct snd_soc_card snd_soc_tegra_wm8903 = {
230 .name = "tegra-wm8903",
231 .owner = THIS_MODULE,
232 .dai_link = &tegra_wm8903_dai,
233 .num_links = 1,
234 .remove = tegra_wm8903_remove,
235 .controls = tegra_wm8903_controls,
236 .num_controls = ARRAY_SIZE(tegra_wm8903_controls),
237 .dapm_widgets = tegra_wm8903_dapm_widgets,
238 .num_dapm_widgets = ARRAY_SIZE(tegra_wm8903_dapm_widgets),
239 .fully_routed = true,
240};
241
242static int tegra_wm8903_driver_probe(struct platform_device *pdev)
243{
244 struct device_node *np = pdev->dev.of_node;
245 struct snd_soc_card *card = &snd_soc_tegra_wm8903;
246 struct tegra_wm8903 *machine;
247 int ret;
248
249 machine = devm_kzalloc(&pdev->dev, sizeof(struct tegra_wm8903),
250 GFP_KERNEL);
251 if (!machine) {
252 dev_err(&pdev->dev, "Can't allocate tegra_wm8903 struct\n");
253 return -ENOMEM;
254 }
255
256 card->dev = &pdev->dev;
257 platform_set_drvdata(pdev, card);
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.codec_of_node = of_parse_phandle(np,
329 "nvidia,audio-codec", 0);
330 if (!tegra_wm8903_dai.codec_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.cpu_of_node = of_parse_phandle(np,
338 "nvidia,i2s-controller", 0);
339 if (!tegra_wm8903_dai.cpu_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.platform_of_node = tegra_wm8903_dai.cpu_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
361err_fini_utils:
362 tegra_asoc_utils_fini(&machine->util_data);
363err:
364 return ret;
365}
366
367static 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
379static const struct of_device_id tegra_wm8903_of_match[] = {
380 { .compatible = "nvidia,tegra-audio-wm8903", },
381 {},
382};
383
384static 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};
393module_platform_driver(tegra_wm8903_driver);
394
395MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
396MODULE_DESCRIPTION("Tegra+WM8903 machine ASoC driver");
397MODULE_LICENSE("GPL");
398MODULE_ALIAS("platform:" DRV_NAME);
399MODULE_DEVICE_TABLE(of, tegra_wm8903_of_match);