ASoC: Tegra: Harmony: Don't use soc-audio platform device
[firefly-linux-kernel-4.4.55.git] / sound / soc / tegra / harmony.c
1 /*
2  * harmony.c - Harmony machine ASoC driver
3  *
4  * Author: Stephen Warren <swarren@nvidia.com>
5  * Copyright (C) 2010-2011 - NVIDIA, Inc.
6  *
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>
32
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/slab.h>
36
37 #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
47 #define DRV_NAME "tegra-snd-harmony"
48 #define PREFIX DRV_NAME ": "
49
50 struct tegra_harmony {
51 };
52
53 static 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
112 static struct snd_soc_ops harmony_asoc_ops = {
113         .hw_params = harmony_asoc_hw_params,
114 };
115
116 static 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
121 static 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
128 static 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
146 static 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",
153         .init = harmony_asoc_init,
154         .ops = &harmony_asoc_ops,
155 };
156
157 static struct snd_soc_card snd_soc_harmony = {
158         .name = "tegra-harmony",
159         .dai_link = &harmony_wm8903_dai,
160         .num_links = 1,
161 };
162
163 static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
164 {
165         struct snd_soc_card *card = &snd_soc_harmony;
166         struct tegra_harmony *harmony;
167         int ret;
168
169         if (!machine_is_harmony()) {
170                 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
171                 return -ENODEV;
172         }
173
174         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
180         ret = tegra_asoc_utils_init();
181         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);
189         if (ret) {
190                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
191                         ret);
192                 goto err_clear_drvdata;
193         }
194
195         return 0;
196
197 err_clear_drvdata:
198         snd_soc_card_set_drvdata(card, NULL);
199         platform_set_drvdata(pdev, NULL);
200         card->dev = NULL;
201         tegra_asoc_utils_fini();
202 err_free_harmony:
203         kfree(harmony);
204         return ret;
205 }
206
207 static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
208 {
209         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;
217
218         tegra_asoc_utils_fini();
219
220         kfree(harmony);
221
222         return 0;
223 }
224
225 static 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
234 static int __init snd_tegra_harmony_init(void)
235 {
236         return platform_driver_register(&tegra_snd_harmony_driver);
237 }
238 module_init(snd_tegra_harmony_init);
239
240 static void __exit snd_tegra_harmony_exit(void)
241 {
242         platform_driver_unregister(&tegra_snd_harmony_driver);
243 }
244 module_exit(snd_tegra_harmony_exit);
245
246 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
247 MODULE_DESCRIPTION("Harmony machine ASoC driver");
248 MODULE_LICENSE("GPL");