cb95e53448538e0e7f1f16b663495bdcd6713954
[deliverable/linux.git] / sound / soc / tegra / harmony.c
1 /*
2 * harmony.c - Harmony machine ASoC driver
3 *
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2010-2011 - NVIDIA, Inc.
6 *
7 * Based on code copyright/by:
8 *
9 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
10 *
11 * Copyright 2007 Wolfson Microelectronics PLC.
12 * Author: Graeme Gregory
13 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 * 02110-1301 USA
28 *
29 */
30
31 #include <asm/mach-types.h>
32
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
36 #include <linux/gpio.h>
37
38 #include <mach/harmony_audio.h>
39
40 #include <sound/core.h>
41 #include <sound/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45
46 #include "../codecs/wm8903.h"
47
48 #include "tegra_das.h"
49 #include "tegra_i2s.h"
50 #include "tegra_pcm.h"
51 #include "tegra_asoc_utils.h"
52
53 #define DRV_NAME "tegra-snd-harmony"
54
55 struct tegra_harmony {
56 struct tegra_asoc_utils_data util_data;
57 struct harmony_audio_platform_data *pdata;
58 int gpio_spkr_en_requested;
59 };
60
61 static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params *params)
63 {
64 struct snd_soc_pcm_runtime *rtd = substream->private_data;
65 struct snd_soc_dai *codec_dai = rtd->codec_dai;
66 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
67 struct snd_soc_codec *codec = rtd->codec;
68 struct snd_soc_card *card = codec->card;
69 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
70 int srate, mclk, mclk_change;
71 int err;
72
73 srate = params_rate(params);
74 switch (srate) {
75 case 64000:
76 case 88200:
77 case 96000:
78 mclk = 128 * srate;
79 break;
80 default:
81 mclk = 256 * srate;
82 break;
83 }
84 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
85 while (mclk < 6000000)
86 mclk *= 2;
87
88 err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
89 &mclk_change);
90 if (err < 0) {
91 dev_err(card->dev, "Can't configure clocks\n");
92 return err;
93 }
94
95 err = snd_soc_dai_set_fmt(codec_dai,
96 SND_SOC_DAIFMT_I2S |
97 SND_SOC_DAIFMT_NB_NF |
98 SND_SOC_DAIFMT_CBS_CFS);
99 if (err < 0) {
100 dev_err(card->dev, "codec_dai fmt not set\n");
101 return err;
102 }
103
104 err = snd_soc_dai_set_fmt(cpu_dai,
105 SND_SOC_DAIFMT_I2S |
106 SND_SOC_DAIFMT_NB_NF |
107 SND_SOC_DAIFMT_CBS_CFS);
108 if (err < 0) {
109 dev_err(card->dev, "cpu_dai fmt not set\n");
110 return err;
111 }
112
113 if (mclk_change) {
114 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
115 SND_SOC_CLOCK_IN);
116 if (err < 0) {
117 dev_err(card->dev, "codec_dai clock not set\n");
118 return err;
119 }
120 }
121
122 return 0;
123 }
124
125 static struct snd_soc_ops harmony_asoc_ops = {
126 .hw_params = harmony_asoc_hw_params,
127 };
128
129 static struct snd_soc_jack harmony_hp_jack;
130
131 static struct snd_soc_jack_pin harmony_hp_jack_pins[] = {
132 {
133 .pin = "Headphone Jack",
134 .mask = SND_JACK_HEADPHONE,
135 },
136 };
137
138 static struct snd_soc_jack_gpio harmony_hp_jack_gpios[] = {
139 {
140 .name = "headphone detect",
141 .report = SND_JACK_HEADPHONE,
142 .debounce_time = 150,
143 .invert = 1,
144 }
145 };
146
147 static struct snd_soc_jack harmony_mic_jack;
148
149 static struct snd_soc_jack_pin harmony_mic_jack_pins[] = {
150 {
151 .pin = "Mic Jack",
152 .mask = SND_JACK_MICROPHONE,
153 },
154 };
155
156 static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
157 struct snd_kcontrol *k, int event)
158 {
159 struct snd_soc_codec *codec = w->codec;
160 struct snd_soc_card *card = codec->card;
161 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
162 struct harmony_audio_platform_data *pdata = harmony->pdata;
163
164 gpio_set_value_cansleep(pdata->gpio_spkr_en,
165 SND_SOC_DAPM_EVENT_ON(event));
166
167 return 0;
168 }
169
170 static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
171 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
172 SND_SOC_DAPM_HP("Headphone Jack", NULL),
173 SND_SOC_DAPM_MIC("Mic Jack", NULL),
174 };
175
176 static const struct snd_soc_dapm_route harmony_audio_map[] = {
177 {"Headphone Jack", NULL, "HPOUTR"},
178 {"Headphone Jack", NULL, "HPOUTL"},
179 {"Int Spk", NULL, "ROP"},
180 {"Int Spk", NULL, "RON"},
181 {"Int Spk", NULL, "LOP"},
182 {"Int Spk", NULL, "LON"},
183 {"Mic Bias", NULL, "Mic Jack"},
184 {"IN1L", NULL, "Mic Bias"},
185 };
186
187 static const struct snd_kcontrol_new harmony_controls[] = {
188 SOC_DAPM_PIN_SWITCH("Int Spk"),
189 };
190
191 static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
192 {
193 struct snd_soc_codec *codec = rtd->codec;
194 struct snd_soc_dapm_context *dapm = &codec->dapm;
195 struct snd_soc_card *card = codec->card;
196 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
197 struct harmony_audio_platform_data *pdata = harmony->pdata;
198 int ret;
199
200 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
201 if (ret) {
202 dev_err(card->dev, "cannot get spkr_en gpio\n");
203 return ret;
204 }
205 harmony->gpio_spkr_en_requested = 1;
206
207 gpio_direction_output(pdata->gpio_spkr_en, 0);
208
209 ret = snd_soc_add_controls(codec, harmony_controls,
210 ARRAY_SIZE(harmony_controls));
211 if (ret < 0)
212 return ret;
213
214 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
215 ARRAY_SIZE(harmony_dapm_widgets));
216
217 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
218 ARRAY_SIZE(harmony_audio_map));
219
220 harmony_hp_jack_gpios[0].gpio = pdata->gpio_hp_det;
221 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
222 &harmony_hp_jack);
223 snd_soc_jack_add_pins(&harmony_hp_jack,
224 ARRAY_SIZE(harmony_hp_jack_pins),
225 harmony_hp_jack_pins);
226 snd_soc_jack_add_gpios(&harmony_hp_jack,
227 ARRAY_SIZE(harmony_hp_jack_gpios),
228 harmony_hp_jack_gpios);
229
230 snd_soc_jack_new(codec, "Mic Jack", SND_JACK_MICROPHONE,
231 &harmony_mic_jack);
232 snd_soc_jack_add_pins(&harmony_mic_jack,
233 ARRAY_SIZE(harmony_mic_jack_pins),
234 harmony_mic_jack_pins);
235 wm8903_mic_detect(codec, &harmony_mic_jack, SND_JACK_MICROPHONE, 0);
236
237 snd_soc_dapm_force_enable_pin(dapm, "Mic Bias");
238
239 snd_soc_dapm_sync(dapm);
240
241 return 0;
242 }
243
244 static struct snd_soc_dai_link harmony_wm8903_dai = {
245 .name = "WM8903",
246 .stream_name = "WM8903 PCM",
247 .codec_name = "wm8903.0-001a",
248 .platform_name = "tegra-pcm-audio",
249 .cpu_dai_name = "tegra-i2s.0",
250 .codec_dai_name = "wm8903-hifi",
251 .init = harmony_asoc_init,
252 .ops = &harmony_asoc_ops,
253 };
254
255 static struct snd_soc_card snd_soc_harmony = {
256 .name = "tegra-harmony",
257 .dai_link = &harmony_wm8903_dai,
258 .num_links = 1,
259 };
260
261 static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
262 {
263 struct snd_soc_card *card = &snd_soc_harmony;
264 struct tegra_harmony *harmony;
265 struct harmony_audio_platform_data *pdata;
266 int ret;
267
268 if (!machine_is_harmony()) {
269 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
270 return -ENODEV;
271 }
272
273 pdata = pdev->dev.platform_data;
274 if (!pdata) {
275 dev_err(&pdev->dev, "no platform data supplied\n");
276 return -EINVAL;
277 }
278
279 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
280 if (!harmony) {
281 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
282 return -ENOMEM;
283 }
284
285 harmony->pdata = pdata;
286
287 ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
288 if (ret)
289 goto err_free_harmony;
290
291 card->dev = &pdev->dev;
292 platform_set_drvdata(pdev, card);
293 snd_soc_card_set_drvdata(card, harmony);
294
295 ret = snd_soc_register_card(card);
296 if (ret) {
297 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
298 ret);
299 goto err_clear_drvdata;
300 }
301
302 return 0;
303
304 err_clear_drvdata:
305 snd_soc_card_set_drvdata(card, NULL);
306 platform_set_drvdata(pdev, NULL);
307 card->dev = NULL;
308 tegra_asoc_utils_fini(&harmony->util_data);
309 err_free_harmony:
310 kfree(harmony);
311 return ret;
312 }
313
314 static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
315 {
316 struct snd_soc_card *card = platform_get_drvdata(pdev);
317 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
318 struct harmony_audio_platform_data *pdata = harmony->pdata;
319
320 snd_soc_unregister_card(card);
321
322 snd_soc_card_set_drvdata(card, NULL);
323 platform_set_drvdata(pdev, NULL);
324 card->dev = NULL;
325
326 tegra_asoc_utils_fini(&harmony->util_data);
327
328 if (harmony->gpio_spkr_en_requested)
329 gpio_free(pdata->gpio_spkr_en);
330
331 kfree(harmony);
332
333 return 0;
334 }
335
336 static struct platform_driver tegra_snd_harmony_driver = {
337 .driver = {
338 .name = DRV_NAME,
339 .owner = THIS_MODULE,
340 },
341 .probe = tegra_snd_harmony_probe,
342 .remove = __devexit_p(tegra_snd_harmony_remove),
343 };
344
345 static int __init snd_tegra_harmony_init(void)
346 {
347 return platform_driver_register(&tegra_snd_harmony_driver);
348 }
349 module_init(snd_tegra_harmony_init);
350
351 static void __exit snd_tegra_harmony_exit(void)
352 {
353 platform_driver_unregister(&tegra_snd_harmony_driver);
354 }
355 module_exit(snd_tegra_harmony_exit);
356
357 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
358 MODULE_DESCRIPTION("Harmony machine ASoC driver");
359 MODULE_LICENSE("GPL");
This page took 0.039356 seconds and 4 git commands to generate.