blob: cb95e53448538e0e7f1f16b663495bdcd6713954 [file] [log] [blame]
Stephen Warrena8bf1ba2011-01-07 22:36:16 -07001/*
2 * harmony.c - Harmony machine ASoC driver
3 *
4 * Author: Stephen Warren <swarren@nvidia.com>
Stephen Warren72de2b12011-01-28 14:26:36 -07005 * Copyright (C) 2010-2011 - NVIDIA, Inc.
Stephen Warrena8bf1ba2011-01-07 22:36:16 -07006 *
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>
Stephen Warren72de2b12011-01-28 14:26:36 -070032
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070033#include <linux/module.h>
Stephen Warren72de2b12011-01-28 14:26:36 -070034#include <linux/platform_device.h>
35#include <linux/slab.h>
Stephen Warren6e267642011-01-28 14:26:37 -070036#include <linux/gpio.h>
37
38#include <mach/harmony_audio.h>
Stephen Warren72de2b12011-01-28 14:26:36 -070039
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070040#include <sound/core.h>
Stephen Warrenf7d3e402011-02-03 13:56:13 -070041#include <sound/jack.h>
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070042#include <sound/pcm.h>
43#include <sound/pcm_params.h>
44#include <sound/soc.h>
45
Stephen Warren41b5f9b2011-02-10 15:37:16 -070046#include "../codecs/wm8903.h"
47
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070048#include "tegra_das.h"
49#include "tegra_i2s.h"
50#include "tegra_pcm.h"
51#include "tegra_asoc_utils.h"
52
Stephen Warren72de2b12011-01-28 14:26:36 -070053#define DRV_NAME "tegra-snd-harmony"
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070054
Stephen Warren72de2b12011-01-28 14:26:36 -070055struct tegra_harmony {
Stephen Warrend64e57c2011-01-28 14:26:40 -070056 struct tegra_asoc_utils_data util_data;
Stephen Warren6e267642011-01-28 14:26:37 -070057 struct harmony_audio_platform_data *pdata;
58 int gpio_spkr_en_requested;
Stephen Warren72de2b12011-01-28 14:26:36 -070059};
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070060
61static 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;
Stephen Warrenc244d472011-01-28 14:26:39 -070067 struct snd_soc_codec *codec = rtd->codec;
68 struct snd_soc_card *card = codec->card;
Stephen Warrend64e57c2011-01-28 14:26:40 -070069 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070070 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
Stephen Warrend64e57c2011-01-28 14:26:40 -070088 err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
89 &mclk_change);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070090 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -070091 dev_err(card->dev, "Can't configure clocks\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070092 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) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700100 dev_err(card->dev, "codec_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700101 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) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700109 dev_err(card->dev, "cpu_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700110 return err;
111 }
112
113 if (mclk_change) {
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700114 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
115 SND_SOC_CLOCK_IN);
116 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700117 dev_err(card->dev, "codec_dai clock not set\n");
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700118 return err;
119 }
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700120 }
121
122 return 0;
123}
124
125static struct snd_soc_ops harmony_asoc_ops = {
126 .hw_params = harmony_asoc_hw_params,
127};
128
Stephen Warrenf7d3e402011-02-03 13:56:13 -0700129static struct snd_soc_jack harmony_hp_jack;
130
131static struct snd_soc_jack_pin harmony_hp_jack_pins[] = {
132 {
133 .pin = "Headphone Jack",
134 .mask = SND_JACK_HEADPHONE,
135 },
136};
137
138static 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
Stephen Warren41b5f9b2011-02-10 15:37:16 -0700147static struct snd_soc_jack harmony_mic_jack;
148
149static struct snd_soc_jack_pin harmony_mic_jack_pins[] = {
150 {
151 .pin = "Mic Jack",
152 .mask = SND_JACK_MICROPHONE,
153 },
154};
155
Stephen Warren6e267642011-01-28 14:26:37 -0700156static 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,
Stephen Warrenf9eabc32011-01-31 11:00:17 -0700165 SND_SOC_DAPM_EVENT_ON(event));
Stephen Warren6e267642011-01-28 14:26:37 -0700166
167 return 0;
168}
169
Stephen Warren62ffac42011-01-13 15:22:08 -0700170static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
Stephen Warren6e267642011-01-28 14:26:37 -0700171 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
Stephen Warren62ffac42011-01-13 15:22:08 -0700172 SND_SOC_DAPM_HP("Headphone Jack", NULL),
173 SND_SOC_DAPM_MIC("Mic Jack", NULL),
174};
175
176static const struct snd_soc_dapm_route harmony_audio_map[] = {
177 {"Headphone Jack", NULL, "HPOUTR"},
178 {"Headphone Jack", NULL, "HPOUTL"},
Stephen Warren6e267642011-01-28 14:26:37 -0700179 {"Int Spk", NULL, "ROP"},
180 {"Int Spk", NULL, "RON"},
181 {"Int Spk", NULL, "LOP"},
182 {"Int Spk", NULL, "LON"},
Stephen Warren62ffac42011-01-13 15:22:08 -0700183 {"Mic Bias", NULL, "Mic Jack"},
184 {"IN1L", NULL, "Mic Bias"},
185};
186
Stephen Warren3d8bc392011-02-03 13:56:14 -0700187static const struct snd_kcontrol_new harmony_controls[] = {
188 SOC_DAPM_PIN_SWITCH("Int Spk"),
189};
190
Stephen Warren62ffac42011-01-13 15:22:08 -0700191static 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;
Stephen Warren6e267642011-01-28 14:26:37 -0700195 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);
Stephen Warren62ffac42011-01-13 15:22:08 -0700208
Stephen Warren3d8bc392011-02-03 13:56:14 -0700209 ret = snd_soc_add_controls(codec, harmony_controls,
210 ARRAY_SIZE(harmony_controls));
211 if (ret < 0)
212 return ret;
213
Stephen Warren62ffac42011-01-13 15:22:08 -0700214 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
Stephen Warrenf7d3e402011-02-03 13:56:13 -0700220 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
Stephen Warren41b5f9b2011-02-10 15:37:16 -0700230 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
Stephen Warren62ffac42011-01-13 15:22:08 -0700241 return 0;
242}
243
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700244static struct snd_soc_dai_link harmony_wm8903_dai = {
245 .name = "WM8903",
246 .stream_name = "WM8903 PCM",
Mark Brown4b592c92011-02-09 13:47:06 +0000247 .codec_name = "wm8903.0-001a",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700248 .platform_name = "tegra-pcm-audio",
249 .cpu_dai_name = "tegra-i2s.0",
250 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700251 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700252 .ops = &harmony_asoc_ops,
253};
254
255static struct snd_soc_card snd_soc_harmony = {
256 .name = "tegra-harmony",
257 .dai_link = &harmony_wm8903_dai,
258 .num_links = 1,
259};
260
Stephen Warren72de2b12011-01-28 14:26:36 -0700261static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700262{
Stephen Warren72de2b12011-01-28 14:26:36 -0700263 struct snd_soc_card *card = &snd_soc_harmony;
264 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700265 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700266 int ret;
267
268 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700269 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700270 return -ENODEV;
271 }
272
Stephen Warren6e267642011-01-28 14:26:37 -0700273 pdata = pdev->dev.platform_data;
274 if (!pdata) {
275 dev_err(&pdev->dev, "no platform data supplied\n");
276 return -EINVAL;
277 }
278
Stephen Warren72de2b12011-01-28 14:26:36 -0700279 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
Stephen Warren6e267642011-01-28 14:26:37 -0700285 harmony->pdata = pdata;
286
Stephen Warrend64e57c2011-01-28 14:26:40 -0700287 ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
Stephen Warren72de2b12011-01-28 14:26:36 -0700288 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);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700296 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700297 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700298 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700299 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700300 }
301
302 return 0;
303
Stephen Warren72de2b12011-01-28 14:26:36 -0700304err_clear_drvdata:
305 snd_soc_card_set_drvdata(card, NULL);
306 platform_set_drvdata(pdev, NULL);
307 card->dev = NULL;
Stephen Warrend64e57c2011-01-28 14:26:40 -0700308 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700309err_free_harmony:
310 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700311 return ret;
312}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700313
Stephen Warren72de2b12011-01-28 14:26:36 -0700314static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700315{
Stephen Warren72de2b12011-01-28 14:26:36 -0700316 struct snd_soc_card *card = platform_get_drvdata(pdev);
317 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700318 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700319
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;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700325
Stephen Warrend64e57c2011-01-28 14:26:40 -0700326 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700327
Stephen Warren6e267642011-01-28 14:26:37 -0700328 if (harmony->gpio_spkr_en_requested)
329 gpio_free(pdata->gpio_spkr_en);
330
Stephen Warren72de2b12011-01-28 14:26:36 -0700331 kfree(harmony);
332
333 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700334}
Stephen Warren72de2b12011-01-28 14:26:36 -0700335
336static 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
345static int __init snd_tegra_harmony_init(void)
346{
347 return platform_driver_register(&tegra_snd_harmony_driver);
348}
349module_init(snd_tegra_harmony_init);
350
351static void __exit snd_tegra_harmony_exit(void)
352{
353 platform_driver_unregister(&tegra_snd_harmony_driver);
354}
355module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700356
357MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
358MODULE_DESCRIPTION("Harmony machine ASoC driver");
359MODULE_LICENSE("GPL");