2 * Copyright (C) 2015 - 2016 Samsung Electronics Co., Ltd.
4 * Authors: Inha Song <ideal.song@samsung.com>
5 * Sylwester Nawrocki <s.nawrocki@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
13 #include <linux/clk.h>
14 #include <linux/gpio.h>
15 #include <linux/module.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
21 #include "../codecs/wm5110.h"
24 * The source clock is XCLKOUT with its mux set to the external fixed rate
27 #define MCLK_RATE 24000000U
29 #define TM2_DAI_AIF1 0
30 #define TM2_DAI_AIF2 1
32 struct tm2_machine_priv
{
33 struct snd_soc_codec
*codec
;
34 unsigned int sysclk_rate
;
35 struct gpio_desc
*gpio_mic_bias
;
38 static int tm2_start_sysclk(struct snd_soc_card
*card
)
40 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
41 struct snd_soc_codec
*codec
= priv
->codec
;
44 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1_REFCLK
,
45 ARIZONA_FLL_SRC_MCLK1
,
49 dev_err(codec
->dev
, "Failed to set FLL1 source: %d\n", ret
);
53 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1
,
54 ARIZONA_FLL_SRC_MCLK1
,
58 dev_err(codec
->dev
, "Failed to start FLL1: %d\n", ret
);
62 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_SYSCLK
,
67 dev_err(codec
->dev
, "Failed to set SYSCLK source: %d\n", ret
);
74 static int tm2_stop_sysclk(struct snd_soc_card
*card
)
76 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
77 struct snd_soc_codec
*codec
= priv
->codec
;
80 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1
, 0, 0, 0);
82 dev_err(codec
->dev
, "Failed to stop FLL1: %d\n", ret
);
86 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_SYSCLK
,
87 ARIZONA_CLK_SRC_FLL1
, 0, 0);
89 dev_err(codec
->dev
, "Failed to stop SYSCLK: %d\n", ret
);
96 static int tm2_aif1_hw_params(struct snd_pcm_substream
*substream
,
97 struct snd_pcm_hw_params
*params
)
99 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
100 struct snd_soc_codec
*codec
= rtd
->codec
;
101 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
103 switch (params_rate(params
)) {
113 /* Highest possible SYSCLK frequency: 147.456MHz */
114 priv
->sysclk_rate
= 147456000U;
121 /* Highest possible SYSCLK frequency: 135.4752 MHz */
122 priv
->sysclk_rate
= 135475200U;
125 dev_err(codec
->dev
, "Not supported sample rate: %d\n",
126 params_rate(params
));
130 return tm2_start_sysclk(rtd
->card
);
133 static struct snd_soc_ops tm2_aif1_ops
= {
134 .hw_params
= tm2_aif1_hw_params
,
137 static int tm2_aif2_hw_params(struct snd_pcm_substream
*substream
,
138 struct snd_pcm_hw_params
*params
)
140 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
141 struct snd_soc_codec
*codec
= rtd
->codec
;
142 unsigned int asyncclk_rate
;
145 switch (params_rate(params
)) {
149 /* Highest possible ASYNCCLK frequency: 49.152MHz */
150 asyncclk_rate
= 49152000U;
153 /* Highest possible ASYNCCLK frequency: 45.1584 MHz */
154 asyncclk_rate
= 45158400U;
157 dev_err(codec
->dev
, "Not supported sample rate: %d\n",
158 params_rate(params
));
162 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2_REFCLK
,
163 ARIZONA_FLL_SRC_MCLK1
,
167 dev_err(codec
->dev
, "Failed to set FLL2 source: %d\n", ret
);
171 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2
,
172 ARIZONA_FLL_SRC_MCLK1
,
176 dev_err(codec
->dev
, "Failed to start FLL2: %d\n", ret
);
180 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_ASYNCCLK
,
181 ARIZONA_CLK_SRC_FLL2
,
185 dev_err(codec
->dev
, "Failed to set ASYNCCLK source: %d\n", ret
);
192 static int tm2_aif2_hw_free(struct snd_pcm_substream
*substream
)
194 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
195 struct snd_soc_codec
*codec
= rtd
->codec
;
199 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2
, ARIZONA_FLL_SRC_MCLK1
,
202 dev_err(codec
->dev
, "Failed to stop FLL2: %d\n", ret
);
207 static struct snd_soc_ops tm2_aif2_ops
= {
208 .hw_params
= tm2_aif2_hw_params
,
209 .hw_free
= tm2_aif2_hw_free
,
212 static int tm2_mic_bias(struct snd_soc_dapm_widget
*w
,
213 struct snd_kcontrol
*kcontrol
, int event
)
215 struct snd_soc_card
*card
= w
->dapm
->card
;
216 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
219 case SND_SOC_DAPM_PRE_PMU
:
220 gpiod_set_value_cansleep(priv
->gpio_mic_bias
, 1);
222 case SND_SOC_DAPM_POST_PMD
:
223 gpiod_set_value_cansleep(priv
->gpio_mic_bias
, 0);
230 static int tm2_set_bias_level(struct snd_soc_card
*card
,
231 struct snd_soc_dapm_context
*dapm
,
232 enum snd_soc_bias_level level
)
234 struct snd_soc_pcm_runtime
*rtd
;
236 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[0].name
);
238 if (dapm
->dev
!= rtd
->codec_dai
->dev
)
242 case SND_SOC_BIAS_STANDBY
:
243 if (card
->dapm
.bias_level
== SND_SOC_BIAS_OFF
)
244 tm2_start_sysclk(card
);
246 case SND_SOC_BIAS_OFF
:
247 tm2_stop_sysclk(card
);
256 static struct snd_soc_aux_dev tm2_speaker_amp_dev
;
258 static int tm2_late_probe(struct snd_soc_card
*card
)
260 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
261 struct snd_soc_dai_link_component dlc
= { 0 };
262 unsigned int ch_map
[] = { 0, 1 };
263 struct snd_soc_dai
*amp_pdm_dai
;
264 struct snd_soc_pcm_runtime
*rtd
;
265 struct snd_soc_dai
*aif1_dai
;
266 struct snd_soc_dai
*aif2_dai
;
269 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[TM2_DAI_AIF1
].name
);
270 aif1_dai
= rtd
->codec_dai
;
271 priv
->codec
= rtd
->codec
;
273 ret
= snd_soc_dai_set_sysclk(aif1_dai
, ARIZONA_CLK_SYSCLK
, 0, 0);
275 dev_err(aif1_dai
->dev
, "Failed to set SYSCLK: %d\n", ret
);
279 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[TM2_DAI_AIF2
].name
);
280 aif2_dai
= rtd
->codec_dai
;
282 ret
= snd_soc_dai_set_sysclk(aif2_dai
, ARIZONA_CLK_ASYNCCLK
, 0, 0);
284 dev_err(aif2_dai
->dev
, "Failed to set ASYNCCLK: %d\n", ret
);
288 dlc
.of_node
= tm2_speaker_amp_dev
.codec_of_node
;
289 amp_pdm_dai
= snd_soc_find_dai(&dlc
);
293 /* Set the MAX98504 V/I sense PDM Tx DAI channel mapping */
294 ret
= snd_soc_dai_set_channel_map(amp_pdm_dai
, ARRAY_SIZE(ch_map
),
299 ret
= snd_soc_dai_set_tdm_slot(amp_pdm_dai
, 0x3, 0x0, 2, 16);
306 static const struct snd_kcontrol_new tm2_controls
[] = {
307 SOC_DAPM_PIN_SWITCH("HP"),
308 SOC_DAPM_PIN_SWITCH("SPK"),
309 SOC_DAPM_PIN_SWITCH("RCV"),
310 SOC_DAPM_PIN_SWITCH("VPS"),
311 SOC_DAPM_PIN_SWITCH("HDMI"),
313 SOC_DAPM_PIN_SWITCH("Main Mic"),
314 SOC_DAPM_PIN_SWITCH("Sub Mic"),
315 SOC_DAPM_PIN_SWITCH("Third Mic"),
317 SOC_DAPM_PIN_SWITCH("Headset Mic"),
320 const struct snd_soc_dapm_widget tm2_dapm_widgets
[] = {
321 SND_SOC_DAPM_HP("HP", NULL
),
322 SND_SOC_DAPM_SPK("SPK", NULL
),
323 SND_SOC_DAPM_SPK("RCV", NULL
),
324 SND_SOC_DAPM_LINE("VPS", NULL
),
325 SND_SOC_DAPM_LINE("HDMI", NULL
),
327 SND_SOC_DAPM_MIC("Main Mic", tm2_mic_bias
),
328 SND_SOC_DAPM_MIC("Sub Mic", NULL
),
329 SND_SOC_DAPM_MIC("Third Mic", NULL
),
331 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
334 static const struct snd_soc_component_driver tm2_component
= {
338 static struct snd_soc_dai_driver tm2_ext_dai
[] = {
340 .name
= "Voice call",
346 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
347 SNDRV_PCM_RATE_48000
),
348 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
355 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
356 SNDRV_PCM_RATE_48000
),
357 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
367 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
),
368 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
375 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
),
376 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
381 static struct snd_soc_dai_link tm2_dai_links
[] = {
383 .name
= "WM5110 AIF1",
384 .stream_name
= "HiFi Primary",
385 .codec_dai_name
= "wm5110-aif1",
386 .ops
= &tm2_aif1_ops
,
387 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
388 SND_SOC_DAIFMT_CBM_CFM
,
390 .name
= "WM5110 Voice",
391 .stream_name
= "Voice call",
392 .codec_dai_name
= "wm5110-aif2",
393 .ops
= &tm2_aif2_ops
,
394 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
395 SND_SOC_DAIFMT_CBM_CFM
,
399 .stream_name
= "Bluetooth",
400 .codec_dai_name
= "wm5110-aif3",
401 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
402 SND_SOC_DAIFMT_CBM_CFM
,
407 static struct snd_soc_card tm2_card
= {
408 .owner
= THIS_MODULE
,
410 .dai_link
= tm2_dai_links
,
411 .num_links
= ARRAY_SIZE(tm2_dai_links
),
412 .controls
= tm2_controls
,
413 .num_controls
= ARRAY_SIZE(tm2_controls
),
414 .dapm_widgets
= tm2_dapm_widgets
,
415 .num_dapm_widgets
= ARRAY_SIZE(tm2_dapm_widgets
),
416 .aux_dev
= &tm2_speaker_amp_dev
,
419 .late_probe
= tm2_late_probe
,
420 .set_bias_level
= tm2_set_bias_level
,
423 static int tm2_probe(struct platform_device
*pdev
)
425 struct device
*dev
= &pdev
->dev
;
426 struct snd_soc_card
*card
= &tm2_card
;
427 struct tm2_machine_priv
*priv
;
428 struct device_node
*cpu_dai_node
, *codec_dai_node
;
431 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
435 snd_soc_card_set_drvdata(card
, priv
);
438 priv
->gpio_mic_bias
= devm_gpiod_get(dev
, "mic-bias",
440 if (IS_ERR(priv
->gpio_mic_bias
)) {
441 dev_err(dev
, "Failed to get mic bias gpio\n");
442 return PTR_ERR(priv
->gpio_mic_bias
);
445 ret
= snd_soc_of_parse_card_name(card
, "model");
447 dev_err(dev
, "Card name is not specified\n");
451 ret
= snd_soc_of_parse_audio_routing(card
, "samsung,audio-routing");
453 dev_err(dev
, "Audio routing is not specified or invalid\n");
457 card
->aux_dev
[0].codec_of_node
= of_parse_phandle(dev
->of_node
,
458 "audio-amplifier", 0);
459 if (!card
->aux_dev
[0].codec_of_node
) {
460 dev_err(dev
, "audio-amplifier property invalid or missing\n");
464 cpu_dai_node
= of_parse_phandle(dev
->of_node
, "i2s-controller", 0);
466 dev_err(dev
, "i2s-controllers property invalid or missing\n");
471 codec_dai_node
= of_parse_phandle(dev
->of_node
, "audio-codec", 0);
472 if (!codec_dai_node
) {
473 dev_err(dev
, "audio-codec property invalid or missing\n");
475 goto cpu_dai_node_put
;
478 for (i
= 0; i
< card
->num_links
; i
++) {
479 card
->dai_link
[i
].cpu_dai_name
= NULL
;
480 card
->dai_link
[i
].cpu_name
= NULL
;
481 card
->dai_link
[i
].platform_name
= NULL
;
482 card
->dai_link
[i
].codec_of_node
= codec_dai_node
;
483 card
->dai_link
[i
].cpu_of_node
= cpu_dai_node
;
484 card
->dai_link
[i
].platform_of_node
= cpu_dai_node
;
487 ret
= devm_snd_soc_register_component(dev
, &tm2_component
,
488 tm2_ext_dai
, ARRAY_SIZE(tm2_ext_dai
));
490 dev_err(dev
, "Failed to register component: %d\n", ret
);
491 goto codec_dai_node_put
;
494 ret
= devm_snd_soc_register_card(dev
, card
);
496 dev_err(dev
, "Failed to register card: %d\n", ret
);
497 goto codec_dai_node_put
;
501 of_node_put(codec_dai_node
);
503 of_node_put(cpu_dai_node
);
505 of_node_put(card
->aux_dev
[0].codec_of_node
);
509 static int tm2_pm_prepare(struct device
*dev
)
511 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
513 return tm2_stop_sysclk(card
);
516 static void tm2_pm_complete(struct device
*dev
)
518 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
520 tm2_start_sysclk(card
);
523 const struct dev_pm_ops tm2_pm_ops
= {
524 .prepare
= tm2_pm_prepare
,
525 .suspend
= snd_soc_suspend
,
526 .resume
= snd_soc_resume
,
527 .complete
= tm2_pm_complete
,
528 .freeze
= snd_soc_suspend
,
529 .thaw
= snd_soc_resume
,
530 .poweroff
= snd_soc_poweroff
,
531 .restore
= snd_soc_resume
,
534 static const struct of_device_id tm2_of_match
[] = {
535 { .compatible
= "samsung,tm2-audio" },
538 MODULE_DEVICE_TABLE(of
, tm2_of_match
);
540 static struct platform_driver tm2_driver
= {
544 .of_match_table
= tm2_of_match
,
548 module_platform_driver(tm2_driver
);
550 MODULE_AUTHOR("Inha Song <ideal.song@samsung.com>");
551 MODULE_DESCRIPTION("ALSA SoC Exynos TM2 Audio Support");
552 MODULE_LICENSE("GPL v2");