]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/tegra/tegra_alc5632.c
ASoC: Tegra+ALC5632: Implement device tree support in board file
[karo-tx-linux.git] / sound / soc / tegra / tegra_alc5632.c
1 /*
2 * tegra_alc5632.c  --  Toshiba AC100(PAZ00) machine ASoC driver
3 *
4 * Copyright (C) 2011 The AC100 Kernel Team <ac100@lists.lauchpad.net>
5 *
6 * Authors:  Leon Romanovsky <leon@leon.nu>
7 *           Andrey Danin <danindrey@mail.ru>
8 *           Marc Dietrich <marvin24@gmx.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15 #include <asm/mach-types.h>
16
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/gpio.h>
21
22 #include <sound/core.h>
23 #include <sound/jack.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27
28 #include "../codecs/alc5632.h"
29
30 #include "tegra_das.h"
31 #include "tegra_i2s.h"
32 #include "tegra_pcm.h"
33 #include "tegra_asoc_utils.h"
34
35 #define DRV_NAME "tegra-alc5632"
36
37 struct tegra_alc5632 {
38         struct tegra_asoc_utils_data util_data;
39         struct platform_device *pcm_dev;
40 };
41
42 static int tegra_alc5632_asoc_hw_params(struct snd_pcm_substream *substream,
43                                         struct snd_pcm_hw_params *params)
44 {
45         struct snd_soc_pcm_runtime *rtd = substream->private_data;
46         struct snd_soc_dai *codec_dai = rtd->codec_dai;
47         struct snd_soc_codec *codec = rtd->codec;
48         struct snd_soc_card *card = codec->card;
49         struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
50         int srate, mclk;
51         int err;
52
53         srate = params_rate(params);
54         mclk = 512 * srate;
55
56         err = tegra_asoc_utils_set_rate(&alc5632->util_data, srate, mclk);
57         if (err < 0) {
58                 dev_err(card->dev, "Can't configure clocks\n");
59                 return err;
60         }
61
62         err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
63                                         SND_SOC_CLOCK_IN);
64         if (err < 0) {
65                 dev_err(card->dev, "codec_dai clock not set\n");
66                 return err;
67         }
68
69         return 0;
70 }
71
72 static struct snd_soc_ops tegra_alc5632_asoc_ops = {
73         .hw_params = tegra_alc5632_asoc_hw_params,
74 };
75
76 static struct snd_soc_jack tegra_alc5632_hs_jack;
77
78 static struct snd_soc_jack_pin tegra_alc5632_hs_jack_pins[] = {
79         {
80                 .pin = "Headset Mic",
81                 .mask = SND_JACK_MICROPHONE,
82         },
83         {
84                 .pin = "Headset Stereophone",
85                 .mask = SND_JACK_HEADPHONE,
86         },
87 };
88
89 static const struct snd_soc_dapm_widget tegra_alc5632_dapm_widgets[] = {
90         SND_SOC_DAPM_SPK("Int Spk", NULL),
91         SND_SOC_DAPM_HP("Headset Stereophone", NULL),
92         SND_SOC_DAPM_MIC("Headset Mic", NULL),
93 };
94
95 static const struct snd_soc_dapm_route tegra_alc5632_audio_map[] = {
96         /* Internal Speaker */
97         {"Int Spk", NULL, "SPKOUT"},
98         {"Int Spk", NULL, "SPKOUTN"},
99
100         /* Headset Mic */
101         {"MIC1", NULL, "MICBIAS1"},
102         {"MICBIAS1", NULL, "Headset Mic"},
103
104         /* Headset Stereophone */
105         {"Headset Stereophone", NULL, "HPR"},
106         {"Headset Stereophone", NULL, "HPL"},
107 };
108
109 static const struct snd_kcontrol_new tegra_alc5632_controls[] = {
110         SOC_DAPM_PIN_SWITCH("Int Spk"),
111 };
112
113 static int tegra_alc5632_asoc_init(struct snd_soc_pcm_runtime *rtd)
114 {
115         struct snd_soc_codec *codec = rtd->codec;
116         struct snd_soc_dapm_context *dapm = &codec->dapm;
117
118         snd_soc_jack_new(codec, "Headset Jack", SND_JACK_HEADSET,
119                          &tegra_alc5632_hs_jack);
120         snd_soc_jack_add_pins(&tegra_alc5632_hs_jack,
121                         ARRAY_SIZE(tegra_alc5632_hs_jack_pins),
122                         tegra_alc5632_hs_jack_pins);
123
124         snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
125
126         return 0;
127 }
128
129 static struct snd_soc_dai_link tegra_alc5632_dai = {
130         .name = "ALC5632",
131         .stream_name = "ALC5632 PCM",
132         .platform_name = "tegra-pcm-audio",
133         .codec_dai_name = "alc5632-hifi",
134         .init = tegra_alc5632_asoc_init,
135         .ops = &tegra_alc5632_asoc_ops,
136         .dai_fmt = SND_SOC_DAIFMT_I2S
137                            | SND_SOC_DAIFMT_NB_NF
138                            | SND_SOC_DAIFMT_CBS_CFS,
139 };
140
141 static struct snd_soc_card snd_soc_tegra_alc5632 = {
142         .name = "tegra-alc5632",
143         .owner = THIS_MODULE,
144         .dai_link = &tegra_alc5632_dai,
145         .num_links = 1,
146         .controls = tegra_alc5632_controls,
147         .num_controls = ARRAY_SIZE(tegra_alc5632_controls),
148         .dapm_widgets = tegra_alc5632_dapm_widgets,
149         .num_dapm_widgets = ARRAY_SIZE(tegra_alc5632_dapm_widgets),
150         .dapm_routes = tegra_alc5632_audio_map,
151         .num_dapm_routes = ARRAY_SIZE(tegra_alc5632_audio_map),
152         .fully_routed = true,
153 };
154
155 static __devinit int tegra_alc5632_probe(struct platform_device *pdev)
156 {
157         struct snd_soc_card *card = &snd_soc_tegra_alc5632;
158         struct tegra_alc5632 *alc5632;
159         int ret;
160
161         alc5632 = devm_kzalloc(&pdev->dev,
162                         sizeof(struct tegra_alc5632), GFP_KERNEL);
163         if (!alc5632) {
164                 dev_err(&pdev->dev, "Can't allocate tegra_alc5632\n");
165                 ret = -ENOMEM;
166                 goto err;
167         }
168
169         card->dev = &pdev->dev;
170         platform_set_drvdata(pdev, card);
171         snd_soc_card_set_drvdata(card, alc5632);
172
173         alc5632->pcm_dev = ERR_PTR(-EINVAL);
174
175         if (!(pdev->dev.of_node)) {
176                 dev_err(&pdev->dev, "Must be instantiated using device tree\n");
177                 ret = -EINVAL;
178                 goto err;
179         }
180
181         ret = snd_soc_of_parse_card_name(card, "nvidia,model");
182         if (ret)
183                 goto err;
184
185         ret = snd_soc_of_parse_audio_routing(card, "nvidia,audio-routing");
186         if (ret)
187                 goto err;
188
189         tegra_alc5632_dai.codec_of_node = of_parse_phandle(
190                         pdev->dev.of_node, "nvidia,audio-codec", 0);
191
192         if (!tegra_alc5632_dai.codec_of_node) {
193                 dev_err(&pdev->dev,
194                         "Property 'nvidia,audio-codec' missing or invalid\n");
195                 ret = -EINVAL;
196                 goto err;
197         }
198
199         tegra_alc5632_dai.cpu_dai_of_node = of_parse_phandle(
200                         pdev->dev.of_node, "nvidia,i2s-controller", 0);
201         if (!tegra_alc5632_dai.cpu_dai_of_node) {
202                 dev_err(&pdev->dev,
203                 "Property 'nvidia,i2s-controller' missing or invalid\n");
204                 ret = -EINVAL;
205                 goto err;
206         }
207
208         alc5632->pcm_dev = platform_device_register_simple(
209                 "tegra-pcm-audio", -1, NULL, 0);
210         if (IS_ERR(alc5632->pcm_dev)) {
211                 dev_err(&pdev->dev,
212                         "Can't instantiate tegra-pcm-audio\n");
213                 ret = PTR_ERR(alc5632->pcm_dev);
214                 goto err;
215         }
216
217         ret = tegra_asoc_utils_init(&alc5632->util_data, &pdev->dev);
218         if (ret)
219                 goto err_unregister;
220
221         ret = snd_soc_register_card(card);
222         if (ret) {
223                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
224                         ret);
225                 goto err_fini_utils;
226         }
227
228         return 0;
229
230 err_fini_utils:
231         tegra_asoc_utils_fini(&alc5632->util_data);
232 err_unregister:
233         if (!IS_ERR(alc5632->pcm_dev))
234                 platform_device_unregister(alc5632->pcm_dev);
235 err:
236         return ret;
237 }
238
239 static int __devexit tegra_alc5632_remove(struct platform_device *pdev)
240 {
241         struct snd_soc_card *card = platform_get_drvdata(pdev);
242         struct tegra_alc5632 *alc5632 = snd_soc_card_get_drvdata(card);
243
244         snd_soc_unregister_card(card);
245
246         tegra_asoc_utils_fini(&alc5632->util_data);
247         if (!IS_ERR(alc5632->pcm_dev))
248                 platform_device_unregister(alc5632->pcm_dev);
249
250         return 0;
251 }
252
253 static const struct of_device_id tegra_alc5632_of_match[] __devinitconst = {
254         { .compatible = "nvidia,tegra-audio-alc5632", },
255         {},
256 };
257
258 static struct platform_driver tegra_alc5632_driver = {
259         .driver = {
260                 .name = DRV_NAME,
261                 .owner = THIS_MODULE,
262                 .pm = &snd_soc_pm_ops,
263                 .of_match_table = tegra_alc5632_of_match,
264         },
265         .probe = tegra_alc5632_probe,
266         .remove = __devexit_p(tegra_alc5632_remove),
267 };
268 module_platform_driver(tegra_alc5632_driver);
269
270 MODULE_AUTHOR("Leon Romanovsky <leon@leon.nu>");
271 MODULE_DESCRIPTION("Tegra+ALC5632 machine ASoC driver");
272 MODULE_LICENSE("GPL");
273 MODULE_ALIAS("platform:" DRV_NAME);
274 MODULE_DEVICE_TABLE(of, tegra_alc5632_of_match);