ASoC: Tegra: Harmony: Add switch control for 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/jack.h>
42 #include <sound/pcm.h>
43 #include <sound/pcm_params.h>
44 #include <sound/soc.h>
45
46 #include "tegra_das.h"
47 #include "tegra_i2s.h"
48 #include "tegra_pcm.h"
49 #include "tegra_asoc_utils.h"
50
51 #define DRV_NAME "tegra-snd-harmony"
52
53 struct tegra_harmony {
54         struct tegra_asoc_utils_data util_data;
55         struct harmony_audio_platform_data *pdata;
56         int gpio_spkr_en_requested;
57 };
58
59 static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
60                                         struct snd_pcm_hw_params *params)
61 {
62         struct snd_soc_pcm_runtime *rtd = substream->private_data;
63         struct snd_soc_dai *codec_dai = rtd->codec_dai;
64         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
65         struct snd_soc_codec *codec = rtd->codec;
66         struct snd_soc_card *card = codec->card;
67         struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
68         int srate, mclk, mclk_change;
69         int err;
70
71         srate = params_rate(params);
72         switch (srate) {
73         case 64000:
74         case 88200:
75         case 96000:
76                 mclk = 128 * srate;
77                 break;
78         default:
79                 mclk = 256 * srate;
80                 break;
81         }
82         /* FIXME: Codec only requires >= 3MHz if OSR==0 */
83         while (mclk < 6000000)
84                 mclk *= 2;
85
86         err = tegra_asoc_utils_set_rate(&harmony->util_data, srate, mclk,
87                                         &mclk_change);
88         if (err < 0) {
89                 dev_err(card->dev, "Can't configure clocks\n");
90                 return err;
91         }
92
93         err = snd_soc_dai_set_fmt(codec_dai,
94                                         SND_SOC_DAIFMT_I2S |
95                                         SND_SOC_DAIFMT_NB_NF |
96                                         SND_SOC_DAIFMT_CBS_CFS);
97         if (err < 0) {
98                 dev_err(card->dev, "codec_dai fmt not set\n");
99                 return err;
100         }
101
102         err = snd_soc_dai_set_fmt(cpu_dai,
103                                         SND_SOC_DAIFMT_I2S |
104                                         SND_SOC_DAIFMT_NB_NF |
105                                         SND_SOC_DAIFMT_CBS_CFS);
106         if (err < 0) {
107                 dev_err(card->dev, "cpu_dai fmt not set\n");
108                 return err;
109         }
110
111         if (mclk_change) {
112                 err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
113                                              SND_SOC_CLOCK_IN);
114                 if (err < 0) {
115                         dev_err(card->dev, "codec_dai clock not set\n");
116                         return err;
117                 }
118         }
119
120         return 0;
121 }
122
123 static struct snd_soc_ops harmony_asoc_ops = {
124         .hw_params = harmony_asoc_hw_params,
125 };
126
127 static struct snd_soc_jack harmony_hp_jack;
128
129 static struct snd_soc_jack_pin harmony_hp_jack_pins[] = {
130         {
131                 .pin = "Headphone Jack",
132                 .mask = SND_JACK_HEADPHONE,
133         },
134 };
135
136 static struct snd_soc_jack_gpio harmony_hp_jack_gpios[] = {
137         {
138                 .name = "headphone detect",
139                 .report = SND_JACK_HEADPHONE,
140                 .debounce_time = 150,
141                 .invert = 1,
142         }
143 };
144
145 static int harmony_event_int_spk(struct snd_soc_dapm_widget *w,
146                                         struct snd_kcontrol *k, int event)
147 {
148         struct snd_soc_codec *codec = w->codec;
149         struct snd_soc_card *card = codec->card;
150         struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
151         struct harmony_audio_platform_data *pdata = harmony->pdata;
152
153         gpio_set_value_cansleep(pdata->gpio_spkr_en,
154                                 SND_SOC_DAPM_EVENT_ON(event));
155
156         return 0;
157 }
158
159 static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
160         SND_SOC_DAPM_SPK("Int Spk", harmony_event_int_spk),
161         SND_SOC_DAPM_HP("Headphone Jack", NULL),
162         SND_SOC_DAPM_MIC("Mic Jack", NULL),
163 };
164
165 static const struct snd_soc_dapm_route harmony_audio_map[] = {
166         {"Headphone Jack", NULL, "HPOUTR"},
167         {"Headphone Jack", NULL, "HPOUTL"},
168         {"Int Spk", NULL, "ROP"},
169         {"Int Spk", NULL, "RON"},
170         {"Int Spk", NULL, "LOP"},
171         {"Int Spk", NULL, "LON"},
172         {"Mic Bias", NULL, "Mic Jack"},
173         {"IN1L", NULL, "Mic Bias"},
174 };
175
176 static const struct snd_kcontrol_new harmony_controls[] = {
177         SOC_DAPM_PIN_SWITCH("Int Spk"),
178 };
179
180 static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
181 {
182         struct snd_soc_codec *codec = rtd->codec;
183         struct snd_soc_dapm_context *dapm = &codec->dapm;
184         struct snd_soc_card *card = codec->card;
185         struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
186         struct harmony_audio_platform_data *pdata = harmony->pdata;
187         int ret;
188
189         ret = gpio_request(pdata->gpio_spkr_en, "spkr_en");
190         if (ret) {
191                 dev_err(card->dev, "cannot get spkr_en gpio\n");
192                 return ret;
193         }
194         harmony->gpio_spkr_en_requested = 1;
195
196         gpio_direction_output(pdata->gpio_spkr_en, 0);
197
198         ret = snd_soc_add_controls(codec, harmony_controls,
199                                    ARRAY_SIZE(harmony_controls));
200         if (ret < 0)
201                 return ret;
202
203         snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
204                                         ARRAY_SIZE(harmony_dapm_widgets));
205
206         snd_soc_dapm_add_routes(dapm, harmony_audio_map,
207                                 ARRAY_SIZE(harmony_audio_map));
208
209         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
210         snd_soc_dapm_sync(dapm);
211
212         harmony_hp_jack_gpios[0].gpio = pdata->gpio_hp_det;
213         snd_soc_jack_new(codec, "Headphone Jack", SND_JACK_HEADPHONE,
214                          &harmony_hp_jack);
215         snd_soc_jack_add_pins(&harmony_hp_jack,
216                               ARRAY_SIZE(harmony_hp_jack_pins),
217                               harmony_hp_jack_pins);
218         snd_soc_jack_add_gpios(&harmony_hp_jack,
219                                ARRAY_SIZE(harmony_hp_jack_gpios),
220                                harmony_hp_jack_gpios);
221
222         return 0;
223 }
224
225 static struct snd_soc_dai_link harmony_wm8903_dai = {
226         .name = "WM8903",
227         .stream_name = "WM8903 PCM",
228         .codec_name = "wm8903-codec.0-001a",
229         .platform_name = "tegra-pcm-audio",
230         .cpu_dai_name = "tegra-i2s.0",
231         .codec_dai_name = "wm8903-hifi",
232         .init = harmony_asoc_init,
233         .ops = &harmony_asoc_ops,
234 };
235
236 static struct snd_soc_card snd_soc_harmony = {
237         .name = "tegra-harmony",
238         .dai_link = &harmony_wm8903_dai,
239         .num_links = 1,
240 };
241
242 static __devinit int tegra_snd_harmony_probe(struct platform_device *pdev)
243 {
244         struct snd_soc_card *card = &snd_soc_harmony;
245         struct tegra_harmony *harmony;
246         struct harmony_audio_platform_data *pdata;
247         int ret;
248
249         if (!machine_is_harmony()) {
250                 dev_err(&pdev->dev, "Not running on Tegra Harmony!\n");
251                 return -ENODEV;
252         }
253
254         pdata = pdev->dev.platform_data;
255         if (!pdata) {
256                 dev_err(&pdev->dev, "no platform data supplied\n");
257                 return -EINVAL;
258         }
259
260         harmony = kzalloc(sizeof(struct tegra_harmony), GFP_KERNEL);
261         if (!harmony) {
262                 dev_err(&pdev->dev, "Can't allocate tegra_harmony\n");
263                 return -ENOMEM;
264         }
265
266         harmony->pdata = pdata;
267
268         ret = tegra_asoc_utils_init(&harmony->util_data, &pdev->dev);
269         if (ret)
270                 goto err_free_harmony;
271
272         card->dev = &pdev->dev;
273         platform_set_drvdata(pdev, card);
274         snd_soc_card_set_drvdata(card, harmony);
275
276         ret = snd_soc_register_card(card);
277         if (ret) {
278                 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n",
279                         ret);
280                 goto err_clear_drvdata;
281         }
282
283         return 0;
284
285 err_clear_drvdata:
286         snd_soc_card_set_drvdata(card, NULL);
287         platform_set_drvdata(pdev, NULL);
288         card->dev = NULL;
289         tegra_asoc_utils_fini(&harmony->util_data);
290 err_free_harmony:
291         kfree(harmony);
292         return ret;
293 }
294
295 static int __devexit tegra_snd_harmony_remove(struct platform_device *pdev)
296 {
297         struct snd_soc_card *card = platform_get_drvdata(pdev);
298         struct tegra_harmony *harmony = snd_soc_card_get_drvdata(card);
299         struct harmony_audio_platform_data *pdata = harmony->pdata;
300
301         snd_soc_unregister_card(card);
302
303         snd_soc_card_set_drvdata(card, NULL);
304         platform_set_drvdata(pdev, NULL);
305         card->dev = NULL;
306
307         tegra_asoc_utils_fini(&harmony->util_data);
308
309         if (harmony->gpio_spkr_en_requested)
310                 gpio_free(pdata->gpio_spkr_en);
311
312         kfree(harmony);
313
314         return 0;
315 }
316
317 static struct platform_driver tegra_snd_harmony_driver = {
318         .driver = {
319                 .name = DRV_NAME,
320                 .owner = THIS_MODULE,
321         },
322         .probe = tegra_snd_harmony_probe,
323         .remove = __devexit_p(tegra_snd_harmony_remove),
324 };
325
326 static int __init snd_tegra_harmony_init(void)
327 {
328         return platform_driver_register(&tegra_snd_harmony_driver);
329 }
330 module_init(snd_tegra_harmony_init);
331
332 static void __exit snd_tegra_harmony_exit(void)
333 {
334         platform_driver_unregister(&tegra_snd_harmony_driver);
335 }
336 module_exit(snd_tegra_harmony_exit);
337
338 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
339 MODULE_DESCRIPTION("Harmony machine ASoC driver");
340 MODULE_LICENSE("GPL");