Merge branch 'timers-nohz-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / sound / soc / omap / omap-twl4030.c
1 /*
2  * omap-twl4030.c  --  SoC audio for TI SoC based boards with twl4030 codec
3  *
4  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
5  * All rights reserved.
6  *
7  * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
8  *
9  * This driver replaces the following machine drivers:
10  * omap3beagle (Author: Steve Sakoman <steve@sakoman.com>)
11  * omap3evm (Author: Anuj Aggarwal <anuj.aggarwal@ti.com>)
12  * overo (Author: Steve Sakoman <steve@sakoman.com>)
13  * igep0020 (Author: Enric Balletbo i Serra <eballetbo@iseebcn.com>)
14  * zoom2 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
15  * sdp3430 (Author: Misael Lopez Cruz <misael.lopez@ti.com>)
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License
19  * version 2 as published by the Free Software Foundation.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
29  * 02110-1301 USA
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
47 struct omap_twl4030 {
48         int jack_detect;        /* board can detect jack events */
49         struct snd_soc_jack hs_jack;
50 };
51
52 static 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         unsigned int fmt;
57
58         switch (params_channels(params)) {
59         case 2: /* Stereo I2S mode */
60                 fmt =   SND_SOC_DAIFMT_I2S |
61                         SND_SOC_DAIFMT_NB_NF |
62                         SND_SOC_DAIFMT_CBM_CFM;
63                 break;
64         case 4: /* Four channel TDM mode */
65                 fmt =   SND_SOC_DAIFMT_DSP_A |
66                         SND_SOC_DAIFMT_IB_NF |
67                         SND_SOC_DAIFMT_CBM_CFM;
68                 break;
69         default:
70                 return -EINVAL;
71         }
72
73         return snd_soc_runtime_set_dai_fmt(rtd, fmt);
74 }
75
76 static struct snd_soc_ops omap_twl4030_ops = {
77         .hw_params = omap_twl4030_hw_params,
78 };
79
80 static const struct snd_soc_dapm_widget dapm_widgets[] = {
81         SND_SOC_DAPM_SPK("Earpiece Spk", NULL),
82         SND_SOC_DAPM_SPK("Handsfree Spk", NULL),
83         SND_SOC_DAPM_HP("Headset Stereophone", NULL),
84         SND_SOC_DAPM_SPK("Ext Spk", NULL),
85         SND_SOC_DAPM_SPK("Carkit Spk", NULL),
86
87         SND_SOC_DAPM_MIC("Main Mic", NULL),
88         SND_SOC_DAPM_MIC("Sub Mic", NULL),
89         SND_SOC_DAPM_MIC("Headset Mic", NULL),
90         SND_SOC_DAPM_MIC("Carkit Mic", NULL),
91         SND_SOC_DAPM_MIC("Digital0 Mic", NULL),
92         SND_SOC_DAPM_MIC("Digital1 Mic", NULL),
93         SND_SOC_DAPM_LINE("Line In", NULL),
94 };
95
96 static const struct snd_soc_dapm_route audio_map[] = {
97         /* Headset Stereophone:  HSOL, HSOR */
98         {"Headset Stereophone", NULL, "HSOL"},
99         {"Headset Stereophone", NULL, "HSOR"},
100         /* External Speakers: HFL, HFR */
101         {"Handsfree Spk", NULL, "HFL"},
102         {"Handsfree Spk", NULL, "HFR"},
103         /* External Speakers: PredrivL, PredrivR */
104         {"Ext Spk", NULL, "PREDRIVEL"},
105         {"Ext Spk", NULL, "PREDRIVER"},
106         /* Carkit speakers:  CARKITL, CARKITR */
107         {"Carkit Spk", NULL, "CARKITL"},
108         {"Carkit Spk", NULL, "CARKITR"},
109         /* Earpiece */
110         {"Earpiece Spk", NULL, "EARPIECE"},
111
112         /* External Mics: MAINMIC, SUBMIC with bias */
113         {"MAINMIC", NULL, "Main Mic"},
114         {"Main Mic", NULL, "Mic Bias 1"},
115         {"SUBMIC", NULL, "Sub Mic"},
116         {"Sub Mic", NULL, "Mic Bias 2"},
117         /* Headset Mic: HSMIC with bias */
118         {"HSMIC", NULL, "Headset Mic"},
119         {"Headset Mic", NULL, "Headset Mic Bias"},
120         /* Digital Mics: DIGIMIC0, DIGIMIC1 with bias */
121         {"DIGIMIC0", NULL, "Digital0 Mic"},
122         {"Digital0 Mic", NULL, "Mic Bias 1"},
123         {"DIGIMIC1", NULL, "Digital1 Mic"},
124         {"Digital1 Mic", NULL, "Mic Bias 2"},
125         /* Carkit In: CARKITMIC */
126         {"CARKITMIC", NULL, "Carkit Mic"},
127         /* Aux In: AUXL, AUXR */
128         {"AUXL", NULL, "Line In"},
129         {"AUXR", NULL, "Line In"},
130 };
131
132 /* Headset jack detection DAPM pins */
133 static struct snd_soc_jack_pin hs_jack_pins[] = {
134         {
135                 .pin = "Headset Mic",
136                 .mask = SND_JACK_MICROPHONE,
137         },
138         {
139                 .pin = "Headset Stereophone",
140                 .mask = SND_JACK_HEADPHONE,
141         },
142 };
143
144 /* Headset jack detection gpios */
145 static struct snd_soc_jack_gpio hs_jack_gpios[] = {
146         {
147                 .name = "hsdet-gpio",
148                 .report = SND_JACK_HEADSET,
149                 .debounce_time = 200,
150         },
151 };
152
153 static inline void twl4030_disconnect_pin(struct snd_soc_dapm_context *dapm,
154                                           int connected, char *pin)
155 {
156         if (!connected)
157                 snd_soc_dapm_disable_pin(dapm, pin);
158 }
159
160 static int omap_twl4030_init(struct snd_soc_pcm_runtime *rtd)
161 {
162         struct snd_soc_codec *codec = rtd->codec;
163         struct snd_soc_card *card = rtd->card;
164         struct snd_soc_dapm_context *dapm = &codec->dapm;
165         struct omap_tw4030_pdata *pdata = dev_get_platdata(card->dev);
166         struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
167         int ret = 0;
168
169         /* Headset jack detection only if it is supported */
170         if (priv->jack_detect > 0) {
171                 hs_jack_gpios[0].gpio = priv->jack_detect;
172
173                 ret = snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
174                                        &priv->hs_jack);
175                 if (ret)
176                         return ret;
177
178                 ret = snd_soc_jack_add_pins(&priv->hs_jack,
179                                             ARRAY_SIZE(hs_jack_pins),
180                                             hs_jack_pins);
181                 if (ret)
182                         return ret;
183
184                 ret = snd_soc_jack_add_gpios(&priv->hs_jack,
185                                              ARRAY_SIZE(hs_jack_gpios),
186                                              hs_jack_gpios);
187                 if (ret)
188                         return ret;
189         }
190
191         /*
192          * NULL pdata means we booted with DT. In this case the routing is
193          * provided and the card is fully routed, no need to mark pins.
194          */
195         if (!pdata || !pdata->custom_routing)
196                 return ret;
197
198         /* Disable not connected paths if not used */
199         twl4030_disconnect_pin(dapm, pdata->has_ear, "Earpiece Spk");
200         twl4030_disconnect_pin(dapm, pdata->has_hf, "Handsfree Spk");
201         twl4030_disconnect_pin(dapm, pdata->has_hs, "Headset Stereophone");
202         twl4030_disconnect_pin(dapm, pdata->has_predriv, "Ext Spk");
203         twl4030_disconnect_pin(dapm, pdata->has_carkit, "Carkit Spk");
204
205         twl4030_disconnect_pin(dapm, pdata->has_mainmic, "Main Mic");
206         twl4030_disconnect_pin(dapm, pdata->has_submic, "Sub Mic");
207         twl4030_disconnect_pin(dapm, pdata->has_hsmic, "Headset Mic");
208         twl4030_disconnect_pin(dapm, pdata->has_carkitmic, "Carkit Mic");
209         twl4030_disconnect_pin(dapm, pdata->has_digimic0, "Digital0 Mic");
210         twl4030_disconnect_pin(dapm, pdata->has_digimic1, "Digital1 Mic");
211         twl4030_disconnect_pin(dapm, pdata->has_linein, "Line In");
212
213         return ret;
214 }
215
216 static int omap_twl4030_card_remove(struct snd_soc_card *card)
217 {
218         struct omap_twl4030 *priv = snd_soc_card_get_drvdata(card);
219
220         if (priv->jack_detect > 0)
221                 snd_soc_jack_free_gpios(&priv->hs_jack,
222                                         ARRAY_SIZE(hs_jack_gpios),
223                                         hs_jack_gpios);
224
225         return 0;
226 }
227
228 /* Digital audio interface glue - connects codec <--> CPU */
229 static struct snd_soc_dai_link omap_twl4030_dai_links[] = {
230         {
231                 .name = "TWL4030 HiFi",
232                 .stream_name = "TWL4030 HiFi",
233                 .cpu_dai_name = "omap-mcbsp.2",
234                 .codec_dai_name = "twl4030-hifi",
235                 .platform_name = "omap-mcbsp.2",
236                 .codec_name = "twl4030-codec",
237                 .init = omap_twl4030_init,
238                 .ops = &omap_twl4030_ops,
239         },
240         {
241                 .name = "TWL4030 Voice",
242                 .stream_name = "TWL4030 Voice",
243                 .cpu_dai_name = "omap-mcbsp.3",
244                 .codec_dai_name = "twl4030-voice",
245                 .platform_name = "omap-mcbsp.3",
246                 .codec_name = "twl4030-codec",
247                 .dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF |
248                            SND_SOC_DAIFMT_CBM_CFM,
249         },
250 };
251
252 /* Audio machine driver */
253 static struct snd_soc_card omap_twl4030_card = {
254         .owner = THIS_MODULE,
255         .remove = omap_twl4030_card_remove,
256         .dai_link = omap_twl4030_dai_links,
257         .num_links = ARRAY_SIZE(omap_twl4030_dai_links),
258
259         .dapm_widgets = dapm_widgets,
260         .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
261         .dapm_routes = audio_map,
262         .num_dapm_routes = ARRAY_SIZE(audio_map),
263 };
264
265 static int omap_twl4030_probe(struct platform_device *pdev)
266 {
267         struct omap_tw4030_pdata *pdata = dev_get_platdata(&pdev->dev);
268         struct device_node *node = pdev->dev.of_node;
269         struct snd_soc_card *card = &omap_twl4030_card;
270         struct omap_twl4030 *priv;
271         int ret = 0;
272
273         card->dev = &pdev->dev;
274
275         priv = devm_kzalloc(&pdev->dev, sizeof(struct omap_twl4030), GFP_KERNEL);
276         if (priv == NULL)
277                 return -ENOMEM;
278
279         if (node) {
280                 struct device_node *dai_node;
281                 struct property *prop;
282
283                 if (snd_soc_of_parse_card_name(card, "ti,model")) {
284                         dev_err(&pdev->dev, "Card name is not provided\n");
285                         return -ENODEV;
286                 }
287
288                 dai_node = of_parse_phandle(node, "ti,mcbsp", 0);
289                 if (!dai_node) {
290                         dev_err(&pdev->dev, "McBSP node is not provided\n");
291                         return -EINVAL;
292                 }
293                 omap_twl4030_dai_links[0].cpu_dai_name  = NULL;
294                 omap_twl4030_dai_links[0].cpu_of_node = dai_node;
295
296                 omap_twl4030_dai_links[0].platform_name  = NULL;
297                 omap_twl4030_dai_links[0].platform_of_node = dai_node;
298
299                 dai_node = of_parse_phandle(node, "ti,mcbsp-voice", 0);
300                 if (!dai_node) {
301                         card->num_links = 1;
302                 } else {
303                         omap_twl4030_dai_links[1].cpu_dai_name  = NULL;
304                         omap_twl4030_dai_links[1].cpu_of_node = dai_node;
305
306                         omap_twl4030_dai_links[1].platform_name  = NULL;
307                         omap_twl4030_dai_links[1].platform_of_node = dai_node;
308                 }
309
310                 priv->jack_detect = of_get_named_gpio(node,
311                                                       "ti,jack-det-gpio", 0);
312
313                 /* Optional: audio routing can be provided */
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 = devm_snd_soc_register_card(&pdev->dev, card);
342         if (ret) {
343                 dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
344                         ret);
345                 return ret;
346         }
347
348         return 0;
349 }
350
351 static const struct of_device_id omap_twl4030_of_match[] = {
352         {.compatible = "ti,omap-twl4030", },
353         { },
354 };
355 MODULE_DEVICE_TABLE(of, omap_twl4030_of_match);
356
357 static struct platform_driver omap_twl4030_driver = {
358         .driver = {
359                 .name = "omap-twl4030",
360                 .pm = &snd_soc_pm_ops,
361                 .of_match_table = omap_twl4030_of_match,
362         },
363         .probe = omap_twl4030_probe,
364 };
365
366 module_platform_driver(omap_twl4030_driver);
367
368 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
369 MODULE_DESCRIPTION("ALSA SoC for TI SoC based boards with twl4030 codec");
370 MODULE_LICENSE("GPL");
371 MODULE_ALIAS("platform:omap-twl4030");