blob: b5311a3ee95f49ae5994e0c2c7790eb8fbf0ab70 [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"
51#define PREFIX DRV_NAME ": "
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070052
Stephen Warren72de2b12011-01-28 14:26:36 -070053struct tegra_harmony {
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;
64 int srate, mclk, mclk_change;
65 int err;
66
67 srate = params_rate(params);
68 switch (srate) {
69 case 64000:
70 case 88200:
71 case 96000:
72 mclk = 128 * srate;
73 break;
74 default:
75 mclk = 256 * srate;
76 break;
77 }
78 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
79 while (mclk < 6000000)
80 mclk *= 2;
81
82 err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change);
83 if (err < 0) {
84 pr_err(PREFIX "Can't configure clocks\n");
85 return err;
86 }
87
88 err = snd_soc_dai_set_fmt(codec_dai,
89 SND_SOC_DAIFMT_I2S |
90 SND_SOC_DAIFMT_NB_NF |
91 SND_SOC_DAIFMT_CBS_CFS);
92 if (err < 0) {
93 pr_err(PREFIX "codec_dai fmt not set\n");
94 return err;
95 }
96
97 err = snd_soc_dai_set_fmt(cpu_dai,
98 SND_SOC_DAIFMT_I2S |
99 SND_SOC_DAIFMT_NB_NF |
100 SND_SOC_DAIFMT_CBS_CFS);
101 if (err < 0) {
102 pr_err(PREFIX "cpu_dai fmt not set\n");
103 return err;
104 }
105
106 if (mclk_change) {
Stephen Warrenbc72fe02011-01-28 14:26:38 -0700107 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
108 SND_SOC_CLOCK_IN);
109 if (err < 0) {
110 pr_err(PREFIX "codec_dai clock not set\n");
111 return err;
112 }
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700113 }
114
115 return 0;
116}
117
118static struct snd_soc_ops harmony_asoc_ops = {
119 .hw_params = harmony_asoc_hw_params,
120};
121
Stephen Warren6e267642011-01-28 14:26:37 -0700122static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
123 struct snd_kcontrol *k, int event)
124{
125 struct snd_soc_codec *codec = w->codec;
126 struct snd_soc_card *card = codec->card;
127 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
128 struct harmony_audio_platform_data *pdata = harmony->pdata;
129
130 gpio_set_value_cansleep(pdata->gpio_spkr_en,
131 !!SND_SOC_DAPM_EVENT_ON(event));
132
133 return 0;
134}
135
Stephen Warren62ffac42011-01-13 15:22:08 -0700136static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
Stephen Warren6e267642011-01-28 14:26:37 -0700137 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
Stephen Warren62ffac42011-01-13 15:22:08 -0700138 SND_SOC_DAPM_HP("Headphone Jack", NULL),
139 SND_SOC_DAPM_MIC("Mic Jack", NULL),
140};
141
142static const struct snd_soc_dapm_route harmony_audio_map[] = {
143 {"Headphone Jack", NULL, "HPOUTR"},
144 {"Headphone Jack", NULL, "HPOUTL"},
Stephen Warren6e267642011-01-28 14:26:37 -0700145 {"Int Spk", NULL, "ROP"},
146 {"Int Spk", NULL, "RON"},
147 {"Int Spk", NULL, "LOP"},
148 {"Int Spk", NULL, "LON"},
Stephen Warren62ffac42011-01-13 15:22:08 -0700149 {"Mic Bias", NULL, "Mic Jack"},
150 {"IN1L", NULL, "Mic Bias"},
151};
152
153static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
154{
155 struct snd_soc_codec *codec = rtd->codec;
156 struct snd_soc_dapm_context *dapm = &codec->dapm;
Stephen Warren6e267642011-01-28 14:26:37 -0700157 struct snd_soc_card *card = codec->card;
158 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
159 struct harmony_audio_platform_data *pdata = harmony->pdata;
160 int ret;
161
162 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
163 if (ret) {
164 dev_err(card->dev, "cannot get spkr_en gpio\n");
165 return ret;
166 }
167 harmony->gpio_spkr_en_requested = 1;
168
169 gpio_direction_output(pdata->gpio_spkr_en, 0);
Stephen Warren62ffac42011-01-13 15:22:08 -0700170
171 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
172 ARRAY_SIZE(harmony_dapm_widgets));
173
174 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
175 ARRAY_SIZE(harmony_audio_map));
176
177 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Stephen Warren6e267642011-01-28 14:26:37 -0700178 snd_soc_dapm_enable_pin(dapm, "Int Spk");
Stephen Warren62ffac42011-01-13 15:22:08 -0700179 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
180 snd_soc_dapm_sync(dapm);
181
182 return 0;
183}
184
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700185static struct snd_soc_dai_link harmony_wm8903_dai = {
186 .name = "WM8903",
187 .stream_name = "WM8903 PCM",
188 .codec_name = "wm8903-codec.0-001a",
189 .platform_name = "tegra-pcm-audio",
190 .cpu_dai_name = "tegra-i2s.0",
191 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700192 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700193 .ops = &harmony_asoc_ops,
194};
195
196static struct snd_soc_card snd_soc_harmony = {
197 .name = "tegra-harmony",
198 .dai_link = &harmony_wm8903_dai,
199 .num_links = 1,
200};
201
Stephen Warren72de2b12011-01-28 14:26:36 -0700202static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700203{
Stephen Warren72de2b12011-01-28 14:26:36 -0700204 struct snd_soc_card *card = &snd_soc_harmony;
205 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700206 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700207 int ret;
208
209 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700210 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700211 return -ENODEV;
212 }
213
Stephen Warren6e267642011-01-28 14:26:37 -0700214 pdata = pdev->dev.platform_data;
215 if (!pdata) {
216 dev_err(&pdev->dev, "no platform data supplied\n");
217 return -EINVAL;
218 }
219
Stephen Warren72de2b12011-01-28 14:26:36 -0700220 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
221 if (!harmony) {
222 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
223 return -ENOMEM;
224 }
225
Stephen Warren6e267642011-01-28 14:26:37 -0700226 harmony->pdata = pdata;
227
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700228 ret = tegra_asoc_utils_init();
Stephen Warren72de2b12011-01-28 14:26:36 -0700229 if (ret)
230 goto err_free_harmony;
231
232 card->dev = &pdev->dev;
233 platform_set_drvdata(pdev, card);
234 snd_soc_card_set_drvdata(card, harmony);
235
236 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700237 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700238 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700239 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700240 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700241 }
242
243 return 0;
244
Stephen Warren72de2b12011-01-28 14:26:36 -0700245err_clear_drvdata:
246 snd_soc_card_set_drvdata(card, NULL);
247 platform_set_drvdata(pdev, NULL);
248 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700249 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700250err_free_harmony:
251 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700252 return ret;
253}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700254
Stephen Warren72de2b12011-01-28 14:26:36 -0700255static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700256{
Stephen Warren72de2b12011-01-28 14:26:36 -0700257 struct snd_soc_card *card = platform_get_drvdata(pdev);
258 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700259 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700260
261 snd_soc_unregister_card(card);
262
263 snd_soc_card_set_drvdata(card, NULL);
264 platform_set_drvdata(pdev, NULL);
265 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700266
267 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700268
Stephen Warren6e267642011-01-28 14:26:37 -0700269 if (harmony->gpio_spkr_en_requested)
270 gpio_free(pdata->gpio_spkr_en);
271
Stephen Warren72de2b12011-01-28 14:26:36 -0700272 kfree(harmony);
273
274 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700275}
Stephen Warren72de2b12011-01-28 14:26:36 -0700276
277static struct platform_driver tegra_snd_harmony_driver = {
278 .driver = {
279 .name = DRV_NAME,
280 .owner = THIS_MODULE,
281 },
282 .probe = tegra_snd_harmony_probe,
283 .remove = __devexit_p(tegra_snd_harmony_remove),
284};
285
286static int __init snd_tegra_harmony_init(void)
287{
288 return platform_driver_register(&tegra_snd_harmony_driver);
289}
290module_init(snd_tegra_harmony_init);
291
292static void __exit snd_tegra_harmony_exit(void)
293{
294 platform_driver_unregister(&tegra_snd_harmony_driver);
295}
296module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700297
298MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
299MODULE_DESCRIPTION("Harmony machine ASoC driver");
300MODULE_LICENSE("GPL");