1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/platform_device.h>
34#include <linux/platform_data/omap-twl4030.h>
35#include <linux/module.h>
36#include <linux/of.h>
37#include <linux/gpio.h>
38#include <linux/of_gpio.h>
39
40#include <sound/core.h>
41#include <sound/pcm.h>
42#include <sound/soc.h>
43#include <sound/jack.h>
44
45#include "omap-mcbsp.h"
46
47struct omap_twl4030 {
48 int jack_detect;
49 struct snd_soc_jack hs_jack;
50};
51
52static int omap_twl4030_hw_params(struct snd_pcm_substream *substream,
53 struct snd_pcm_hw_params *params)
54{
55 struct snd_soc_pcm_runtime *rtd = substream->private_data;
56 struct snd_soc_dai *codec_dai = rtd->codec_dai;
57 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
58 struct snd_soc_codec *codec = rtd->codec;
59 struct snd_soc_card *card = codec->card;
60 unsigned int fmt;
61 int ret;
62
63 switch (params_channels(params)) {
64 case 2:
65 fmt = SND_SOC_DAIFMT_I2S |
66 SND_SOC_DAIFMT_NB_NF |
67 SND_SOC_DAIFMT_CBM_CFM;
68 break;
69 case 4:
70 fmt = SND_SOC_DAIFMT_DSP_A |
71 SND_SOC_DAIFMT_IB_NF |
72 SND_SOC_DAIFMT_CBM_CFM;
73 break;
74 default:
75 return -EINVAL;
76 }
77
78
79 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
80 if (ret < 0) {
81 dev_err(card->dev, "can't set codec DAI configuration\n");
82 return ret;
83 }
84
85
86 ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
87 if (ret < 0) {
88 dev_err(card->dev, "can't set cpu DAI configuration\n");
89 return ret;
90 }
91
92 return 0;
93}
94
95static struct snd_soc_ops omap_twl4030_ops = {
96 .hw_params = omap_twl4030_hw_params,
97};
98
99static const struct snd_soc_dapm_widget dapm_widgets[] = {
100 SND_SOC_DAPM_SPK("Earpiece Spk", NULL),
101 SND_SOC_DAPM_SPK("Handsfree Spk", NULL),
102 SND_SOC_DAPM_HP("Headset Stereophone", NULL),
103 SND_SOC_DAPM_SPK("Ext Spk", NULL),
104 SND_SOC_DAPM_SPK("Carkit Spk", NULL),
105
106 SND_SOC_DAPM_MIC("Main Mic", NULL),
107 SND_SOC_DAPM_MIC("Sub Mic", NULL),
108 SND_SOC_DAPM_MIC("Headset Mic", NULL),
109 SND_SOC_DAPM_MIC("Carkit Mic", NULL),
110 SND_SOC_DAPM_MIC("Digital0 Mic", NULL),
111 SND_SOC_DAPM_MIC("Digital1 Mic", NULL),
112 SND_SOC_DAPM_LINE("Line In", NULL),
113};
114
115static const struct snd_soc_dapm_route audio_map[] = {
116
117 {"Headset Stereophone", NULL, "HSOL"},
118 {"Headset Stereophone", NULL, "HSOR"},
119
120 {"Handsfree Spk", NULL, "HFL"},
121 {"Handsfree Spk", NULL, "HFR"},
122
123 {"Ext Spk", NULL, "PREDRIVEL"},
124 {"Ext Spk", NULL, "PREDRIVER"},
125
126 {"Carkit Spk", NULL, "CARKITL"},
127 {"Carkit Spk", NULL, "CARKITR"},
128
129 {"Earpiece Spk", NULL, "EARPIECE"},
130
131
132 {"MAINMIC", NULL, "Main Mic"},
133 {"Main Mic", NULL, "Mic Bias 1"},
134 {"SUBMIC", NULL, "Sub Mic"},
135 {"Sub Mic", NULL, "Mic Bias 2"},
136
137 {"HSMIC", NULL, "Headset Mic"},
138 {"Headset Mic", NULL, "Headset Mic Bias"},
139
140 {"DIGIMIC0", NULL, "Digital0 Mic"},
141 {"Digital0 Mic", NULL, "Mic Bias 1"},
142 {"DIGIMIC1", NULL, "Digital1 Mic"},
143 {"Digital1 Mic", NULL, "Mic Bias 2"},
144
145 {"CARKITMIC", NULL, "Carkit Mic"},
146
147 {"AUXL", NULL, "Line In"},
148 {"AUXR", NULL, "Line In"},
149};
150
151
152static struct snd_soc_jack_pin hs_jack_pins[] = {
153 {
154 .pin = "Headset Mic",
155 .mask = SND_JACK_MICROPHONE,
156 },
157 {
158 .pin = "Headset Stereophone",
159 .mask = SND_JACK_HEADPHONE,
160 },
161};
162
163
164static struct snd_soc_jack_gpio hs_jack_gpios[] = {
165 {
166 .name = "hsdet-gpio",
167 .report = SND_JACK_HEADSET,
168 .debounce_time = 200,
169 },
170};
171
172static inline void twl4030_disconnect_pin(struct snd_soc_dapm_context *dapm,
173 int connected, char *pin)
174{
175 if (!connected)
176 snd_soc_dapm_disable_pin(dapm, pin);
177}
178
179static int omap_twl4030_init(struct snd_soc_pcm_runtime *rtd)
180{
181 struct snd_soc_codec *codec = rtd->codec;
182 struct snd_soc_card *card = codec->card;
183 struct snd_soc_dapm_context *dapm = &codec->dapm;
184 struct omap_tw4030_pdata *pdata = dev_get_platdata(card->dev);
185 struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
186 int ret = 0;
187
188
189 if (priv->jack_detect > 0) {
190 hs_jack_gpios[0].gpio = priv->jack_detect;
191
192 ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
193 &priv->hs_jack);
194 if (ret)
195 return ret;
196
197 ret = snd_soc_jack_add_pins(&priv->hs_jack,
198 ARRAY_SIZE(hs_jack_pins),
199 hs_jack_pins);
200 if (ret)
201 return ret;
202
203 ret = snd_soc_jack_add_gpios(&priv->hs_jack,
204 ARRAY_SIZE(hs_jack_gpios),
205 hs_jack_gpios);
206 if (ret)
207 return ret;
208 }
209
210
211
212
213
214 if (!pdata || !pdata->custom_routing)
215 return ret;
216
217
218 twl4030_disconnect_pin(dapm, pdata->has_ear, "Earpiece Spk");
219 twl4030_disconnect_pin(dapm, pdata->has_hf, "Handsfree Spk");
220 twl4030_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
221 twl4030_disconnect_pin(dapm, pdata->has_predriv, "Ext Spk");
222 twl4030_disconnect_pin(dapm, pdata->has_carkit, "Carkit Spk");
223
224 twl4030_disconnect_pin(dapm, pdata->has_mainmic, "Main Mic");
225 twl4030_disconnect_pin(dapm, pdata->has_submic, "Sub Mic");
226 twl4030_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
227 twl4030_disconnect_pin(dapm, pdata->has_carkitmic, "Carkit Mic");
228 twl4030_disconnect_pin(dapm, pdata->has_digimic0, "Digital0 Mic");
229 twl4030_disconnect_pin(dapm, pdata->has_digimic1, "Digital1 Mic");
230 twl4030_disconnect_pin(dapm, pdata->has_linein, "Line In");
231
232 return ret;
233}
234
235
236static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
237 {
238 .name = "TWL4030 HiFi",
239 .stream_name = "TWL4030 HiFi",
240 .cpu_dai_name = "omap-mcbsp.2",
241 .codec_dai_name = "twl4030-hifi",
242 .platform_name = "omap-pcm-audio",
243 .codec_name = "twl4030-codec",
244 .init = omap_twl4030_init,
245 .ops = &omap_twl4030_ops,
246 },
247 {
248 .name = "TWL4030 Voice",
249 .stream_name = "TWL4030 Voice",
250 .cpu_dai_name = "omap-mcbsp.3",
251 .codec_dai_name = "twl4030-voice",
252 .platform_name = "omap-pcm-audio",
253 .codec_name = "twl4030-codec",
254 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
255 SND_SOC_DAIFMT_CBM_CFM,
256 },
257};
258
259
260static struct snd_soc_card omap_twl4030_card = {
261 .owner = THIS_MODULE,
262 .dai_link = omap_twl4030_dai_links,
263 .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
264
265 .dapm_widgets = dapm_widgets,
266 .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
267 .dapm_routes = audio_map,
268 .num_dapm_routes = ARRAY_SIZE(audio_map),
269};
270
271static int omap_twl4030_probe(struct platform_device *pdev)
272{
273 struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
274 struct device_node *node = pdev->dev.of_node;
275 struct snd_soc_card *card = &omap_twl4030_card;
276 struct omap_twl4030 *priv;
277 int ret = 0;
278
279 card->dev = &pdev->dev;
280
281 priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_twl4030), GFP_KERNEL);
282 if (priv == NULL)
283 return -ENOMEM;
284
285 if (node) {
286 struct device_node *dai_node;
287 struct property *prop;
288
289 if (snd_soc_of_parse_card_name(card, "ti,model")) {
290 dev_err(&pdev->dev, "Card name is not provided\n");
291 return -ENODEV;
292 }
293
294 dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
295 if (!dai_node) {
296 dev_err(&pdev->dev, "McBSP node is not provided\n");
297 return -EINVAL;
298 }
299 omap_twl4030_dai_links[0].cpu_dai_name = NULL;
300 omap_twl4030_dai_links[0].cpu_of_node = dai_node;
301
302 dai_node = of_parse_phandle(node, "ti,mcbsp-voice", 0);
303 if (!dai_node) {
304 card->num_links = 1;
305 } else {
306 omap_twl4030_dai_links[1].cpu_dai_name = NULL;
307 omap_twl4030_dai_links[1].cpu_of_node = dai_node;
308 }
309
310 priv->jack_detect = of_get_named_gpio(node,
311 "ti,jack-det-gpio", 0);
312
313
314 prop = of_find_property(node, "ti,audio-routing", NULL);
315 if (prop) {
316 ret = snd_soc_of_parse_audio_routing(card,
317 "ti,audio-routing");
318 if (ret)
319 return ret;
320
321 card->fully_routed = 1;
322 }
323 } else if (pdata) {
324 if (pdata->card_name) {
325 card->name = pdata->card_name;
326 } else {
327 dev_err(&pdev->dev, "Card name is not provided\n");
328 return -ENODEV;
329 }
330
331 if (!pdata->voice_connected)
332 card->num_links = 1;
333
334 priv->jack_detect = pdata->jack_detect;
335 } else {
336 dev_err(&pdev->dev, "Missing pdata\n");
337 return -ENODEV;
338 }
339
340 snd_soc_card_set_drvdata(card, priv);
341 ret = snd_soc_register_card(card);
342 if (ret) {
343 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
344 ret);
345 return ret;
346 }
347
348 return 0;
349}
350
351static int omap_twl4030_remove(struct platform_device *pdev)
352{
353 struct snd_soc_card *card = platform_get_drvdata(pdev);
354 struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
355
356 if (priv->jack_detect > 0)
357 snd_soc_jack_free_gpios(&priv->hs_jack,
358 ARRAY_SIZE(hs_jack_gpios),
359 hs_jack_gpios);
360 snd_soc_unregister_card(card);
361
362 return 0;
363}
364
365static const struct of_device_id omap_twl4030_of_match[] = {
366 {.compatible = "ti,omap-twl4030", },
367 { },
368};
369MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
370
371static struct platform_driver omap_twl4030_driver = {
372 .driver = {
373 .name = "omap-twl4030",
374 .owner = THIS_MODULE,
375 .pm = &snd_soc_pm_ops,
376 .of_match_table = omap_twl4030_of_match,
377 },
378 .probe = omap_twl4030_probe,
379 .remove = omap_twl4030_remove,
380};
381
382module_platform_driver(omap_twl4030_driver);
383
384MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
385MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
386MODULE_LICENSE("GPL");
387MODULE_ALIAS("platform:omap-twl4030");
388