ALSA: usb-audio: Avoid access before bLength check in build_audio_procunit()
[linux/fpc-iii.git] / sound / soc / omap / omap-abe-twl6040.c
blob07af30017b48864bd88e9ee3ab5abe18bca6a2b6
1 /*
2 * omap-abe-twl6040.c -- SoC audio for TI OMAP based boards with ABE and
3 * twl6040 codec
5 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/mfd/twl6040.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/soc.h>
32 #include <sound/jack.h>
34 #include "omap-dmic.h"
35 #include "omap-mcpdm.h"
36 #include "../codecs/twl6040.h"
38 struct abe_twl6040 {
39 struct snd_soc_card card;
40 struct snd_soc_dai_link dai_links[2];
41 int jack_detection; /* board can detect jack events */
42 int mclk_freq; /* MCLK frequency speed for twl6040 */
45 struct platform_device *dmic_codec_dev;
47 static int omap_abe_hw_params(struct snd_pcm_substream *substream,
48 struct snd_pcm_hw_params *params)
50 struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 struct snd_soc_card *card = rtd->card;
53 struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
54 int clk_id, freq;
55 int ret;
57 clk_id = twl6040_get_clk_id(rtd->codec);
58 if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
59 freq = priv->mclk_freq;
60 else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
61 freq = 32768;
62 else
63 return -EINVAL;
65 /* set the codec mclk */
66 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
67 SND_SOC_CLOCK_IN);
68 if (ret) {
69 printk(KERN_ERR "can't set codec system clock\n");
70 return ret;
72 return ret;
75 static struct snd_soc_ops omap_abe_ops = {
76 .hw_params = omap_abe_hw_params,
79 static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
80 struct snd_pcm_hw_params *params)
82 struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
84 int ret = 0;
86 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
87 19200000, SND_SOC_CLOCK_IN);
88 if (ret < 0) {
89 printk(KERN_ERR "can't set DMIC cpu system clock\n");
90 return ret;
92 ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
93 SND_SOC_CLOCK_OUT);
94 if (ret < 0) {
95 printk(KERN_ERR "can't set DMIC output clock\n");
96 return ret;
98 return 0;
101 static struct snd_soc_ops omap_abe_dmic_ops = {
102 .hw_params = omap_abe_dmic_hw_params,
105 /* Headset jack */
106 static struct snd_soc_jack hs_jack;
108 /*Headset jack detection DAPM pins */
109 static struct snd_soc_jack_pin hs_jack_pins[] = {
111 .pin = "Headset Mic",
112 .mask = SND_JACK_MICROPHONE,
115 .pin = "Headset Stereophone",
116 .mask = SND_JACK_HEADPHONE,
120 /* SDP4430 machine DAPM */
121 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
122 /* Outputs */
123 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
124 SND_SOC_DAPM_SPK("Earphone Spk", NULL),
125 SND_SOC_DAPM_SPK("Ext Spk", NULL),
126 SND_SOC_DAPM_LINE("Line Out", NULL),
127 SND_SOC_DAPM_SPK("Vibrator", NULL),
129 /* Inputs */
130 SND_SOC_DAPM_MIC("Headset Mic", NULL),
131 SND_SOC_DAPM_MIC("Main Handset Mic", NULL),
132 SND_SOC_DAPM_MIC("Sub Handset Mic", NULL),
133 SND_SOC_DAPM_LINE("Line In", NULL),
135 /* Digital microphones */
136 SND_SOC_DAPM_MIC("Digital Mic", NULL),
139 static const struct snd_soc_dapm_route audio_map[] = {
140 /* Routings for outputs */
141 {"Headset Stereophone", NULL, "HSOL"},
142 {"Headset Stereophone", NULL, "HSOR"},
144 {"Earphone Spk", NULL, "EP"},
146 {"Ext Spk", NULL, "HFL"},
147 {"Ext Spk", NULL, "HFR"},
149 {"Line Out", NULL, "AUXL"},
150 {"Line Out", NULL, "AUXR"},
152 {"Vibrator", NULL, "VIBRAL"},
153 {"Vibrator", NULL, "VIBRAR"},
155 /* Routings for inputs */
156 {"HSMIC", NULL, "Headset Mic"},
157 {"Headset Mic", NULL, "Headset Mic Bias"},
159 {"MAINMIC", NULL, "Main Handset Mic"},
160 {"Main Handset Mic", NULL, "Main Mic Bias"},
162 {"SUBMIC", NULL, "Sub Handset Mic"},
163 {"Sub Handset Mic", NULL, "Main Mic Bias"},
165 {"AFML", NULL, "Line In"},
166 {"AFMR", NULL, "Line In"},
169 static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
171 struct snd_soc_codec *codec = rtd->codec;
172 struct snd_soc_card *card = rtd->card;
173 struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
174 int hs_trim;
175 int ret = 0;
178 * Configure McPDM offset cancellation based on the HSOTRIM value from
179 * twl6040.
181 hs_trim = twl6040_get_trim_value(codec, TWL6040_TRIM_HSOTRIM);
182 omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
183 TWL6040_HSF_TRIM_RIGHT(hs_trim));
185 /* Headset jack detection only if it is supported */
186 if (priv->jack_detection) {
187 ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
188 SND_JACK_HEADSET, &hs_jack,
189 hs_jack_pins,
190 ARRAY_SIZE(hs_jack_pins));
191 if (ret)
192 return ret;
194 twl6040_hs_jack_detect(codec, &hs_jack, SND_JACK_HEADSET);
197 return 0;
200 static const struct snd_soc_dapm_route dmic_audio_map[] = {
201 {"DMic", NULL, "Digital Mic"},
202 {"Digital Mic", NULL, "Digital Mic1 Bias"},
205 static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
207 struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
209 return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
210 ARRAY_SIZE(dmic_audio_map));
213 static int omap_abe_probe(struct platform_device *pdev)
215 struct device_node *node = pdev->dev.of_node;
216 struct snd_soc_card *card;
217 struct device_node *dai_node;
218 struct abe_twl6040 *priv;
219 int num_links = 0;
220 int ret = 0;
222 if (!node) {
223 dev_err(&pdev->dev, "of node is missing.\n");
224 return -ENODEV;
227 priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
228 if (priv == NULL)
229 return -ENOMEM;
231 card = &priv->card;
232 card->dev = &pdev->dev;
233 card->owner = THIS_MODULE;
234 card->dapm_widgets = twl6040_dapm_widgets;
235 card->num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets);
236 card->dapm_routes = audio_map;
237 card->num_dapm_routes = ARRAY_SIZE(audio_map);
239 if (snd_soc_of_parse_card_name(card, "ti,model")) {
240 dev_err(&pdev->dev, "Card name is not provided\n");
241 return -ENODEV;
244 ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing");
245 if (ret) {
246 dev_err(&pdev->dev, "Error while parsing DAPM routing\n");
247 return ret;
250 dai_node = of_parse_phandle(node, "ti,mcpdm", 0);
251 if (!dai_node) {
252 dev_err(&pdev->dev, "McPDM node is not provided\n");
253 return -EINVAL;
256 priv->dai_links[0].name = "DMIC";
257 priv->dai_links[0].stream_name = "TWL6040";
258 priv->dai_links[0].cpu_of_node = dai_node;
259 priv->dai_links[0].platform_of_node = dai_node;
260 priv->dai_links[0].codec_dai_name = "twl6040-legacy";
261 priv->dai_links[0].codec_name = "twl6040-codec";
262 priv->dai_links[0].init = omap_abe_twl6040_init;
263 priv->dai_links[0].ops = &omap_abe_ops;
265 dai_node = of_parse_phandle(node, "ti,dmic", 0);
266 if (dai_node) {
267 num_links = 2;
268 priv->dai_links[1].name = "TWL6040";
269 priv->dai_links[1].stream_name = "DMIC Capture";
270 priv->dai_links[1].cpu_of_node = dai_node;
271 priv->dai_links[1].platform_of_node = dai_node;
272 priv->dai_links[1].codec_dai_name = "dmic-hifi";
273 priv->dai_links[1].codec_name = "dmic-codec";
274 priv->dai_links[1].init = omap_abe_dmic_init;
275 priv->dai_links[1].ops = &omap_abe_dmic_ops;
276 } else {
277 num_links = 1;
280 priv->jack_detection = of_property_read_bool(node, "ti,jack-detection");
281 of_property_read_u32(node, "ti,mclk-freq", &priv->mclk_freq);
282 if (!priv->mclk_freq) {
283 dev_err(&pdev->dev, "MCLK frequency not provided\n");
284 return -EINVAL;
287 card->fully_routed = 1;
289 if (!priv->mclk_freq) {
290 dev_err(&pdev->dev, "MCLK frequency missing\n");
291 return -ENODEV;
294 card->dai_link = priv->dai_links;
295 card->num_links = num_links;
297 snd_soc_card_set_drvdata(card, priv);
299 ret = devm_snd_soc_register_card(&pdev->dev, card);
300 if (ret)
301 dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
302 ret);
304 return ret;
307 static const struct of_device_id omap_abe_of_match[] = {
308 {.compatible = "ti,abe-twl6040", },
309 { },
311 MODULE_DEVICE_TABLE(of, omap_abe_of_match);
313 static struct platform_driver omap_abe_driver = {
314 .driver = {
315 .name = "omap-abe-twl6040",
316 .pm = &snd_soc_pm_ops,
317 .of_match_table = omap_abe_of_match,
319 .probe = omap_abe_probe,
322 static int __init omap_abe_init(void)
324 int ret;
326 dmic_codec_dev = platform_device_register_simple("dmic-codec", -1, NULL,
328 if (IS_ERR(dmic_codec_dev)) {
329 pr_err("%s: dmic-codec device registration failed\n", __func__);
330 return PTR_ERR(dmic_codec_dev);
333 ret = platform_driver_register(&omap_abe_driver);
334 if (ret) {
335 pr_err("%s: platform driver registration failed\n", __func__);
336 platform_device_unregister(dmic_codec_dev);
339 return ret;
341 module_init(omap_abe_init);
343 static void __exit omap_abe_exit(void)
345 platform_driver_unregister(&omap_abe_driver);
346 platform_device_unregister(dmic_codec_dev);
348 module_exit(omap_abe_exit);
350 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
351 MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
352 MODULE_LICENSE("GPL");
353 MODULE_ALIAS("platform:omap-abe-twl6040");