blob: 11e2cb8256649b6db08f12e953f33d6dff7f8f95 [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>
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
Stephen Warren72de2b12011-01-28 14:26:36 -070050#define DRV_NAME "tegra-snd-harmony"
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070051
Stephen Warren72de2b12011-01-28 14:26:36 -070052struct tegra_harmony {
Stephen Warrend64e57c2011-01-28 14:26:40 -070053 struct tegra_asoc_utils_data util_data;
Stephen Warren6e267642011-01-28 14:26:37 -070054 struct harmony_audio_platform_data *pdata;
55 int gpio_spkr_en_requested;
Stephen Warren72de2b12011-01-28 14:26:36 -070056};
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070057
58static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
59 struct snd_pcm_hw_params *params)
60{
61 struct snd_soc_pcm_runtime *rtd = substream->private_data;
62 struct snd_soc_dai *codec_dai = rtd->codec_dai;
63 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Stephen Warrenc244d472011-01-28 14:26:39 -070064 struct snd_soc_codec *codec = rtd->codec;
65 struct snd_soc_card *card = codec->card;
Stephen Warrend64e57c2011-01-28 14:26:40 -070066 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070067 int srate, mclk, mclk_change;
68 int err;
69
70 srate = params_rate(params);
71 switch (srate) {
72 case 64000:
73 case 88200:
74 case 96000:
75 mclk = 128 * srate;
76 break;
77 default:
78 mclk = 256 * srate;
79 break;
80 }
81 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
82 while (mclk < 6000000)
83 mclk *= 2;
84
Stephen Warrend64e57c2011-01-28 14:26:40 -070085 err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
86 &mclk_change);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070087 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -070088 dev_err(card->dev, "Can't configure clocks\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070089 return err;
90 }
91
92 err = snd_soc_dai_set_fmt(codec_dai,
93 SND_SOC_DAIFMT_I2S |
94 SND_SOC_DAIFMT_NB_NF |
95 SND_SOC_DAIFMT_CBS_CFS);
96 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -070097 dev_err(card->dev, "codec_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070098 return err;
99 }
100
101 err = snd_soc_dai_set_fmt(cpu_dai,
102 SND_SOC_DAIFMT_I2S |
103 SND_SOC_DAIFMT_NB_NF |
104 SND_SOC_DAIFMT_CBS_CFS);
105 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700106 dev_err(card->dev, "cpu_dai fmt not set\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700107 return err;
108 }
109
110 if (mclk_change) {
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700111 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
112 SND_SOC_CLOCK_IN);
113 if (err < 0) {
Stephen Warrenc244d472011-01-28 14:26:39 -0700114 dev_err(card->dev, "codec_dai clock not set\n");
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700115 return err;
116 }
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700117 }
118
119 return 0;
120}
121
122static struct snd_soc_ops harmony_asoc_ops = {
123 .hw_params = harmony_asoc_hw_params,
124};
125
Stephen Warren6e267642011-01-28 14:26:37 -0700126static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
127 struct snd_kcontrol *k, int event)
128{
129 struct snd_soc_codec *codec = w->codec;
130 struct snd_soc_card *card = codec->card;
131 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
132 struct harmony_audio_platform_data *pdata = harmony->pdata;
133
134 gpio_set_value_cansleep(pdata->gpio_spkr_en,
Stephen Warrenf9eabc32011-01-31 11:00:17 -0700135 SND_SOC_DAPM_EVENT_ON(event));
Stephen Warren6e267642011-01-28 14:26:37 -0700136
137 return 0;
138}
139
Stephen Warren62ffac42011-01-13 15:22:08 -0700140static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
Stephen Warren6e267642011-01-28 14:26:37 -0700141 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
Stephen Warren62ffac42011-01-13 15:22:08 -0700142 SND_SOC_DAPM_HP("Headphone Jack", NULL),
143 SND_SOC_DAPM_MIC("Mic Jack", NULL),
144};
145
146static const struct snd_soc_dapm_route harmony_audio_map[] = {
147 {"Headphone Jack", NULL, "HPOUTR"},
148 {"Headphone Jack", NULL, "HPOUTL"},
Stephen Warren6e267642011-01-28 14:26:37 -0700149 {"Int Spk", NULL, "ROP"},
150 {"Int Spk", NULL, "RON"},
151 {"Int Spk", NULL, "LOP"},
152 {"Int Spk", NULL, "LON"},
Stephen Warren62ffac42011-01-13 15:22:08 -0700153 {"Mic Bias", NULL, "Mic Jack"},
154 {"IN1L", NULL, "Mic Bias"},
155};
156
157static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
158{
159 struct snd_soc_codec *codec = rtd->codec;
160 struct snd_soc_dapm_context *dapm = &codec->dapm;
Stephen Warren6e267642011-01-28 14:26:37 -0700161 struct snd_soc_card *card = codec->card;
162 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
163 struct harmony_audio_platform_data *pdata = harmony->pdata;
164 int ret;
165
166 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
167 if (ret) {
168 dev_err(card->dev, "cannot get spkr_en gpio\n");
169 return ret;
170 }
171 harmony->gpio_spkr_en_requested = 1;
172
173 gpio_direction_output(pdata->gpio_spkr_en, 0);
Stephen Warren62ffac42011-01-13 15:22:08 -0700174
175 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
176 ARRAY_SIZE(harmony_dapm_widgets));
177
178 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
179 ARRAY_SIZE(harmony_audio_map));
180
181 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Stephen Warren6e267642011-01-28 14:26:37 -0700182 snd_soc_dapm_enable_pin(dapm, "Int Spk");
Stephen Warren62ffac42011-01-13 15:22:08 -0700183 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
184 snd_soc_dapm_sync(dapm);
185
186 return 0;
187}
188
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700189static struct snd_soc_dai_link harmony_wm8903_dai = {
190 .name = "WM8903",
191 .stream_name = "WM8903 PCM",
192 .codec_name = "wm8903-codec.0-001a",
193 .platform_name = "tegra-pcm-audio",
194 .cpu_dai_name = "tegra-i2s.0",
195 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700196 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700197 .ops = &harmony_asoc_ops,
198};
199
200static struct snd_soc_card snd_soc_harmony = {
201 .name = "tegra-harmony",
202 .dai_link = &harmony_wm8903_dai,
203 .num_links = 1,
204};
205
Stephen Warren72de2b12011-01-28 14:26:36 -0700206static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700207{
Stephen Warren72de2b12011-01-28 14:26:36 -0700208 struct snd_soc_card *card = &snd_soc_harmony;
209 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700210 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700211 int ret;
212
213 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700214 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700215 return -ENODEV;
216 }
217
Stephen Warren6e267642011-01-28 14:26:37 -0700218 pdata = pdev->dev.platform_data;
219 if (!pdata) {
220 dev_err(&pdev->dev, "no platform data supplied\n");
221 return -EINVAL;
222 }
223
Stephen Warren72de2b12011-01-28 14:26:36 -0700224 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
225 if (!harmony) {
226 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
227 return -ENOMEM;
228 }
229
Stephen Warren6e267642011-01-28 14:26:37 -0700230 harmony->pdata = pdata;
231
Stephen Warrend64e57c2011-01-28 14:26:40 -0700232 ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
Stephen Warren72de2b12011-01-28 14:26:36 -0700233 if (ret)
234 goto err_free_harmony;
235
236 card->dev = &pdev->dev;
237 platform_set_drvdata(pdev, card);
238 snd_soc_card_set_drvdata(card, harmony);
239
240 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700241 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700242 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700243 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700244 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700245 }
246
247 return 0;
248
Stephen Warren72de2b12011-01-28 14:26:36 -0700249err_clear_drvdata:
250 snd_soc_card_set_drvdata(card, NULL);
251 platform_set_drvdata(pdev, NULL);
252 card->dev = NULL;
Stephen Warrend64e57c2011-01-28 14:26:40 -0700253 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700254err_free_harmony:
255 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700256 return ret;
257}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700258
Stephen Warren72de2b12011-01-28 14:26:36 -0700259static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700260{
Stephen Warren72de2b12011-01-28 14:26:36 -0700261 struct snd_soc_card *card = platform_get_drvdata(pdev);
262 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700263 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700264
265 snd_soc_unregister_card(card);
266
267 snd_soc_card_set_drvdata(card, NULL);
268 platform_set_drvdata(pdev, NULL);
269 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700270
Stephen Warrend64e57c2011-01-28 14:26:40 -0700271 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700272
Stephen Warren6e267642011-01-28 14:26:37 -0700273 if (harmony->gpio_spkr_en_requested)
274 gpio_free(pdata->gpio_spkr_en);
275
Stephen Warren72de2b12011-01-28 14:26:36 -0700276 kfree(harmony);
277
278 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700279}
Stephen Warren72de2b12011-01-28 14:26:36 -0700280
281static struct platform_driver tegra_snd_harmony_driver = {
282 .driver = {
283 .name = DRV_NAME,
284 .owner = THIS_MODULE,
285 },
286 .probe = tegra_snd_harmony_probe,
287 .remove = __devexit_p(tegra_snd_harmony_remove),
288};
289
290static int __init snd_tegra_harmony_init(void)
291{
292 return platform_driver_register(&tegra_snd_harmony_driver);
293}
294module_init(snd_tegra_harmony_init);
295
296static void __exit snd_tegra_harmony_exit(void)
297{
298 platform_driver_unregister(&tegra_snd_harmony_driver);
299}
300module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700301
302MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
303MODULE_DESCRIPTION("Harmony machine ASoC driver");
304MODULE_LICENSE("GPL");