blob: be95405df3f4212f61a6ac2395798d10c840b645 [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
Stephen Warren3d8bc392011-02-03 13:56:14 -0700176static const struct snd_kcontrol_new harmony_controls[] = {
177 SOC_DAPM_PIN_SWITCH("Int Spk"),
178};
179
Stephen Warren62ffac42011-01-13 15:22:08 -0700180static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
181{
182 struct snd_soc_codec *codec = rtd->codec;
183 struct snd_soc_dapm_context *dapm = &codec->dapm;
Stephen Warren6e267642011-01-28 14:26:37 -0700184 struct snd_soc_card *card = codec->card;
185 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
186 struct harmony_audio_platform_data *pdata = harmony->pdata;
187 int ret;
188
189 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
190 if (ret) {
191 dev_err(card->dev, "cannot get spkr_en gpio\n");
192 return ret;
193 }
194 harmony->gpio_spkr_en_requested = 1;
195
196 gpio_direction_output(pdata->gpio_spkr_en, 0);
Stephen Warren62ffac42011-01-13 15:22:08 -0700197
Stephen Warren3d8bc392011-02-03 13:56:14 -0700198 ret = snd_soc_add_controls(codec, harmony_controls,
199 ARRAY_SIZE(harmony_controls));
200 if (ret < 0)
201 return ret;
202
Stephen Warren62ffac42011-01-13 15:22:08 -0700203 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
204 ARRAY_SIZE(harmony_dapm_widgets));
205
206 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
207 ARRAY_SIZE(harmony_audio_map));
208
Stephen Warren62ffac42011-01-13 15:22:08 -0700209 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
210 snd_soc_dapm_sync(dapm);
211
Stephen Warrenf7d3e402011-02-03 13:56:13 -0700212 harmony_hp_jack_gpios[0].gpio = pdata->gpio_hp_det;
213 snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
214 &harmony_hp_jack);
215 snd_soc_jack_add_pins(&harmony_hp_jack,
216 ARRAY_SIZE(harmony_hp_jack_pins),
217 harmony_hp_jack_pins);
218 snd_soc_jack_add_gpios(&harmony_hp_jack,
219 ARRAY_SIZE(harmony_hp_jack_gpios),
220 harmony_hp_jack_gpios);
221
Stephen Warren62ffac42011-01-13 15:22:08 -0700222 return 0;
223}
224
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700225static struct snd_soc_dai_link harmony_wm8903_dai = {
226 .name = "WM8903",
227 .stream_name = "WM8903 PCM",
228 .codec_name = "wm8903-codec.0-001a",
229 .platform_name = "tegra-pcm-audio",
230 .cpu_dai_name = "tegra-i2s.0",
231 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700232 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700233 .ops = &harmony_asoc_ops,
234};
235
236static struct snd_soc_card snd_soc_harmony = {
237 .name = "tegra-harmony",
238 .dai_link = &harmony_wm8903_dai,
239 .num_links = 1,
240};
241
Stephen Warren72de2b12011-01-28 14:26:36 -0700242static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700243{
Stephen Warren72de2b12011-01-28 14:26:36 -0700244 struct snd_soc_card *card = &snd_soc_harmony;
245 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700246 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700247 int ret;
248
249 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700250 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700251 return -ENODEV;
252 }
253
Stephen Warren6e267642011-01-28 14:26:37 -0700254 pdata = pdev->dev.platform_data;
255 if (!pdata) {
256 dev_err(&pdev->dev, "no platform data supplied\n");
257 return -EINVAL;
258 }
259
Stephen Warren72de2b12011-01-28 14:26:36 -0700260 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
261 if (!harmony) {
262 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
263 return -ENOMEM;
264 }
265
Stephen Warren6e267642011-01-28 14:26:37 -0700266 harmony->pdata = pdata;
267
Stephen Warrend64e57c2011-01-28 14:26:40 -0700268 ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
Stephen Warren72de2b12011-01-28 14:26:36 -0700269 if (ret)
270 goto err_free_harmony;
271
272 card->dev = &pdev->dev;
273 platform_set_drvdata(pdev, card);
274 snd_soc_card_set_drvdata(card, harmony);
275
276 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700277 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700278 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700279 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700280 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700281 }
282
283 return 0;
284
Stephen Warren72de2b12011-01-28 14:26:36 -0700285err_clear_drvdata:
286 snd_soc_card_set_drvdata(card, NULL);
287 platform_set_drvdata(pdev, NULL);
288 card->dev = NULL;
Stephen Warrend64e57c2011-01-28 14:26:40 -0700289 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700290err_free_harmony:
291 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700292 return ret;
293}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700294
Stephen Warren72de2b12011-01-28 14:26:36 -0700295static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700296{
Stephen Warren72de2b12011-01-28 14:26:36 -0700297 struct snd_soc_card *card = platform_get_drvdata(pdev);
298 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700299 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700300
301 snd_soc_unregister_card(card);
302
303 snd_soc_card_set_drvdata(card, NULL);
304 platform_set_drvdata(pdev, NULL);
305 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700306
Stephen Warrend64e57c2011-01-28 14:26:40 -0700307 tegra_asoc_utils_fini(&harmony->util_data);
Stephen Warren72de2b12011-01-28 14:26:36 -0700308
Stephen Warren6e267642011-01-28 14:26:37 -0700309 if (harmony->gpio_spkr_en_requested)
310 gpio_free(pdata->gpio_spkr_en);
311
Stephen Warren72de2b12011-01-28 14:26:36 -0700312 kfree(harmony);
313
314 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700315}
Stephen Warren72de2b12011-01-28 14:26:36 -0700316
317static struct platform_driver tegra_snd_harmony_driver = {
318 .driver = {
319 .name = DRV_NAME,
320 .owner = THIS_MODULE,
321 },
322 .probe = tegra_snd_harmony_probe,
323 .remove = __devexit_p(tegra_snd_harmony_remove),
324};
325
326static int __init snd_tegra_harmony_init(void)
327{
328 return platform_driver_register(&tegra_snd_harmony_driver);
329}
330module_init(snd_tegra_harmony_init);
331
332static void __exit snd_tegra_harmony_exit(void)
333{
334 platform_driver_unregister(&tegra_snd_harmony_driver);
335}
336module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700337
338MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
339MODULE_DESCRIPTION("Harmony machine ASoC driver");
340MODULE_LICENSE("GPL");