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/gpio/consumer.h>
16 #include <linux/module.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
22 #include "../codecs/wm5110.h"
25 * The source clock is XCLKOUT with its mux set to the external fixed rate
28 #define MCLK_RATE 24000000U
30 #define TM2_DAI_AIF1 0
31 #define TM2_DAI_AIF2 1
33 struct tm2_machine_priv
{
34 struct snd_soc_codec
*codec
;
35 unsigned int sysclk_rate
;
36 struct gpio_desc
*gpio_mic_bias
;
39 static int tm2_start_sysclk(struct snd_soc_card
*card
)
41 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
42 struct snd_soc_codec
*codec
= priv
->codec
;
45 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1_REFCLK
,
46 ARIZONA_FLL_SRC_MCLK1
,
50 dev_err(codec
->dev
, "Failed to set FLL1 source: %d\n", ret
);
54 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1
,
55 ARIZONA_FLL_SRC_MCLK1
,
59 dev_err(codec
->dev
, "Failed to start FLL1: %d\n", ret
);
63 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_SYSCLK
,
68 dev_err(codec
->dev
, "Failed to set SYSCLK source: %d\n", ret
);
75 static int tm2_stop_sysclk(struct snd_soc_card
*card
)
77 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
78 struct snd_soc_codec
*codec
= priv
->codec
;
81 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL1
, 0, 0, 0);
83 dev_err(codec
->dev
, "Failed to stop FLL1: %d\n", ret
);
87 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_SYSCLK
,
88 ARIZONA_CLK_SRC_FLL1
, 0, 0);
90 dev_err(codec
->dev
, "Failed to stop SYSCLK: %d\n", ret
);
97 static int tm2_aif1_hw_params(struct snd_pcm_substream
*substream
,
98 struct snd_pcm_hw_params
*params
)
100 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
101 struct snd_soc_codec
*codec
= rtd
->codec
;
102 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(rtd
->card
);
104 switch (params_rate(params
)) {
114 /* Highest possible SYSCLK frequency: 147.456MHz */
115 priv
->sysclk_rate
= 147456000U;
122 /* Highest possible SYSCLK frequency: 135.4752 MHz */
123 priv
->sysclk_rate
= 135475200U;
126 dev_err(codec
->dev
, "Not supported sample rate: %d\n",
127 params_rate(params
));
131 return tm2_start_sysclk(rtd
->card
);
134 static struct snd_soc_ops tm2_aif1_ops
= {
135 .hw_params
= tm2_aif1_hw_params
,
138 static int tm2_aif2_hw_params(struct snd_pcm_substream
*substream
,
139 struct snd_pcm_hw_params
*params
)
141 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
142 struct snd_soc_codec
*codec
= rtd
->codec
;
143 unsigned int asyncclk_rate
;
146 switch (params_rate(params
)) {
150 /* Highest possible ASYNCCLK frequency: 49.152MHz */
151 asyncclk_rate
= 49152000U;
154 /* Highest possible ASYNCCLK frequency: 45.1584 MHz */
155 asyncclk_rate
= 45158400U;
158 dev_err(codec
->dev
, "Not supported sample rate: %d\n",
159 params_rate(params
));
163 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2_REFCLK
,
164 ARIZONA_FLL_SRC_MCLK1
,
168 dev_err(codec
->dev
, "Failed to set FLL2 source: %d\n", ret
);
172 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2
,
173 ARIZONA_FLL_SRC_MCLK1
,
177 dev_err(codec
->dev
, "Failed to start FLL2: %d\n", ret
);
181 ret
= snd_soc_codec_set_sysclk(codec
, ARIZONA_CLK_ASYNCCLK
,
182 ARIZONA_CLK_SRC_FLL2
,
186 dev_err(codec
->dev
, "Failed to set ASYNCCLK source: %d\n", ret
);
193 static int tm2_aif2_hw_free(struct snd_pcm_substream
*substream
)
195 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
196 struct snd_soc_codec
*codec
= rtd
->codec
;
200 ret
= snd_soc_codec_set_pll(codec
, WM5110_FLL2
, ARIZONA_FLL_SRC_MCLK1
,
203 dev_err(codec
->dev
, "Failed to stop FLL2: %d\n", ret
);
208 static struct snd_soc_ops tm2_aif2_ops
= {
209 .hw_params
= tm2_aif2_hw_params
,
210 .hw_free
= tm2_aif2_hw_free
,
213 static int tm2_mic_bias(struct snd_soc_dapm_widget
*w
,
214 struct snd_kcontrol
*kcontrol
, int event
)
216 struct snd_soc_card
*card
= w
->dapm
->card
;
217 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
220 case SND_SOC_DAPM_PRE_PMU
:
221 gpiod_set_value_cansleep(priv
->gpio_mic_bias
, 1);
223 case SND_SOC_DAPM_POST_PMD
:
224 gpiod_set_value_cansleep(priv
->gpio_mic_bias
, 0);
231 static int tm2_set_bias_level(struct snd_soc_card
*card
,
232 struct snd_soc_dapm_context
*dapm
,
233 enum snd_soc_bias_level level
)
235 struct snd_soc_pcm_runtime
*rtd
;
237 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[0].name
);
239 if (dapm
->dev
!= rtd
->codec_dai
->dev
)
243 case SND_SOC_BIAS_STANDBY
:
244 if (card
->dapm
.bias_level
== SND_SOC_BIAS_OFF
)
245 tm2_start_sysclk(card
);
247 case SND_SOC_BIAS_OFF
:
248 tm2_stop_sysclk(card
);
257 static struct snd_soc_aux_dev tm2_speaker_amp_dev
;
259 static int tm2_late_probe(struct snd_soc_card
*card
)
261 struct tm2_machine_priv
*priv
= snd_soc_card_get_drvdata(card
);
262 struct snd_soc_dai_link_component dlc
= { 0 };
263 unsigned int ch_map
[] = { 0, 1 };
264 struct snd_soc_dai
*amp_pdm_dai
;
265 struct snd_soc_pcm_runtime
*rtd
;
266 struct snd_soc_dai
*aif1_dai
;
267 struct snd_soc_dai
*aif2_dai
;
270 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[TM2_DAI_AIF1
].name
);
271 aif1_dai
= rtd
->codec_dai
;
272 priv
->codec
= rtd
->codec
;
274 ret
= snd_soc_dai_set_sysclk(aif1_dai
, ARIZONA_CLK_SYSCLK
, 0, 0);
276 dev_err(aif1_dai
->dev
, "Failed to set SYSCLK: %d\n", ret
);
280 rtd
= snd_soc_get_pcm_runtime(card
, card
->dai_link
[TM2_DAI_AIF2
].name
);
281 aif2_dai
= rtd
->codec_dai
;
283 ret
= snd_soc_dai_set_sysclk(aif2_dai
, ARIZONA_CLK_ASYNCCLK
, 0, 0);
285 dev_err(aif2_dai
->dev
, "Failed to set ASYNCCLK: %d\n", ret
);
289 dlc
.of_node
= tm2_speaker_amp_dev
.codec_of_node
;
290 amp_pdm_dai
= snd_soc_find_dai(&dlc
);
294 /* Set the MAX98504 V/I sense PDM Tx DAI channel mapping */
295 ret
= snd_soc_dai_set_channel_map(amp_pdm_dai
, ARRAY_SIZE(ch_map
),
300 ret
= snd_soc_dai_set_tdm_slot(amp_pdm_dai
, 0x3, 0x0, 2, 16);
307 static const struct snd_kcontrol_new tm2_controls
[] = {
308 SOC_DAPM_PIN_SWITCH("HP"),
309 SOC_DAPM_PIN_SWITCH("SPK"),
310 SOC_DAPM_PIN_SWITCH("RCV"),
311 SOC_DAPM_PIN_SWITCH("VPS"),
312 SOC_DAPM_PIN_SWITCH("HDMI"),
314 SOC_DAPM_PIN_SWITCH("Main Mic"),
315 SOC_DAPM_PIN_SWITCH("Sub Mic"),
316 SOC_DAPM_PIN_SWITCH("Third Mic"),
318 SOC_DAPM_PIN_SWITCH("Headset Mic"),
321 static const struct snd_soc_dapm_widget tm2_dapm_widgets
[] = {
322 SND_SOC_DAPM_HP("HP", NULL
),
323 SND_SOC_DAPM_SPK("SPK", NULL
),
324 SND_SOC_DAPM_SPK("RCV", NULL
),
325 SND_SOC_DAPM_LINE("VPS", NULL
),
326 SND_SOC_DAPM_LINE("HDMI", NULL
),
328 SND_SOC_DAPM_MIC("Main Mic", tm2_mic_bias
),
329 SND_SOC_DAPM_MIC("Sub Mic", NULL
),
330 SND_SOC_DAPM_MIC("Third Mic", NULL
),
332 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
335 static const struct snd_soc_component_driver tm2_component
= {
339 static struct snd_soc_dai_driver tm2_ext_dai
[] = {
341 .name
= "Voice call",
347 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
348 SNDRV_PCM_RATE_48000
),
349 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
356 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
|
357 SNDRV_PCM_RATE_48000
),
358 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
368 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
),
369 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
376 .rates
= (SNDRV_PCM_RATE_8000
| SNDRV_PCM_RATE_16000
),
377 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
382 static struct snd_soc_dai_link tm2_dai_links
[] = {
384 .name
= "WM5110 AIF1",
385 .stream_name
= "HiFi Primary",
386 .cpu_dai_name
= SAMSUNG_I2S_DAI
,
387 .codec_dai_name
= "wm5110-aif1",
388 .ops
= &tm2_aif1_ops
,
389 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
390 SND_SOC_DAIFMT_CBM_CFM
,
392 .name
= "WM5110 Voice",
393 .stream_name
= "Voice call",
394 .cpu_dai_name
= SAMSUNG_I2S_DAI
,
395 .codec_dai_name
= "wm5110-aif2",
396 .ops
= &tm2_aif2_ops
,
397 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
398 SND_SOC_DAIFMT_CBM_CFM
,
402 .stream_name
= "Bluetooth",
403 .cpu_dai_name
= SAMSUNG_I2S_DAI
,
404 .codec_dai_name
= "wm5110-aif3",
405 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
406 SND_SOC_DAIFMT_CBM_CFM
,
411 static struct snd_soc_card tm2_card
= {
412 .owner
= THIS_MODULE
,
414 .dai_link
= tm2_dai_links
,
415 .num_links
= ARRAY_SIZE(tm2_dai_links
),
416 .controls
= tm2_controls
,
417 .num_controls
= ARRAY_SIZE(tm2_controls
),
418 .dapm_widgets
= tm2_dapm_widgets
,
419 .num_dapm_widgets
= ARRAY_SIZE(tm2_dapm_widgets
),
420 .aux_dev
= &tm2_speaker_amp_dev
,
423 .late_probe
= tm2_late_probe
,
424 .set_bias_level
= tm2_set_bias_level
,
427 static int tm2_probe(struct platform_device
*pdev
)
429 struct device
*dev
= &pdev
->dev
;
430 struct snd_soc_card
*card
= &tm2_card
;
431 struct tm2_machine_priv
*priv
;
432 struct device_node
*cpu_dai_node
, *codec_dai_node
;
435 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
439 snd_soc_card_set_drvdata(card
, priv
);
442 priv
->gpio_mic_bias
= devm_gpiod_get(dev
, "mic-bias", GPIOD_OUT_HIGH
);
443 if (IS_ERR(priv
->gpio_mic_bias
)) {
444 dev_err(dev
, "Failed to get mic bias gpio\n");
445 return PTR_ERR(priv
->gpio_mic_bias
);
448 ret
= snd_soc_of_parse_card_name(card
, "model");
450 dev_err(dev
, "Card name is not specified\n");
454 ret
= snd_soc_of_parse_audio_routing(card
, "samsung,audio-routing");
456 dev_err(dev
, "Audio routing is not specified or invalid\n");
460 card
->aux_dev
[0].codec_of_node
= of_parse_phandle(dev
->of_node
,
461 "audio-amplifier", 0);
462 if (!card
->aux_dev
[0].codec_of_node
) {
463 dev_err(dev
, "audio-amplifier property invalid or missing\n");
467 cpu_dai_node
= of_parse_phandle(dev
->of_node
, "i2s-controller", 0);
469 dev_err(dev
, "i2s-controllers property invalid or missing\n");
474 codec_dai_node
= of_parse_phandle(dev
->of_node
, "audio-codec", 0);
475 if (!codec_dai_node
) {
476 dev_err(dev
, "audio-codec property invalid or missing\n");
478 goto cpu_dai_node_put
;
481 for (i
= 0; i
< card
->num_links
; i
++) {
482 card
->dai_link
[i
].cpu_name
= NULL
;
483 card
->dai_link
[i
].platform_name
= NULL
;
484 card
->dai_link
[i
].codec_of_node
= codec_dai_node
;
485 card
->dai_link
[i
].cpu_of_node
= cpu_dai_node
;
486 card
->dai_link
[i
].platform_of_node
= cpu_dai_node
;
489 ret
= devm_snd_soc_register_component(dev
, &tm2_component
,
490 tm2_ext_dai
, ARRAY_SIZE(tm2_ext_dai
));
492 dev_err(dev
, "Failed to register component: %d\n", ret
);
493 goto codec_dai_node_put
;
496 ret
= devm_snd_soc_register_card(dev
, card
);
498 dev_err(dev
, "Failed to register card: %d\n", ret
);
499 goto codec_dai_node_put
;
503 of_node_put(codec_dai_node
);
505 of_node_put(cpu_dai_node
);
507 of_node_put(card
->aux_dev
[0].codec_of_node
);
511 static int tm2_pm_prepare(struct device
*dev
)
513 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
515 return tm2_stop_sysclk(card
);
518 static void tm2_pm_complete(struct device
*dev
)
520 struct snd_soc_card
*card
= dev_get_drvdata(dev
);
522 tm2_start_sysclk(card
);
525 static const struct dev_pm_ops tm2_pm_ops
= {
526 .prepare
= tm2_pm_prepare
,
527 .suspend
= snd_soc_suspend
,
528 .resume
= snd_soc_resume
,
529 .complete
= tm2_pm_complete
,
530 .freeze
= snd_soc_suspend
,
531 .thaw
= snd_soc_resume
,
532 .poweroff
= snd_soc_poweroff
,
533 .restore
= snd_soc_resume
,
536 static const struct of_device_id tm2_of_match
[] = {
537 { .compatible
= "samsung,tm2-audio" },
540 MODULE_DEVICE_TABLE(of
, tm2_of_match
);
542 static struct platform_driver tm2_driver
= {
546 .of_match_table
= tm2_of_match
,
550 module_platform_driver(tm2_driver
);
552 MODULE_AUTHOR("Inha Song <ideal.song@samsung.com>");
553 MODULE_DESCRIPTION("ALSA SoC Exynos TM2 Audio Support");
554 MODULE_LICENSE("GPL v2");