76793a93c13343eca73494c996e770635599c1a7
[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/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
44
45 #include "tegra_das.h"
46 #include "tegra_i2s.h"
47 #include "tegra_pcm.h"
48 #include "tegra_asoc_utils.h"
49
50 #define DRV_NAME "tegra-snd-harmony"
51
52 struct tegra_harmony {
53 struct harmony_audio_platform_data *pdata;
54 int gpio_spkr_en_requested;
55 };
56
57 static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
58 struct snd_pcm_hw_params *params)
59 {
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 struct snd_soc_dai *codec_dai = rtd->codec_dai;
62 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
63 struct snd_soc_codec *codec = rtd->codec;
64 struct snd_soc_card *card = codec->card;
65 int srate, mclk, mclk_change;
66 int err;
67
68 srate = params_rate(params);
69 switch (srate) {
70 case 64000:
71 case 88200:
72 case 96000:
73 mclk = 128 * srate;
74 break;
75 default:
76 mclk = 256 * srate;
77 break;
78 }
79 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
80 while (mclk < 6000000)
81 mclk *= 2;
82
83 err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change);
84 if (err < 0) {
85 dev_err(card->dev, "Can't configure clocks\n");
86 return err;
87 }
88
89 err = snd_soc_dai_set_fmt(codec_dai,
90 SND_SOC_DAIFMT_I2S |
91 SND_SOC_DAIFMT_NB_NF |
92 SND_SOC_DAIFMT_CBS_CFS);
93 if (err < 0) {
94 dev_err(card->dev, "codec_dai fmt not set\n");
95 return err;
96 }
97
98 err = snd_soc_dai_set_fmt(cpu_dai,
99 SND_SOC_DAIFMT_I2S |
100 SND_SOC_DAIFMT_NB_NF |
101 SND_SOC_DAIFMT_CBS_CFS);
102 if (err < 0) {
103 dev_err(card->dev, "cpu_dai fmt not set\n");
104 return err;
105 }
106
107 if (mclk_change) {
108 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
109 SND_SOC_CLOCK_IN);
110 if (err < 0) {
111 dev_err(card->dev, "codec_dai clock not set\n");
112 return err;
113 }
114 }
115
116 return 0;
117 }
118
119 static struct snd_soc_ops harmony_asoc_ops = {
120 .hw_params = harmony_asoc_hw_params,
121 };
122
123 static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
124 struct snd_kcontrol *k, int event)
125 {
126 struct snd_soc_codec *codec = w->codec;
127 struct snd_soc_card *card = codec->card;
128 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
129 struct harmony_audio_platform_data *pdata = harmony->pdata;
130
131 gpio_set_value_cansleep(pdata->gpio_spkr_en,
132 !!SND_SOC_DAPM_EVENT_ON(event));
133
134 return 0;
135 }
136
137 static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
138 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
139 SND_SOC_DAPM_HP("Headphone Jack", NULL),
140 SND_SOC_DAPM_MIC("Mic Jack", NULL),
141 };
142
143 static const struct snd_soc_dapm_route harmony_audio_map[] = {
144 {"Headphone Jack", NULL, "HPOUTR"},
145 {"Headphone Jack", NULL, "HPOUTL"},
146 {"Int Spk", NULL, "ROP"},
147 {"Int Spk", NULL, "RON"},
148 {"Int Spk", NULL, "LOP"},
149 {"Int Spk", NULL, "LON"},
150 {"Mic Bias", NULL, "Mic Jack"},
151 {"IN1L", NULL, "Mic Bias"},
152 };
153
154 static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
155 {
156 struct snd_soc_codec *codec = rtd->codec;
157 struct snd_soc_dapm_context *dapm = &codec->dapm;
158 struct snd_soc_card *card = codec->card;
159 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
160 struct harmony_audio_platform_data *pdata = harmony->pdata;
161 int ret;
162
163 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
164 if (ret) {
165 dev_err(card->dev, "cannot get spkr_en gpio\n");
166 return ret;
167 }
168 harmony->gpio_spkr_en_requested = 1;
169
170 gpio_direction_output(pdata->gpio_spkr_en, 0);
171
172 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
173 ARRAY_SIZE(harmony_dapm_widgets));
174
175 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
176 ARRAY_SIZE(harmony_audio_map));
177
178 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
179 snd_soc_dapm_enable_pin(dapm, "Int Spk");
180 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
181 snd_soc_dapm_sync(dapm);
182
183 return 0;
184 }
185
186 static struct snd_soc_dai_link harmony_wm8903_dai = {
187 .name = "WM8903",
188 .stream_name = "WM8903 PCM",
189 .codec_name = "wm8903-codec.0-001a",
190 .platform_name = "tegra-pcm-audio",
191 .cpu_dai_name = "tegra-i2s.0",
192 .codec_dai_name = "wm8903-hifi",
193 .init = harmony_asoc_init,
194 .ops = &harmony_asoc_ops,
195 };
196
197 static struct snd_soc_card snd_soc_harmony = {
198 .name = "tegra-harmony",
199 .dai_link = &harmony_wm8903_dai,
200 .num_links = 1,
201 };
202
203 static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
204 {
205 struct snd_soc_card *card = &snd_soc_harmony;
206 struct tegra_harmony *harmony;
207 struct harmony_audio_platform_data *pdata;
208 int ret;
209
210 if (!machine_is_harmony()) {
211 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
212 return -ENODEV;
213 }
214
215 pdata = pdev->dev.platform_data;
216 if (!pdata) {
217 dev_err(&pdev->dev, "no platform data supplied\n");
218 return -EINVAL;
219 }
220
221 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
222 if (!harmony) {
223 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
224 return -ENOMEM;
225 }
226
227 harmony->pdata = pdata;
228
229 ret = tegra_asoc_utils_init();
230 if (ret)
231 goto err_free_harmony;
232
233 card->dev = &pdev->dev;
234 platform_set_drvdata(pdev, card);
235 snd_soc_card_set_drvdata(card, harmony);
236
237 ret = snd_soc_register_card(card);
238 if (ret) {
239 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
240 ret);
241 goto err_clear_drvdata;
242 }
243
244 return 0;
245
246 err_clear_drvdata:
247 snd_soc_card_set_drvdata(card, NULL);
248 platform_set_drvdata(pdev, NULL);
249 card->dev = NULL;
250 tegra_asoc_utils_fini();
251 err_free_harmony:
252 kfree(harmony);
253 return ret;
254 }
255
256 static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
257 {
258 struct snd_soc_card *card = platform_get_drvdata(pdev);
259 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
260 struct harmony_audio_platform_data *pdata = harmony->pdata;
261
262 snd_soc_unregister_card(card);
263
264 snd_soc_card_set_drvdata(card, NULL);
265 platform_set_drvdata(pdev, NULL);
266 card->dev = NULL;
267
268 tegra_asoc_utils_fini();
269
270 if (harmony->gpio_spkr_en_requested)
271 gpio_free(pdata->gpio_spkr_en);
272
273 kfree(harmony);
274
275 return 0;
276 }
277
278 static struct platform_driver tegra_snd_harmony_driver = {
279 .driver = {
280 .name = DRV_NAME,
281 .owner = THIS_MODULE,
282 },
283 .probe = tegra_snd_harmony_probe,
284 .remove = __devexit_p(tegra_snd_harmony_remove),
285 };
286
287 static int __init snd_tegra_harmony_init(void)
288 {
289 return platform_driver_register(&tegra_snd_harmony_driver);
290 }
291 module_init(snd_tegra_harmony_init);
292
293 static void __exit snd_tegra_harmony_exit(void)
294 {
295 platform_driver_unregister(&tegra_snd_harmony_driver);
296 }
297 module_exit(snd_tegra_harmony_exit);
298
299 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
300 MODULE_DESCRIPTION("Harmony machine ASoC driver");
301 MODULE_LICENSE("GPL");
This page took 0.036331 seconds and 4 git commands to generate.