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/pcm.h>
42 #include <sound/pcm_params.h>
43 #include <sound/soc.h>
45 #include "tegra_das.h"
46 #include "tegra_i2s.h"
47 #include "tegra_pcm.h"
48 #include "tegra_asoc_utils.h"
50 #define DRV_NAME "tegra-snd-harmony"
52 struct tegra_harmony
{
53 struct harmony_audio_platform_data
*pdata
;
54 int gpio_spkr_en_requested
;
57 static int harmony_asoc_hw_params(struct snd_pcm_substream
*substream
,
58 struct snd_pcm_hw_params
*params
)
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
;
68 srate
= params_rate(params
);
79 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
80 while (mclk
< 6000000)
83 err
= tegra_asoc_utils_set_rate(srate
, mclk
, &mclk_change
);
85 dev_err(card
->dev
, "Can't configure clocks\n");
89 err
= snd_soc_dai_set_fmt(codec_dai
,
91 SND_SOC_DAIFMT_NB_NF
|
92 SND_SOC_DAIFMT_CBS_CFS
);
94 dev_err(card
->dev
, "codec_dai fmt not set\n");
98 err
= snd_soc_dai_set_fmt(cpu_dai
,
100 SND_SOC_DAIFMT_NB_NF
|
101 SND_SOC_DAIFMT_CBS_CFS
);
103 dev_err(card
->dev
, "cpu_dai fmt not set\n");
108 err
= snd_soc_dai_set_sysclk(codec_dai
, 0, mclk
,
111 dev_err(card
->dev
, "codec_dai clock not set\n");
119 static struct snd_soc_ops harmony_asoc_ops
= {
120 .hw_params
= harmony_asoc_hw_params
,
123 static int harmony_event_int_spk(struct snd_soc_dapm_widget
*w
,
124 struct snd_kcontrol
*k
, int event
)
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
;
131 gpio_set_value_cansleep(pdata
->gpio_spkr_en
,
132 !!SND_SOC_DAPM_EVENT_ON(event
));
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
),
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"},
154 static int harmony_asoc_init(struct snd_soc_pcm_runtime
*rtd
)
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
;
163 ret
= gpio_request(pdata
->gpio_spkr_en
, "spkr_en");
165 dev_err(card
->dev
, "cannot get spkr_en gpio\n");
168 harmony
->gpio_spkr_en_requested
= 1;
170 gpio_direction_output(pdata
->gpio_spkr_en
, 0);
172 snd_soc_dapm_new_controls(dapm
, harmony_dapm_widgets
,
173 ARRAY_SIZE(harmony_dapm_widgets
));
175 snd_soc_dapm_add_routes(dapm
, harmony_audio_map
,
176 ARRAY_SIZE(harmony_audio_map
));
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
);
186 static struct snd_soc_dai_link harmony_wm8903_dai
= {
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
,
197 static struct snd_soc_card snd_soc_harmony
= {
198 .name
= "tegra-harmony",
199 .dai_link
= &harmony_wm8903_dai
,
203 static __devinit
int tegra_snd_harmony_probe(struct platform_device
*pdev
)
205 struct snd_soc_card
*card
= &snd_soc_harmony
;
206 struct tegra_harmony
*harmony
;
207 struct harmony_audio_platform_data
*pdata
;
210 if (!machine_is_harmony()) {
211 dev_err(&pdev
->dev
, "Not running on Tegra Harmony!\n");
215 pdata
= pdev
->dev
.platform_data
;
217 dev_err(&pdev
->dev
, "no platform data supplied\n");
221 harmony
= kzalloc(sizeof(struct tegra_harmony
), GFP_KERNEL
);
223 dev_err(&pdev
->dev
, "Can't allocate tegra_harmony\n");
227 harmony
->pdata
= pdata
;
229 ret
= tegra_asoc_utils_init();
231 goto err_free_harmony
;
233 card
->dev
= &pdev
->dev
;
234 platform_set_drvdata(pdev
, card
);
235 snd_soc_card_set_drvdata(card
, harmony
);
237 ret
= snd_soc_register_card(card
);
239 dev_err(&pdev
->dev
, "snd_soc_register_card failed (%d)\n",
241 goto err_clear_drvdata
;
247 snd_soc_card_set_drvdata(card
, NULL
);
248 platform_set_drvdata(pdev
, NULL
);
250 tegra_asoc_utils_fini();
256 static int __devexit
tegra_snd_harmony_remove(struct platform_device
*pdev
)
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
;
262 snd_soc_unregister_card(card
);
264 snd_soc_card_set_drvdata(card
, NULL
);
265 platform_set_drvdata(pdev
, NULL
);
268 tegra_asoc_utils_fini();
270 if (harmony
->gpio_spkr_en_requested
)
271 gpio_free(pdata
->gpio_spkr_en
);
278 static struct platform_driver tegra_snd_harmony_driver
= {
281 .owner
= THIS_MODULE
,
283 .probe
= tegra_snd_harmony_probe
,
284 .remove
= __devexit_p(tegra_snd_harmony_remove
),
287 static int __init
snd_tegra_harmony_init(void)
289 return platform_driver_register(&tegra_snd_harmony_driver
);
291 module_init(snd_tegra_harmony_init
);
293 static void __exit
snd_tegra_harmony_exit(void)
295 platform_driver_unregister(&tegra_snd_harmony_driver
);
297 module_exit(snd_tegra_harmony_exit
);
299 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
300 MODULE_DESCRIPTION("Harmony machine ASoC driver");
301 MODULE_LICENSE("GPL");