blob: fcd316e1c94ffbeccffa51a274d077f6dc3ea950 [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
46#include "tegra_das.h"
47#include "tegra_i2s.h"
48#include "tegra_pcm.h"
49#include "tegra_asoc_utils.h"
50
Stephen Warren72de2b12011-01-28 14:26:36 -070051#define DRV_NAME "tegra-snd-harmony"
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070052
Stephen Warren72de2b12011-01-28 14:26:36 -070053struct tegra_harmony {
Stephen Warrend64e57c2011-01-28 14:26:40 -070054 struct tegra_asoc_utils_data util_data;
Stephen Warren6e267642011-01-28 14:26:37 -070055 struct harmony_audio_platform_data *pdata;
56 int gpio_spkr_en_requested;
Stephen Warren72de2b12011-01-28 14:26:36 -070057};
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070058
59static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
60 struct snd_pcm_hw_params *params)
61{
62 struct snd_soc_pcm_runtime *rtd = substream->private_data;
63 struct snd_soc_dai *codec_dai = rtd->codec_dai;
64 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warrenc244d472011-01-28 14:26:39 -070065 struct snd_soc_codec *codec = rtd->codec;
66 struct snd_soc_card *card = codec->card;
Stephen Warrend64e57c2011-01-28 14:26:40 -070067 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070068 int srate, mclk, mclk_change;
69 int err;
70
71 srate = params_rate(params);
72 switch (srate) {
73 case 64000:
74 case 88200:
75 case 96000:
76 mclk = 128 * srate;
77 break;
78 default:
79 mclk = 256 * srate;
80 break;
81 }
82 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
83 while (mclk < 6000000)
84 mclk *= 2;
85
Stephen Warrend64e57c2011-01-28 14:26:40 -070086 err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
87 &mclk_change);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070088 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -070089 dev_err(card->dev, "Can't configure clocks\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070090 return err;
91 }
92
93 err = snd_soc_dai_set_fmt(codec_dai,
94 SND_SOC_DAIFMT_I2S |
95 SND_SOC_DAIFMT_NB_NF |
96 SND_SOC_DAIFMT_CBS_CFS);
97 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -070098 dev_err(card->dev, "codec_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070099 return err;
100 }
101
102 err = snd_soc_dai_set_fmt(cpu_dai,
103 SND_SOC_DAIFMT_I2S |
104 SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBS_CFS);
106 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700107 dev_err(card->dev, "cpu_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700108 return err;
109 }
110
111 if (mclk_change) {
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700112 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
113 SND_SOC_CLOCK_IN);
114 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700115 dev_err(card->dev, "codec_dai clock not set\n");
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700116 return err;
117 }
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700118 }
119
120 return 0;
121}
122
123static struct snd_soc_ops harmony_asoc_ops = {
124 .hw_params = harmony_asoc_hw_params,
125};
126
Stephen Warrenf7d3e402011-02-03 13:56:13 -0700127static struct snd_soc_jack harmony_hp_jack;
128
129static struct snd_soc_jack_pin harmony_hp_jack_pins[] = {
130 {
131 .pin = "Headphone Jack",
132 .mask = SND_JACK_HEADPHONE,
133 },
134};
135
136static struct snd_soc_jack_gpio harmony_hp_jack_gpios[] = {
137 {
138 .name = "headphone detect",
139 .report = SND_JACK_HEADPHONE,
140 .debounce_time = 150,
141 .invert = 1,
142 }
143};
144
Stephen Warren6e267642011-01-28 14:26:37 -0700145static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
146 struct snd_kcontrol *k, int event)
147{
148 struct snd_soc_codec *codec = w->codec;
149 struct snd_soc_card *card = codec->card;
150 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
151 struct harmony_audio_platform_data *pdata = harmony->pdata;
152
153 gpio_set_value_cansleep(pdata->gpio_spkr_en,
Stephen Warrenf9eabc32011-01-31 11:00:17 -0700154 SND_SOC_DAPM_EVENT_ON(event));
Stephen Warren6e267642011-01-28 14:26:37 -0700155
156 return 0;
157}
158
Stephen Warren62ffac42011-01-13 15:22:08 -0700159static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
Stephen Warren6e267642011-01-28 14:26:37 -0700160 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
Stephen Warren62ffac42011-01-13 15:22:08 -0700161 SND_SOC_DAPM_HP("Headphone Jack", NULL),
162 SND_SOC_DAPM_MIC("Mic Jack", NULL),
163};
164
165static const struct snd_soc_dapm_route harmony_audio_map[] = {
166 {"Headphone Jack", NULL, "HPOUTR"},
167 {"Headphone Jack", NULL, "HPOUTL"},
Stephen Warren6e267642011-01-28 14:26:37 -0700168 {"Int Spk", NULL, "ROP"},
169 {"Int Spk", NULL, "RON"},
170 {"Int Spk", NULL, "LOP"},
171 {"Int Spk", NULL, "LON"},
Stephen Warren62ffac42011-01-13 15:22:08 -0700172 {"Mic Bias", NULL, "Mic Jack"},
173 {"IN1L", NULL, "Mic Bias"},
174};
175
176static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
177{
178 struct snd_soc_codec *codec = rtd->codec;
179 struct snd_soc_dapm_context *dapm = &codec->dapm;
Stephen Warren6e267642011-01-28 14:26:37 -0700180 struct snd_soc_card *card = codec->card;
181 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
182 struct harmony_audio_platform_data *pdata = harmony->pdata;
183 int ret;
184
185 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
186 if (ret) {
187 dev_err(card->dev, "cannot get spkr_en gpio\n");
188 return ret;
189 }
190 harmony->gpio_spkr_en_requested = 1;
191
192 gpio_direction_output(pdata->gpio_spkr_en, 0);
Stephen Warren62ffac42011-01-13 15:22:08 -0700193
194 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
195 ARRAY_SIZE(harmony_dapm_widgets));
196
197 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
198 ARRAY_SIZE(harmony_audio_map));
199
Stephen Warren6e267642011-01-28 14:26:37 -0700200 snd_soc_dapm_enable_pin(dapm, "Int Spk");
Stephen Warren62ffac42011-01-13 15:22:08 -0700201 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
202 snd_soc_dapm_sync(dapm);
203
Stephen Warrenf7d3e402011-02-03 13:56:13 -0700204 harmony_hp_jack_gpios[0].gpio = pdata->gpio_hp_det;
205 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
206 &harmony_hp_jack);
207 snd_soc_jack_add_pins(&harmony_hp_jack,
208 ARRAY_SIZE(harmony_hp_jack_pins),
209 harmony_hp_jack_pins);
210 snd_soc_jack_add_gpios(&harmony_hp_jack,
211 ARRAY_SIZE(harmony_hp_jack_gpios),
212 harmony_hp_jack_gpios);
213
Stephen Warren62ffac42011-01-13 15:22:08 -0700214 return 0;
215}
216
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700217static struct snd_soc_dai_link harmony_wm8903_dai = {
218 .name = "WM8903",
219 .stream_name = "WM8903 PCM",
220 .codec_name = "wm8903-codec.0-001a",
221 .platform_name = "tegra-pcm-audio",
222 .cpu_dai_name = "tegra-i2s.0",
223 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700224 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700225 .ops = &harmony_asoc_ops,
226};
227
228static struct snd_soc_card snd_soc_harmony = {
229 .name = "tegra-harmony",
230 .dai_link = &harmony_wm8903_dai,
231 .num_links = 1,
232};
233
Stephen Warren72de2b12011-01-28 14:26:36 -0700234static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700235{
Stephen Warren72de2b12011-01-28 14:26:36 -0700236 struct snd_soc_card *card = &snd_soc_harmony;
237 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700238 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700239 int ret;
240
241 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700242 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700243 return -ENODEV;
244 }
245
Stephen Warren6e267642011-01-28 14:26:37 -0700246 pdata = pdev->dev.platform_data;
247 if (!pdata) {
248 dev_err(&pdev->dev, "no platform data supplied\n");
249 return -EINVAL;
250 }
251
Stephen Warren72de2b12011-01-28 14:26:36 -0700252 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
253 if (!harmony) {
254 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
255 return -ENOMEM;
256 }
257
Stephen Warren6e267642011-01-28 14:26:37 -0700258 harmony->pdata = pdata;
259
Stephen Warrend64e57c2011-01-28 14:26:40 -0700260 ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
Stephen Warren72de2b12011-01-28 14:26:36 -0700261 if (ret)
262 goto err_free_harmony;
263
264 card->dev = &pdev->dev;
265 platform_set_drvdata(pdev, card);
266 snd_soc_card_set_drvdata(card, harmony);
267
268 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700269 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700270 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700271 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700272 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700273 }
274
275 return 0;
276
Stephen Warren72de2b12011-01-28 14:26:36 -0700277err_clear_drvdata:
278 snd_soc_card_set_drvdata(card, NULL);
279 platform_set_drvdata(pdev, NULL);
280 card->dev = NULL;
Stephen Warrend64e57c2011-01-28 14:26:40 -0700281 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700282err_free_harmony:
283 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700284 return ret;
285}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700286
Stephen Warren72de2b12011-01-28 14:26:36 -0700287static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700288{
Stephen Warren72de2b12011-01-28 14:26:36 -0700289 struct snd_soc_card *card = platform_get_drvdata(pdev);
290 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700291 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700292
293 snd_soc_unregister_card(card);
294
295 snd_soc_card_set_drvdata(card, NULL);
296 platform_set_drvdata(pdev, NULL);
297 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700298
Stephen Warrend64e57c2011-01-28 14:26:40 -0700299 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700300
Stephen Warren6e267642011-01-28 14:26:37 -0700301 if (harmony->gpio_spkr_en_requested)
302 gpio_free(pdata->gpio_spkr_en);
303
Stephen Warren72de2b12011-01-28 14:26:36 -0700304 kfree(harmony);
305
306 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700307}
Stephen Warren72de2b12011-01-28 14:26:36 -0700308
309static struct platform_driver tegra_snd_harmony_driver = {
310 .driver = {
311 .name = DRV_NAME,
312 .owner = THIS_MODULE,
313 },
314 .probe = tegra_snd_harmony_probe,
315 .remove = __devexit_p(tegra_snd_harmony_remove),
316};
317
318static int __init snd_tegra_harmony_init(void)
319{
320 return platform_driver_register(&tegra_snd_harmony_driver);
321}
322module_init(snd_tegra_harmony_init);
323
324static void __exit snd_tegra_harmony_exit(void)
325{
326 platform_driver_unregister(&tegra_snd_harmony_driver);
327}
328module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700329
330MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
331MODULE_DESCRIPTION("Harmony machine ASoC driver");
332MODULE_LICENSE("GPL");