ASoC: Tegra: Harmony: Support the internal speaker
[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 #include <linux/gpio.h>
37
38 #include <mach/harmony_audio.h>
39
40 #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
50 #define DRV_NAME "tegra-snd-harmony"
51 #define PREFIX DRV_NAME ": "
52
53 struct tegra_harmony {
54         struct harmony_audio_platform_data *pdata;
55         int gpio_spkr_en_requested;
56 };
57
58 static 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
117 static struct snd_soc_ops harmony_asoc_ops = {
118         .hw_params = harmony_asoc_hw_params,
119 };
120
121 static 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
135 static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
136         SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
137         SND_SOC_DAPM_HP("Headphone Jack", NULL),
138         SND_SOC_DAPM_MIC("Mic Jack", NULL),
139 };
140
141 static const struct snd_soc_dapm_route harmony_audio_map[] = {
142         {"Headphone Jack", NULL, "HPOUTR"},
143         {"Headphone Jack", NULL, "HPOUTL"},
144         {"Int Spk", NULL, "ROP"},
145         {"Int Spk", NULL, "RON"},
146         {"Int Spk", NULL, "LOP"},
147         {"Int Spk", NULL, "LON"},
148         {"Mic Bias", NULL, "Mic Jack"},
149         {"IN1L", NULL, "Mic Bias"},
150 };
151
152 static 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;
156         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);
169
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");
177         snd_soc_dapm_enable_pin(dapm, "Int Spk");
178         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
179         snd_soc_dapm_sync(dapm);
180
181         return 0;
182 }
183
184 static 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",
191         .init = harmony_asoc_init,
192         .ops = &harmony_asoc_ops,
193 };
194
195 static struct snd_soc_card snd_soc_harmony = {
196         .name = "tegra-harmony",
197         .dai_link = &harmony_wm8903_dai,
198         .num_links = 1,
199 };
200
201 static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
202 {
203         struct snd_soc_card *card = &snd_soc_harmony;
204         struct tegra_harmony *harmony;
205         struct harmony_audio_platform_data *pdata;
206         int ret;
207
208         if (!machine_is_harmony()) {
209                 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
210                 return -ENODEV;
211         }
212
213         pdata = pdev->dev.platform_data;
214         if (!pdata) {
215                 dev_err(&pdev->dev, "no platform data supplied\n");
216                 return -EINVAL;
217         }
218
219         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
225         harmony->pdata = pdata;
226
227         ret = tegra_asoc_utils_init();
228         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);
236         if (ret) {
237                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
238                         ret);
239                 goto err_clear_drvdata;
240         }
241
242         return 0;
243
244 err_clear_drvdata:
245         snd_soc_card_set_drvdata(card, NULL);
246         platform_set_drvdata(pdev, NULL);
247         card->dev = NULL;
248         tegra_asoc_utils_fini();
249 err_free_harmony:
250         kfree(harmony);
251         return ret;
252 }
253
254 static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
255 {
256         struct snd_soc_card *card = platform_get_drvdata(pdev);
257         struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
258         struct harmony_audio_platform_data *pdata = harmony->pdata;
259
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;
265
266         tegra_asoc_utils_fini();
267
268         if (harmony->gpio_spkr_en_requested)
269                 gpio_free(pdata->gpio_spkr_en);
270
271         kfree(harmony);
272
273         return 0;
274 }
275
276 static 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
285 static int __init snd_tegra_harmony_init(void)
286 {
287         return platform_driver_register(&tegra_snd_harmony_driver);
288 }
289 module_init(snd_tegra_harmony_init);
290
291 static void __exit snd_tegra_harmony_exit(void)
292 {
293         platform_driver_unregister(&tegra_snd_harmony_driver);
294 }
295 module_exit(snd_tegra_harmony_exit);
296
297 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
298 MODULE_DESCRIPTION("Harmony machine ASoC driver");
299 MODULE_LICENSE("GPL");