xref: /openbmc/linux/sound/soc/samsung/smdk_wm8994.c (revision 28a48058)
1 /*
2  *  smdk_wm8994.c
3  *
4  *  This program is free software; you can redistribute  it and/or modify it
5  *  under  the terms of  the GNU General  Public License as published by the
6  *  Free Software Foundation;  either version 2 of the  License, or (at your
7  *  option) any later version.
8  */
9 
10 #include "../codecs/wm8994.h"
11 #include <sound/pcm_params.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 
15  /*
16   * Default CFG switch settings to use this driver:
17   *	SMDKV310: CFG5-1000, CFG7-111111
18   */
19 
20  /*
21   * Configure audio route as :-
22   * $ amixer sset 'DAC1' on,on
23   * $ amixer sset 'Right Headphone Mux' 'DAC'
24   * $ amixer sset 'Left Headphone Mux' 'DAC'
25   * $ amixer sset 'DAC1R Mixer AIF1.1' on
26   * $ amixer sset 'DAC1L Mixer AIF1.1' on
27   * $ amixer sset 'IN2L' on
28   * $ amixer sset 'IN2L PGA IN2LN' on
29   * $ amixer sset 'MIXINL IN2L' on
30   * $ amixer sset 'AIF1ADC1L Mixer ADC/DMIC' on
31   * $ amixer sset 'IN2R' on
32   * $ amixer sset 'IN2R PGA IN2RN' on
33   * $ amixer sset 'MIXINR IN2R' on
34   * $ amixer sset 'AIF1ADC1R Mixer ADC/DMIC' on
35   */
36 
37 /* SMDK has a 16.934MHZ crystal attached to WM8994 */
38 #define SMDK_WM8994_FREQ 16934000
39 
40 static int smdk_hw_params(struct snd_pcm_substream *substream,
41 	struct snd_pcm_hw_params *params)
42 {
43 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
44 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
45 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
46 	unsigned int pll_out;
47 	int ret;
48 
49 	/* AIF1CLK should be >=3MHz for optimal performance */
50 	if (params_format(params) == SNDRV_PCM_FORMAT_S24_LE)
51 		pll_out = params_rate(params) * 384;
52 	else if (params_rate(params) == 8000 || params_rate(params) == 11025)
53 		pll_out = params_rate(params) * 512;
54 	else
55 		pll_out = params_rate(params) * 256;
56 
57 	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
58 					 | SND_SOC_DAIFMT_NB_NF
59 					 | SND_SOC_DAIFMT_CBM_CFM);
60 	if (ret < 0)
61 		return ret;
62 
63 	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
64 					 | SND_SOC_DAIFMT_NB_NF
65 					 | SND_SOC_DAIFMT_CBM_CFM);
66 	if (ret < 0)
67 		return ret;
68 
69 	ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, WM8994_FLL_SRC_MCLK1,
70 					SMDK_WM8994_FREQ, pll_out);
71 	if (ret < 0)
72 		return ret;
73 
74 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_FLL1,
75 					pll_out, SND_SOC_CLOCK_IN);
76 	if (ret < 0)
77 		return ret;
78 
79 	return 0;
80 }
81 
82 /*
83  * SMDK WM8994 DAI operations.
84  */
85 static struct snd_soc_ops smdk_ops = {
86 	.hw_params = smdk_hw_params,
87 };
88 
89 static int smdk_wm8994_init_paiftx(struct snd_soc_pcm_runtime *rtd)
90 {
91 	struct snd_soc_codec *codec = rtd->codec;
92 	struct snd_soc_dapm_context *dapm = &codec->dapm;
93 
94 	/* HeadPhone */
95 	snd_soc_dapm_enable_pin(dapm, "HPOUT1R");
96 	snd_soc_dapm_enable_pin(dapm, "HPOUT1L");
97 
98 	/* MicIn */
99 	snd_soc_dapm_enable_pin(dapm, "IN1LN");
100 	snd_soc_dapm_enable_pin(dapm, "IN1RN");
101 
102 	/* LineIn */
103 	snd_soc_dapm_enable_pin(dapm, "IN2LN");
104 	snd_soc_dapm_enable_pin(dapm, "IN2RN");
105 
106 	/* Other pins NC */
107 	snd_soc_dapm_nc_pin(dapm, "HPOUT2P");
108 	snd_soc_dapm_nc_pin(dapm, "HPOUT2N");
109 	snd_soc_dapm_nc_pin(dapm, "SPKOUTLN");
110 	snd_soc_dapm_nc_pin(dapm, "SPKOUTLP");
111 	snd_soc_dapm_nc_pin(dapm, "SPKOUTRP");
112 	snd_soc_dapm_nc_pin(dapm, "SPKOUTRN");
113 	snd_soc_dapm_nc_pin(dapm, "LINEOUT1N");
114 	snd_soc_dapm_nc_pin(dapm, "LINEOUT1P");
115 	snd_soc_dapm_nc_pin(dapm, "LINEOUT2N");
116 	snd_soc_dapm_nc_pin(dapm, "LINEOUT2P");
117 	snd_soc_dapm_nc_pin(dapm, "IN1LP");
118 	snd_soc_dapm_nc_pin(dapm, "IN2LP:VXRN");
119 	snd_soc_dapm_nc_pin(dapm, "IN1RP");
120 	snd_soc_dapm_nc_pin(dapm, "IN2RP:VXRP");
121 
122 	return 0;
123 }
124 
125 static struct snd_soc_dai_link smdk_dai[] = {
126 	{ /* Primary DAI i/f */
127 		.name = "WM8994 AIF1",
128 		.stream_name = "Pri_Dai",
129 		.cpu_dai_name = "samsung-i2s.0",
130 		.codec_dai_name = "wm8994-aif1",
131 		.platform_name = "samsung-i2s.0",
132 		.codec_name = "wm8994-codec",
133 		.init = smdk_wm8994_init_paiftx,
134 		.ops = &smdk_ops,
135 	}, { /* Sec_Fifo Playback i/f */
136 		.name = "Sec_FIFO TX",
137 		.stream_name = "Sec_Dai",
138 		.cpu_dai_name = "samsung-i2s-sec",
139 		.codec_dai_name = "wm8994-aif1",
140 		.platform_name = "samsung-i2s-sec",
141 		.codec_name = "wm8994-codec",
142 		.ops = &smdk_ops,
143 	},
144 };
145 
146 static struct snd_soc_card smdk = {
147 	.name = "SMDK-I2S",
148 	.owner = THIS_MODULE,
149 	.dai_link = smdk_dai,
150 	.num_links = ARRAY_SIZE(smdk_dai),
151 };
152 
153 
154 static int smdk_audio_probe(struct platform_device *pdev)
155 {
156 	int ret;
157 	struct device_node *np = pdev->dev.of_node;
158 	struct snd_soc_card *card = &smdk;
159 
160 	card->dev = &pdev->dev;
161 
162 	if (np) {
163 		smdk_dai[0].cpu_dai_name = NULL;
164 		smdk_dai[0].cpu_of_node = of_parse_phandle(np,
165 				"samsung,i2s-controller", 0);
166 		if (!smdk_dai[0].cpu_of_node) {
167 			dev_err(&pdev->dev,
168 			   "Property 'samsung,i2s-controller' missing or invalid\n");
169 			ret = -EINVAL;
170 		}
171 
172 		smdk_dai[0].platform_name = NULL;
173 		smdk_dai[0].platform_of_node = smdk_dai[0].cpu_of_node;
174 	}
175 
176 	ret = snd_soc_register_card(card);
177 
178 	if (ret)
179 		dev_err(&pdev->dev, "snd_soc_register_card() failed:%d\n", ret);
180 
181 	return ret;
182 }
183 
184 static int smdk_audio_remove(struct platform_device *pdev)
185 {
186 	struct snd_soc_card *card = platform_get_drvdata(pdev);
187 
188 	snd_soc_unregister_card(card);
189 
190 	return 0;
191 }
192 
193 #ifdef CONFIG_OF
194 static const struct of_device_id samsung_wm8994_of_match[] = {
195 	{ .compatible = "samsung,smdk-wm8994", },
196 	{},
197 };
198 MODULE_DEVICE_TABLE(of, samsung_wm8994_of_match);
199 #endif /* CONFIG_OF */
200 
201 static struct platform_driver smdk_audio_driver = {
202 	.driver		= {
203 		.name	= "smdk-audio",
204 		.owner	= THIS_MODULE,
205 		.of_match_table = of_match_ptr(samsung_wm8994_of_match),
206 	},
207 	.probe		= smdk_audio_probe,
208 	.remove		= smdk_audio_remove,
209 };
210 
211 module_platform_driver(smdk_audio_driver);
212 
213 MODULE_DESCRIPTION("ALSA SoC SMDK WM8994");
214 MODULE_LICENSE("GPL");
215 MODULE_ALIAS("platform:smdk-audio");
216