1 // SPDX-License-Identifier: GPL-2.0-only
3 * tegra_rt5677.c - Tegra machine ASoC driver for boards using RT5677 codec.
5 * Copyright (c) 2014, The Chromium OS Authors. All rights reserved.
7 * Based on code copyright/by:
9 * Copyright (C) 2010-2012 - NVIDIA, Inc.
10 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
11 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
12 * Copyright 2007 Wolfson Microelectronics PLC.
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/gpio.h>
19 #include <linux/of_gpio.h>
21 #include <sound/core.h>
22 #include <sound/jack.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
27 #include "../codecs/rt5677.h"
29 #include "tegra_asoc_utils.h"
31 #define DRV_NAME "tegra-snd-rt5677"
34 struct tegra_asoc_utils_data util_data
;
41 static int tegra_rt5677_asoc_hw_params(struct snd_pcm_substream
*substream
,
42 struct snd_pcm_hw_params
*params
)
44 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
45 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
46 struct snd_soc_card
*card
= rtd
->card
;
47 struct tegra_rt5677
*machine
= snd_soc_card_get_drvdata(card
);
50 srate
= params_rate(params
);
53 err
= tegra_asoc_utils_set_rate(&machine
->util_data
, srate
, mclk
);
55 dev_err(card
->dev
, "Can't configure clocks\n");
59 err
= snd_soc_dai_set_sysclk(codec_dai
, RT5677_SCLK_S_MCLK
, mclk
,
62 dev_err(card
->dev
, "codec_dai clock not set\n");
69 static int tegra_rt5677_event_hp(struct snd_soc_dapm_widget
*w
,
70 struct snd_kcontrol
*k
, int event
)
72 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
73 struct snd_soc_card
*card
= dapm
->card
;
74 struct tegra_rt5677
*machine
= snd_soc_card_get_drvdata(card
);
76 if (!gpio_is_valid(machine
->gpio_hp_en
))
79 gpio_set_value_cansleep(machine
->gpio_hp_en
,
80 SND_SOC_DAPM_EVENT_ON(event
));
85 static const struct snd_soc_ops tegra_rt5677_ops
= {
86 .hw_params
= tegra_rt5677_asoc_hw_params
,
89 static struct snd_soc_jack tegra_rt5677_hp_jack
;
91 static struct snd_soc_jack_pin tegra_rt5677_hp_jack_pins
= {
93 .mask
= SND_JACK_HEADPHONE
,
95 static struct snd_soc_jack_gpio tegra_rt5677_hp_jack_gpio
= {
96 .name
= "Headphone detection",
97 .report
= SND_JACK_HEADPHONE
,
101 static struct snd_soc_jack tegra_rt5677_mic_jack
;
103 static struct snd_soc_jack_pin tegra_rt5677_mic_jack_pins
= {
104 .pin
= "Headset Mic",
105 .mask
= SND_JACK_MICROPHONE
,
108 static struct snd_soc_jack_gpio tegra_rt5677_mic_jack_gpio
= {
109 .name
= "Headset Mic detection",
110 .report
= SND_JACK_MICROPHONE
,
111 .debounce_time
= 150,
115 static const struct snd_soc_dapm_widget tegra_rt5677_dapm_widgets
[] = {
116 SND_SOC_DAPM_SPK("Speaker", NULL
),
117 SND_SOC_DAPM_HP("Headphone", tegra_rt5677_event_hp
),
118 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
119 SND_SOC_DAPM_MIC("Internal Mic 1", NULL
),
120 SND_SOC_DAPM_MIC("Internal Mic 2", NULL
),
123 static const struct snd_kcontrol_new tegra_rt5677_controls
[] = {
124 SOC_DAPM_PIN_SWITCH("Speaker"),
125 SOC_DAPM_PIN_SWITCH("Headphone"),
126 SOC_DAPM_PIN_SWITCH("Headset Mic"),
127 SOC_DAPM_PIN_SWITCH("Internal Mic 1"),
128 SOC_DAPM_PIN_SWITCH("Internal Mic 2"),
131 static int tegra_rt5677_asoc_init(struct snd_soc_pcm_runtime
*rtd
)
133 struct tegra_rt5677
*machine
= snd_soc_card_get_drvdata(rtd
->card
);
135 snd_soc_card_jack_new(rtd
->card
, "Headphone Jack", SND_JACK_HEADPHONE
,
136 &tegra_rt5677_hp_jack
,
137 &tegra_rt5677_hp_jack_pins
, 1);
139 if (gpio_is_valid(machine
->gpio_hp_det
)) {
140 tegra_rt5677_hp_jack_gpio
.gpio
= machine
->gpio_hp_det
;
141 snd_soc_jack_add_gpios(&tegra_rt5677_hp_jack
, 1,
142 &tegra_rt5677_hp_jack_gpio
);
146 snd_soc_card_jack_new(rtd
->card
, "Mic Jack", SND_JACK_MICROPHONE
,
147 &tegra_rt5677_mic_jack
,
148 &tegra_rt5677_mic_jack_pins
, 1);
150 if (gpio_is_valid(machine
->gpio_mic_present
)) {
151 tegra_rt5677_mic_jack_gpio
.gpio
= machine
->gpio_mic_present
;
152 snd_soc_jack_add_gpios(&tegra_rt5677_mic_jack
, 1,
153 &tegra_rt5677_mic_jack_gpio
);
156 snd_soc_dapm_force_enable_pin(&rtd
->card
->dapm
, "MICBIAS1");
161 SND_SOC_DAILINK_DEFS(pcm
,
162 DAILINK_COMP_ARRAY(COMP_EMPTY()),
163 DAILINK_COMP_ARRAY(COMP_CODEC(NULL
, "rt5677-aif1")),
164 DAILINK_COMP_ARRAY(COMP_EMPTY()));
166 static struct snd_soc_dai_link tegra_rt5677_dai
= {
168 .stream_name
= "RT5677 PCM",
169 .init
= tegra_rt5677_asoc_init
,
170 .ops
= &tegra_rt5677_ops
,
171 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
172 SND_SOC_DAIFMT_CBS_CFS
,
173 SND_SOC_DAILINK_REG(pcm
),
176 static struct snd_soc_card snd_soc_tegra_rt5677
= {
177 .name
= "tegra-rt5677",
178 .owner
= THIS_MODULE
,
179 .dai_link
= &tegra_rt5677_dai
,
181 .controls
= tegra_rt5677_controls
,
182 .num_controls
= ARRAY_SIZE(tegra_rt5677_controls
),
183 .dapm_widgets
= tegra_rt5677_dapm_widgets
,
184 .num_dapm_widgets
= ARRAY_SIZE(tegra_rt5677_dapm_widgets
),
185 .fully_routed
= true,
188 static int tegra_rt5677_probe(struct platform_device
*pdev
)
190 struct device_node
*np
= pdev
->dev
.of_node
;
191 struct snd_soc_card
*card
= &snd_soc_tegra_rt5677
;
192 struct tegra_rt5677
*machine
;
195 machine
= devm_kzalloc(&pdev
->dev
,
196 sizeof(struct tegra_rt5677
), GFP_KERNEL
);
200 card
->dev
= &pdev
->dev
;
201 snd_soc_card_set_drvdata(card
, machine
);
203 machine
->gpio_hp_det
= of_get_named_gpio(np
, "nvidia,hp-det-gpios", 0);
204 if (machine
->gpio_hp_det
== -EPROBE_DEFER
)
205 return -EPROBE_DEFER
;
207 machine
->gpio_mic_present
= of_get_named_gpio(np
,
208 "nvidia,mic-present-gpios", 0);
209 if (machine
->gpio_mic_present
== -EPROBE_DEFER
)
210 return -EPROBE_DEFER
;
212 machine
->gpio_hp_en
= of_get_named_gpio(np
, "nvidia,hp-en-gpios", 0);
213 if (machine
->gpio_hp_en
== -EPROBE_DEFER
)
214 return -EPROBE_DEFER
;
215 if (gpio_is_valid(machine
->gpio_hp_en
)) {
216 ret
= devm_gpio_request_one(&pdev
->dev
, machine
->gpio_hp_en
,
217 GPIOF_OUT_INIT_LOW
, "hp_en");
219 dev_err(card
->dev
, "cannot get hp_en gpio\n");
224 machine
->gpio_dmic_clk_en
= of_get_named_gpio(np
,
225 "nvidia,dmic-clk-en-gpios", 0);
226 if (machine
->gpio_dmic_clk_en
== -EPROBE_DEFER
)
227 return -EPROBE_DEFER
;
228 if (gpio_is_valid(machine
->gpio_dmic_clk_en
)) {
229 ret
= devm_gpio_request_one(&pdev
->dev
,
230 machine
->gpio_dmic_clk_en
,
231 GPIOF_OUT_INIT_HIGH
, "dmic_clk_en");
233 dev_err(card
->dev
, "cannot get dmic_clk_en gpio\n");
238 ret
= snd_soc_of_parse_card_name(card
, "nvidia,model");
242 ret
= snd_soc_of_parse_audio_routing(card
, "nvidia,audio-routing");
246 tegra_rt5677_dai
.codecs
->of_node
= of_parse_phandle(np
,
247 "nvidia,audio-codec", 0);
248 if (!tegra_rt5677_dai
.codecs
->of_node
) {
250 "Property 'nvidia,audio-codec' missing or invalid\n");
255 tegra_rt5677_dai
.cpus
->of_node
= of_parse_phandle(np
,
256 "nvidia,i2s-controller", 0);
257 if (!tegra_rt5677_dai
.cpus
->of_node
) {
259 "Property 'nvidia,i2s-controller' missing or invalid\n");
261 goto err_put_codec_of_node
;
263 tegra_rt5677_dai
.platforms
->of_node
= tegra_rt5677_dai
.cpus
->of_node
;
265 ret
= tegra_asoc_utils_init(&machine
->util_data
, &pdev
->dev
);
267 goto err_put_cpu_of_node
;
269 ret
= snd_soc_register_card(card
);
271 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n",
279 tegra_asoc_utils_fini(&machine
->util_data
);
281 of_node_put(tegra_rt5677_dai
.cpus
->of_node
);
282 tegra_rt5677_dai
.cpus
->of_node
= NULL
;
283 tegra_rt5677_dai
.platforms
->of_node
= NULL
;
284 err_put_codec_of_node
:
285 of_node_put(tegra_rt5677_dai
.codecs
->of_node
);
286 tegra_rt5677_dai
.codecs
->of_node
= NULL
;
291 static int tegra_rt5677_remove(struct platform_device
*pdev
)
293 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
294 struct tegra_rt5677
*machine
= snd_soc_card_get_drvdata(card
);
296 snd_soc_unregister_card(card
);
298 tegra_asoc_utils_fini(&machine
->util_data
);
300 tegra_rt5677_dai
.platforms
->of_node
= NULL
;
301 of_node_put(tegra_rt5677_dai
.codecs
->of_node
);
302 tegra_rt5677_dai
.codecs
->of_node
= NULL
;
303 of_node_put(tegra_rt5677_dai
.cpus
->of_node
);
304 tegra_rt5677_dai
.cpus
->of_node
= NULL
;
309 static const struct of_device_id tegra_rt5677_of_match
[] = {
310 { .compatible
= "nvidia,tegra-audio-rt5677", },
314 static struct platform_driver tegra_rt5677_driver
= {
317 .pm
= &snd_soc_pm_ops
,
318 .of_match_table
= tegra_rt5677_of_match
,
320 .probe
= tegra_rt5677_probe
,
321 .remove
= tegra_rt5677_remove
,
323 module_platform_driver(tegra_rt5677_driver
);
325 MODULE_AUTHOR("Anatol Pomozov <anatol@google.com>");
326 MODULE_DESCRIPTION("Tegra+RT5677 machine ASoC driver");
327 MODULE_LICENSE("GPL v2");
328 MODULE_ALIAS("platform:" DRV_NAME
);
329 MODULE_DEVICE_TABLE(of
, tegra_rt5677_of_match
);