blob: 2d6a15c6bdfeac92220b11e63ecdd799482dcfcb [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>
36
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070037#include <sound/core.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/soc.h>
41
42#include "tegra_das.h"
43#include "tegra_i2s.h"
44#include "tegra_pcm.h"
45#include "tegra_asoc_utils.h"
46
Stephen Warren72de2b12011-01-28 14:26:36 -070047#define DRV_NAME "tegra-snd-harmony"
48#define PREFIX DRV_NAME ": "
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070049
Stephen Warren72de2b12011-01-28 14:26:36 -070050struct tegra_harmony {
51};
Stephen Warrena8bf1ba2011-01-07 22:36:16 -070052
53static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
54 struct snd_pcm_hw_params *params)
55{
56 struct snd_soc_pcm_runtime *rtd = substream->private_data;
57 struct snd_soc_dai *codec_dai = rtd->codec_dai;
58 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
59 int srate, mclk, mclk_change;
60 int err;
61
62 srate = params_rate(params);
63 switch (srate) {
64 case 64000:
65 case 88200:
66 case 96000:
67 mclk = 128 * srate;
68 break;
69 default:
70 mclk = 256 * srate;
71 break;
72 }
73 /* FIXME: Codec only requires >= 3MHz if OSR==0 */
74 while (mclk < 6000000)
75 mclk *= 2;
76
77 err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change);
78 if (err < 0) {
79 pr_err(PREFIX "Can't configure clocks\n");
80 return err;
81 }
82
83 err = snd_soc_dai_set_fmt(codec_dai,
84 SND_SOC_DAIFMT_I2S |
85 SND_SOC_DAIFMT_NB_NF |
86 SND_SOC_DAIFMT_CBS_CFS);
87 if (err < 0) {
88 pr_err(PREFIX "codec_dai fmt not set\n");
89 return err;
90 }
91
92 err = snd_soc_dai_set_fmt(cpu_dai,
93 SND_SOC_DAIFMT_I2S |
94 SND_SOC_DAIFMT_NB_NF |
95 SND_SOC_DAIFMT_CBS_CFS);
96 if (err < 0) {
97 pr_err(PREFIX "cpu_dai fmt not set\n");
98 return err;
99 }
100
101 if (mclk_change) {
102 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
103 if (err < 0) {
104 pr_err(PREFIX "codec_dai clock not set\n");
105 return err;
106 }
107 }
108
109 return 0;
110}
111
112static struct snd_soc_ops harmony_asoc_ops = {
113 .hw_params = harmony_asoc_hw_params,
114};
115
Stephen Warren62ffac42011-01-13 15:22:08 -0700116static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
117 SND_SOC_DAPM_HP("Headphone Jack", NULL),
118 SND_SOC_DAPM_MIC("Mic Jack", NULL),
119};
120
121static const struct snd_soc_dapm_route harmony_audio_map[] = {
122 {"Headphone Jack", NULL, "HPOUTR"},
123 {"Headphone Jack", NULL, "HPOUTL"},
124 {"Mic Bias", NULL, "Mic Jack"},
125 {"IN1L", NULL, "Mic Bias"},
126};
127
128static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
129{
130 struct snd_soc_codec *codec = rtd->codec;
131 struct snd_soc_dapm_context *dapm = &codec->dapm;
132
133 snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
134 ARRAY_SIZE(harmony_dapm_widgets));
135
136 snd_soc_dapm_add_routes(dapm, harmony_audio_map,
137 ARRAY_SIZE(harmony_audio_map));
138
139 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
140 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
141 snd_soc_dapm_sync(dapm);
142
143 return 0;
144}
145
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700146static struct snd_soc_dai_link harmony_wm8903_dai = {
147 .name = "WM8903",
148 .stream_name = "WM8903 PCM",
149 .codec_name = "wm8903-codec.0-001a",
150 .platform_name = "tegra-pcm-audio",
151 .cpu_dai_name = "tegra-i2s.0",
152 .codec_dai_name = "wm8903-hifi",
Stephen Warren62ffac42011-01-13 15:22:08 -0700153 .init = harmony_asoc_init,
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700154 .ops = &harmony_asoc_ops,
155};
156
157static struct snd_soc_card snd_soc_harmony = {
158 .name = "tegra-harmony",
159 .dai_link = &harmony_wm8903_dai,
160 .num_links = 1,
161};
162
Stephen Warren72de2b12011-01-28 14:26:36 -0700163static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700164{
Stephen Warren72de2b12011-01-28 14:26:36 -0700165 struct snd_soc_card *card = &snd_soc_harmony;
166 struct tegra_harmony *harmony;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700167 int ret;
168
169 if (!machine_is_harmony()) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700170 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700171 return -ENODEV;
172 }
173
Stephen Warren72de2b12011-01-28 14:26:36 -0700174 harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
175 if (!harmony) {
176 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
177 return -ENOMEM;
178 }
179
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700180 ret = tegra_asoc_utils_init();
Stephen Warren72de2b12011-01-28 14:26:36 -0700181 if (ret)
182 goto err_free_harmony;
183
184 card->dev = &pdev->dev;
185 platform_set_drvdata(pdev, card);
186 snd_soc_card_set_drvdata(card, harmony);
187
188 ret = snd_soc_register_card(card);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700189 if (ret) {
Stephen Warren72de2b12011-01-28 14:26:36 -0700190 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700191 ret);
Stephen Warren72de2b12011-01-28 14:26:36 -0700192 goto err_clear_drvdata;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700193 }
194
195 return 0;
196
Stephen Warren72de2b12011-01-28 14:26:36 -0700197err_clear_drvdata:
198 snd_soc_card_set_drvdata(card, NULL);
199 platform_set_drvdata(pdev, NULL);
200 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700201 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700202err_free_harmony:
203 kfree(harmony);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700204 return ret;
205}
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700206
Stephen Warren72de2b12011-01-28 14:26:36 -0700207static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700208{
Stephen Warren72de2b12011-01-28 14:26:36 -0700209 struct snd_soc_card *card = platform_get_drvdata(pdev);
210 struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
211
212 snd_soc_unregister_card(card);
213
214 snd_soc_card_set_drvdata(card, NULL);
215 platform_set_drvdata(pdev, NULL);
216 card->dev = NULL;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700217
218 tegra_asoc_utils_fini();
Stephen Warren72de2b12011-01-28 14:26:36 -0700219
220 kfree(harmony);
221
222 return 0;
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700223}
Stephen Warren72de2b12011-01-28 14:26:36 -0700224
225static struct platform_driver tegra_snd_harmony_driver = {
226 .driver = {
227 .name = DRV_NAME,
228 .owner = THIS_MODULE,
229 },
230 .probe = tegra_snd_harmony_probe,
231 .remove = __devexit_p(tegra_snd_harmony_remove),
232};
233
234static int __init snd_tegra_harmony_init(void)
235{
236 return platform_driver_register(&tegra_snd_harmony_driver);
237}
238module_init(snd_tegra_harmony_init);
239
240static void __exit snd_tegra_harmony_exit(void)
241{
242 platform_driver_unregister(&tegra_snd_harmony_driver);
243}
244module_exit(snd_tegra_harmony_exit);
Stephen Warrena8bf1ba2011-01-07 22:36:16 -0700245
246MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
247MODULE_DESCRIPTION("Harmony machine ASoC driver");
248MODULE_LICENSE("GPL");