b160b7113f456d155b86f7f757d07258cf2ed2cf
[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 - 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 #include <linux/module.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37
38 #include "tegra_das.h"
39 #include "tegra_i2s.h"
40 #include "tegra_pcm.h"
41 #include "tegra_asoc_utils.h"
42
43 #define PREFIX "ASoC Harmony: "
44
45 static struct platform_device *harmony_snd_device;
46
47 static int harmony_asoc_hw_params(struct snd_pcm_substream *substream,
48                                         struct snd_pcm_hw_params *params)
49 {
50         struct snd_soc_pcm_runtime *rtd = substream->private_data;
51         struct snd_soc_dai *codec_dai = rtd->codec_dai;
52         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
53         int srate, mclk, mclk_change;
54         int err;
55
56         srate = params_rate(params);
57         switch (srate) {
58         case 64000:
59         case 88200:
60         case 96000:
61                 mclk = 128 * srate;
62                 break;
63         default:
64                 mclk = 256 * srate;
65                 break;
66         }
67         /* FIXME: Codec only requires >= 3MHz if OSR==0 */
68         while (mclk < 6000000)
69                 mclk *= 2;
70
71         err = tegra_asoc_utils_set_rate(srate, mclk, &mclk_change);
72         if (err < 0) {
73                 pr_err(PREFIX "Can't configure clocks\n");
74                 return err;
75         }
76
77         err = snd_soc_dai_set_fmt(codec_dai,
78                                         SND_SOC_DAIFMT_I2S |
79                                         SND_SOC_DAIFMT_NB_NF |
80                                         SND_SOC_DAIFMT_CBS_CFS);
81         if (err < 0) {
82                 pr_err(PREFIX "codec_dai fmt not set\n");
83                 return err;
84         }
85
86         err = snd_soc_dai_set_fmt(cpu_dai,
87                                         SND_SOC_DAIFMT_I2S |
88                                         SND_SOC_DAIFMT_NB_NF |
89                                         SND_SOC_DAIFMT_CBS_CFS);
90         if (err < 0) {
91                 pr_err(PREFIX "cpu_dai fmt not set\n");
92                 return err;
93         }
94
95         if (mclk_change) {
96             err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk, SND_SOC_CLOCK_IN);
97             if (err < 0) {
98                     pr_err(PREFIX "codec_dai clock not set\n");
99                     return err;
100             }
101         }
102
103         return 0;
104 }
105
106 static struct snd_soc_ops harmony_asoc_ops = {
107         .hw_params = harmony_asoc_hw_params,
108 };
109
110 static const struct snd_soc_dapm_widget harmony_dapm_widgets[] = {
111         SND_SOC_DAPM_HP("Headphone Jack", NULL),
112         SND_SOC_DAPM_MIC("Mic Jack", NULL),
113 };
114
115 static const struct snd_soc_dapm_route harmony_audio_map[] = {
116         {"Headphone Jack", NULL, "HPOUTR"},
117         {"Headphone Jack", NULL, "HPOUTL"},
118         {"Mic Bias", NULL, "Mic Jack"},
119         {"IN1L", NULL, "Mic Bias"},
120 };
121
122 static int harmony_asoc_init(struct snd_soc_pcm_runtime *rtd)
123 {
124         struct snd_soc_codec *codec = rtd->codec;
125         struct snd_soc_dapm_context *dapm = &codec->dapm;
126
127         snd_soc_dapm_new_controls(dapm, harmony_dapm_widgets,
128                                         ARRAY_SIZE(harmony_dapm_widgets));
129
130         snd_soc_dapm_add_routes(dapm, harmony_audio_map,
131                                 ARRAY_SIZE(harmony_audio_map));
132
133         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
134         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
135         snd_soc_dapm_sync(dapm);
136
137         return 0;
138 }
139
140 static struct snd_soc_dai_link harmony_wm8903_dai = {
141         .name = "WM8903",
142         .stream_name = "WM8903 PCM",
143         .codec_name = "wm8903-codec.0-001a",
144         .platform_name = "tegra-pcm-audio",
145         .cpu_dai_name = "tegra-i2s.0",
146         .codec_dai_name = "wm8903-hifi",
147         .init = harmony_asoc_init,
148         .ops = &harmony_asoc_ops,
149 };
150
151 static struct snd_soc_card snd_soc_harmony = {
152         .name = "tegra-harmony",
153         .dai_link = &harmony_wm8903_dai,
154         .num_links = 1,
155 };
156
157 static int __init harmony_soc_modinit(void)
158 {
159         int ret;
160
161         if (!machine_is_harmony()) {
162                 pr_err(PREFIX "Not running on Tegra Harmony!\n");
163                 return -ENODEV;
164         }
165
166         ret = tegra_asoc_utils_init();
167         if (ret) {
168                 return ret;
169         }
170
171         /*
172          * Create and register platform device
173          */
174         harmony_snd_device = platform_device_alloc("soc-audio", -1);
175         if (harmony_snd_device == NULL) {
176                 pr_err(PREFIX "platform_device_alloc failed\n");
177                 ret = -ENOMEM;
178                 goto err_clock_utils;
179         }
180
181         platform_set_drvdata(harmony_snd_device, &snd_soc_harmony);
182
183         ret = platform_device_add(harmony_snd_device);
184         if (ret) {
185                 pr_err(PREFIX "platform_device_add failed (%d)\n",
186                         ret);
187                 goto err_device_put;
188         }
189
190         return 0;
191
192 err_device_put:
193         platform_device_put(harmony_snd_device);
194 err_clock_utils:
195         tegra_asoc_utils_fini();
196         return ret;
197 }
198 module_init(harmony_soc_modinit);
199
200 static void __exit harmony_soc_modexit(void)
201 {
202         platform_device_unregister(harmony_snd_device);
203
204         tegra_asoc_utils_fini();
205 }
206 module_exit(harmony_soc_modexit);
207
208 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
209 MODULE_DESCRIPTION("Harmony machine ASoC driver");
210 MODULE_LICENSE("GPL");