blob: 0f5a481509300c5ea361b880d72584967ced5aeb [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) {
107 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
108 if (err < 0) {
109 pr_err(PREFIX "codec_dai clock not set\n");
110 return err;
111 }
112 }
113
114 return 0;
115}
116
117static struct snd_soc_ops harmony_asoc_ops = {
118 .hw_params = harmony_asoc_hw_params,
119};
120
Stephen Warren6e267642011-01-28 14:26:37 -0700121static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
122 struct snd_kcontrol *k, int event)
123{
124 struct snd_soc_codec *codec = w->codec;
125 struct snd_soc_card *card = codec->card;
126 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
127 struct harmony_audio_platform_data *pdata = harmony->pdata;
128
129 gpio_set_value_cansleep(pdata->gpio_spkr_en,
130 !!SND_SOC_DAPM_EVENT_ON(event));
131
132 return 0;
133}
134
Stephen Warren62ffac42011-01-13 15:22:08 -0700135static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
Stephen Warren6e267642011-01-28 14:26:37 -0700136 SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
Stephen Warren62ffac42011-01-13 15:22:08 -0700137 SND_SOC_DAPM_HP("Headphone Jack", NULL),
138 SND_SOC_DAPM_MIC("Mic Jack", NULL),
139};
140
141static const struct snd_soc_dapm_route harmony_audio_map[] = {
142 {"Headphone Jack", NULL, "HPOUTR"},
143 {"Headphone Jack", NULL, "HPOUTL"},
Stephen Warren6e267642011-01-28 14:26:37 -0700144 {"Int Spk", NULL, "ROP"},
145 {"Int Spk", NULL, "RON"},
146 {"Int Spk", NULL, "LOP"},
147 {"Int Spk", NULL, "LON"},
Stephen Warren62ffac42011-01-13 15:22:08 -0700148 {"Mic Bias", NULL, "Mic Jack"},
149 {"IN1L", NULL, "Mic Bias"},
150};
151
152static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
153{
154 struct snd_soc_codec *codec = rtd->codec;
155 struct snd_soc_dapm_context *dapm = &codec->dapm;
Stephen Warren6e267642011-01-28 14:26:37 -0700156 struct snd_soc_card *card = codec->card;
157 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
158 struct harmony_audio_platform_data *pdata = harmony->pdata;
159 int ret;
160
161 ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
162 if (ret) {
163 dev_err(card->dev, "cannot get spkr_en gpio\n");
164 return ret;
165 }
166 harmony->gpio_spkr_en_requested = 1;
167
168 gpio_direction_output(pdata->gpio_spkr_en, 0);
Stephen Warren62ffac42011-01-13 15:22:08 -0700169
170 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
171 ARRAY_SIZE(harmony_dapm_widgets));
172
173 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
174 ARRAY_SIZE(harmony_audio_map));
175
176 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Stephen Warren6e267642011-01-28 14:26:37 -0700177 snd_soc_dapm_enable_pin(dapm, "Int Spk");
Stephen Warren62ffac42011-01-13 15:22:08 -0700178 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
179 snd_soc_dapm_sync(dapm);
180
181 return 0;
182}
183
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700184static struct snd_soc_dai_link harmony_wm8903_dai = {
185 .name = "WM8903",
186 .stream_name = "WM8903 PCM",
187 .codec_name = "wm8903-codec.0-001a",
188 .platform_name = "tegra-pcm-audio",
189 .cpu_dai_name = "tegra-i2s.0",
190 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700191 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700192 .ops = &harmony_asoc_ops,
193};
194
195static struct snd_soc_card snd_soc_harmony = {
196 .name = "tegra-harmony",
197 .dai_link = &harmony_wm8903_dai,
198 .num_links = 1,
199};
200
Stephen Warren72de2b12011-01-28 14:26:36 -0700201static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700202{
Stephen Warren72de2b12011-01-28 14:26:36 -0700203 struct snd_soc_card *card = &snd_soc_harmony;
204 struct tegra_harmony *harmony;
Stephen Warren6e267642011-01-28 14:26:37 -0700205 struct harmony_audio_platform_data *pdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700206 int ret;
207
208 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700209 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700210 return -ENODEV;
211 }
212
Stephen Warren6e267642011-01-28 14:26:37 -0700213 pdata = pdev->dev.platform_data;
214 if (!pdata) {
215 dev_err(&pdev->dev, "no platform data supplied\n");
216 return -EINVAL;
217 }
218
Stephen Warren72de2b12011-01-28 14:26:36 -0700219 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
220 if (!harmony) {
221 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
222 return -ENOMEM;
223 }
224
Stephen Warren6e267642011-01-28 14:26:37 -0700225 harmony->pdata = pdata;
226
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700227 ret = tegra_asoc_utils_init();
Stephen Warren72de2b12011-01-28 14:26:36 -0700228 if (ret)
229 goto err_free_harmony;
230
231 card->dev = &pdev->dev;
232 platform_set_drvdata(pdev, card);
233 snd_soc_card_set_drvdata(card, harmony);
234
235 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700236 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700237 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700238 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700239 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700240 }
241
242 return 0;
243
Stephen Warren72de2b12011-01-28 14:26:36 -0700244err_clear_drvdata:
245 snd_soc_card_set_drvdata(card, NULL);
246 platform_set_drvdata(pdev, NULL);
247 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700248 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700249err_free_harmony:
250 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700251 return ret;
252}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700253
Stephen Warren72de2b12011-01-28 14:26:36 -0700254static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700255{
Stephen Warren72de2b12011-01-28 14:26:36 -0700256 struct snd_soc_card *card = platform_get_drvdata(pdev);
257 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
Stephen Warren6e267642011-01-28 14:26:37 -0700258 struct harmony_audio_platform_data *pdata = harmony->pdata;
Stephen Warren72de2b12011-01-28 14:26:36 -0700259
260 snd_soc_unregister_card(card);
261
262 snd_soc_card_set_drvdata(card, NULL);
263 platform_set_drvdata(pdev, NULL);
264 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700265
266 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700267
Stephen Warren6e267642011-01-28 14:26:37 -0700268 if (harmony->gpio_spkr_en_requested)
269 gpio_free(pdata->gpio_spkr_en);
270
Stephen Warren72de2b12011-01-28 14:26:36 -0700271 kfree(harmony);
272
273 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700274}
Stephen Warren72de2b12011-01-28 14:26:36 -0700275
276static struct platform_driver tegra_snd_harmony_driver = {
277 .driver = {
278 .name = DRV_NAME,
279 .owner = THIS_MODULE,
280 },
281 .probe = tegra_snd_harmony_probe,
282 .remove = __devexit_p(tegra_snd_harmony_remove),
283};
284
285static int __init snd_tegra_harmony_init(void)
286{
287 return platform_driver_register(&tegra_snd_harmony_driver);
288}
289module_init(snd_tegra_harmony_init);
290
291static void __exit snd_tegra_harmony_exit(void)
292{
293 platform_driver_unregister(&tegra_snd_harmony_driver);
294}
295module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700296
297MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
298MODULE_DESCRIPTION("Harmony machine ASoC driver");
299MODULE_LICENSE("GPL");