2 * harmony.c - Harmony machine ASoC driver
4 * Author: Stephen Warren <swarren@nvidia.com>
5 * Copyright (C) 2010-2011 - NVIDIA, Inc.
7 * Based on code copyright/by:
9 * (c) 2009, 2010 Nvidia Graphics Pvt. Ltd.
11 * Copyright 2007 Wolfson Microelectronics PLC.
12 * Author: Graeme Gregory
13 * graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
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.
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.
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
31 #include <asm/mach-types.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
36 #include <linux/gpio.h>
38 #include <mach/harmony_audio.h>
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>
46 #include "../codecs/wm8903.h"
48 #include "tegra_das.h"
49 #include "tegra_i2s.h"
50 #include "tegra_pcm.h"
51 #include "tegra_asoc_utils.h"
53 #define DRV_NAME "tegra-snd-harmony"
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
;
61 static int harmony_asoc_hw_params(struct snd_pcm_substream
*substream
,
62 struct snd_pcm_hw_params
*params
)
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
;
73 srate
= params_rate(params
);
84 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
85 while (mclk
< 6000000)
88 err
= tegra_asoc_utils_set_rate(&harmony
->util_data
, srate
, mclk
,
91 dev_err(card
->dev
, "Can't configure clocks\n");
95 err
= snd_soc_dai_set_fmt(codec_dai
,
97 SND_SOC_DAIFMT_NB_NF
|
98 SND_SOC_DAIFMT_CBS_CFS
);
100 dev_err(card
->dev
, "codec_dai fmt not set\n");
104 err
= snd_soc_dai_set_fmt(cpu_dai
,
106 SND_SOC_DAIFMT_NB_NF
|
107 SND_SOC_DAIFMT_CBS_CFS
);
109 dev_err(card
->dev
, "cpu_dai fmt not set\n");
114 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
117 dev_err(card
->dev
, "codec_dai clock not set\n");
125 static struct snd_soc_ops harmony_asoc_ops
= {
126 .hw_params
= harmony_asoc_hw_params
,
129 static struct snd_soc_jack harmony_hp_jack
;
131 static struct snd_soc_jack_pin harmony_hp_jack_pins
[] = {
133 .pin
= "Headphone Jack",
134 .mask
= SND_JACK_HEADPHONE
,
138 static struct snd_soc_jack_gpio harmony_hp_jack_gpios
[] = {
140 .name
= "headphone detect",
141 .report
= SND_JACK_HEADPHONE
,
142 .debounce_time
= 150,
147 static struct snd_soc_jack harmony_mic_jack
;
149 static struct snd_soc_jack_pin harmony_mic_jack_pins
[] = {
152 .mask
= SND_JACK_MICROPHONE
,
156 static int harmony_event_int_spk(struct snd_soc_dapm_widget
*w
,
157 struct snd_kcontrol
*k
, int event
)
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
;
164 gpio_set_value_cansleep(pdata
->gpio_spkr_en
,
165 SND_SOC_DAPM_EVENT_ON(event
));
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
),
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"},
187 static const struct snd_kcontrol_new harmony_controls
[] = {
188 SOC_DAPM_PIN_SWITCH("Int Spk"),
191 static int harmony_asoc_init(struct snd_soc_pcm_runtime
*rtd
)
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
;
200 ret
= gpio_request(pdata
->gpio_spkr_en
, "spkr_en");
202 dev_err(card
->dev
, "cannot get spkr_en gpio\n");
205 harmony
->gpio_spkr_en_requested
= 1;
207 gpio_direction_output(pdata
->gpio_spkr_en
, 0);
209 ret
= snd_soc_add_controls(codec
, harmony_controls
,
210 ARRAY_SIZE(harmony_controls
));
214 snd_soc_dapm_new_controls(dapm
, harmony_dapm_widgets
,
215 ARRAY_SIZE(harmony_dapm_widgets
));
217 snd_soc_dapm_add_routes(dapm
, harmony_audio_map
,
218 ARRAY_SIZE(harmony_audio_map
));
220 harmony_hp_jack_gpios
[0].gpio
= pdata
->gpio_hp_det
;
221 snd_soc_jack_new(codec
, "Headphone Jack", SND_JACK_HEADPHONE
,
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
);
230 snd_soc_jack_new(codec
, "Mic Jack", SND_JACK_MICROPHONE
,
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);
237 snd_soc_dapm_force_enable_pin(dapm
, "Mic Bias");
239 snd_soc_dapm_sync(dapm
);
244 static struct snd_soc_dai_link harmony_wm8903_dai
= {
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
,
255 static struct snd_soc_card snd_soc_harmony
= {
256 .name
= "tegra-harmony",
257 .dai_link
= &harmony_wm8903_dai
,
261 static __devinit
int tegra_snd_harmony_probe(struct platform_device
*pdev
)
263 struct snd_soc_card
*card
= &snd_soc_harmony
;
264 struct tegra_harmony
*harmony
;
265 struct harmony_audio_platform_data
*pdata
;
268 if (!machine_is_harmony()) {
269 dev_err(&pdev
->dev
, "Not running on Tegra Harmony!\n");
273 pdata
= pdev
->dev
.platform_data
;
275 dev_err(&pdev
->dev
, "no platform data supplied\n");
279 harmony
= kzalloc(sizeof(struct tegra_harmony
), GFP_KERNEL
);
281 dev_err(&pdev
->dev
, "Can't allocate tegra_harmony\n");
285 harmony
->pdata
= pdata
;
287 ret
= tegra_asoc_utils_init(&harmony
->util_data
, &pdev
->dev
);
289 goto err_free_harmony
;
291 card
->dev
= &pdev
->dev
;
292 platform_set_drvdata(pdev
, card
);
293 snd_soc_card_set_drvdata(card
, harmony
);
295 ret
= snd_soc_register_card(card
);
297 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n",
299 goto err_clear_drvdata
;
305 snd_soc_card_set_drvdata(card
, NULL
);
306 platform_set_drvdata(pdev
, NULL
);
308 tegra_asoc_utils_fini(&harmony
->util_data
);
314 static int __devexit
tegra_snd_harmony_remove(struct platform_device
*pdev
)
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
;
320 snd_soc_unregister_card(card
);
322 snd_soc_card_set_drvdata(card
, NULL
);
323 platform_set_drvdata(pdev
, NULL
);
326 tegra_asoc_utils_fini(&harmony
->util_data
);
328 if (harmony
->gpio_spkr_en_requested
)
329 gpio_free(pdata
->gpio_spkr_en
);
336 static struct platform_driver tegra_snd_harmony_driver
= {
339 .owner
= THIS_MODULE
,
341 .probe
= tegra_snd_harmony_probe
,
342 .remove
= __devexit_p(tegra_snd_harmony_remove
),
345 static int __init
snd_tegra_harmony_init(void)
347 return platform_driver_register(&tegra_snd_harmony_driver
);
349 module_init(snd_tegra_harmony_init
);
351 static void __exit
snd_tegra_harmony_exit(void)
353 platform_driver_unregister(&tegra_snd_harmony_driver
);
355 module_exit(snd_tegra_harmony_exit
);
357 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
358 MODULE_DESCRIPTION("Harmony machine ASoC driver");
359 MODULE_LICENSE("GPL");