ASoC: Add trivial pm_runtime usage to Samsung DAI drivers
[deliverable/linux.git] / sound / soc / omap / n810.c
CommitLineData
2e74796a
JN
1/*
2 * n810.c -- SoC audio for Nokia N810
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
7ec41ee5 6 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
2e74796a
JN
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/clk.h>
aa6b904e 25#include <linux/i2c.h>
2e74796a
JN
26#include <linux/platform_device.h>
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
2e74796a
JN
30
31#include <asm/mach-types.h>
a09e64fb 32#include <mach/hardware.h>
f99a633a 33#include <linux/gpio.h>
da155d5b 34#include <linux/module.h>
ce491cf8 35#include <plat/mcbsp.h>
2e74796a
JN
36
37#include "omap-mcbsp.h"
38#include "omap-pcm.h"
2e74796a 39
f99a633a
JN
40#define N810_HEADSET_AMP_GPIO 10
41#define N810_SPEAKER_AMP_GPIO 101
2e74796a 42
f8d5fc92
JN
43enum {
44 N810_JACK_DISABLED,
45 N810_JACK_HP,
46 N810_JACK_HS,
47 N810_JACK_MIC,
48};
49
2e74796a
JN
50static struct clk *sys_clkout2;
51static struct clk *sys_clkout2_src;
52static struct clk *func96m_clk;
53
54static int n810_spk_func;
55static int n810_jack_func;
90b9e476 56static int n810_dmic_func;
2e74796a
JN
57
58static void n810_ext_control(struct snd_soc_codec *codec)
59{
ce6120cc 60 struct snd_soc_dapm_context *dapm = &codec->dapm;
f8d5fc92
JN
61 int hp = 0, line1l = 0;
62
63 switch (n810_jack_func) {
64 case N810_JACK_HS:
65 line1l = 1;
66 case N810_JACK_HP:
67 hp = 1;
68 break;
69 case N810_JACK_MIC:
70 line1l = 1;
71 break;
72 }
73
a5302181 74 if (n810_spk_func)
ce6120cc 75 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
a5302181 76 else
ce6120cc 77 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
a5302181 78
f8d5fc92 79 if (hp)
ce6120cc 80 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
a5302181 81 else
ce6120cc 82 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
f8d5fc92 83 if (line1l)
ce6120cc 84 snd_soc_dapm_enable_pin(dapm, "LINE1L");
f8d5fc92 85 else
ce6120cc 86 snd_soc_dapm_disable_pin(dapm, "LINE1L");
a5302181
LG
87
88 if (n810_dmic_func)
ce6120cc 89 snd_soc_dapm_enable_pin(dapm, "DMic");
a5302181 90 else
ce6120cc 91 snd_soc_dapm_disable_pin(dapm, "DMic");
2e74796a 92
ce6120cc 93 snd_soc_dapm_sync(dapm);
2e74796a
JN
94}
95
96static int n810_startup(struct snd_pcm_substream *substream)
97{
0be43050 98 struct snd_pcm_runtime *runtime = substream->runtime;
2e74796a 99 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 100 struct snd_soc_codec *codec = rtd->codec;
2e74796a 101
0be43050
JN
102 snd_pcm_hw_constraint_minmax(runtime,
103 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
104
2e74796a
JN
105 n810_ext_control(codec);
106 return clk_enable(sys_clkout2);
107}
108
109static void n810_shutdown(struct snd_pcm_substream *substream)
110{
111 clk_disable(sys_clkout2);
112}
113
114static int n810_hw_params(struct snd_pcm_substream *substream,
115 struct snd_pcm_hw_params *params)
116{
117 struct snd_soc_pcm_runtime *rtd = substream->private_data;
f0fba2ad 118 struct snd_soc_dai *codec_dai = rtd->codec_dai;
2e74796a
JN
119 int err;
120
2e74796a 121 /* Set the codec system clock for DAC and ADC */
64105cfd 122 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
2e74796a
JN
123 SND_SOC_CLOCK_IN);
124
125 return err;
126}
127
128static struct snd_soc_ops n810_ops = {
129 .startup = n810_startup,
130 .hw_params = n810_hw_params,
131 .shutdown = n810_shutdown,
132};
133
134static int n810_get_spk(struct snd_kcontrol *kcontrol,
135 struct snd_ctl_elem_value *ucontrol)
136{
137 ucontrol->value.integer.value[0] = n810_spk_func;
138
139 return 0;
140}
141
142static int n810_set_spk(struct snd_kcontrol *kcontrol,
143 struct snd_ctl_elem_value *ucontrol)
144{
145 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
146
147 if (n810_spk_func == ucontrol->value.integer.value[0])
148 return 0;
149
150 n810_spk_func = ucontrol->value.integer.value[0];
151 n810_ext_control(codec);
152
153 return 1;
154}
155
156static int n810_get_jack(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
158{
159 ucontrol->value.integer.value[0] = n810_jack_func;
160
161 return 0;
162}
163
164static int n810_set_jack(struct snd_kcontrol *kcontrol,
165 struct snd_ctl_elem_value *ucontrol)
166{
167 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
168
169 if (n810_jack_func == ucontrol->value.integer.value[0])
170 return 0;
171
172 n810_jack_func = ucontrol->value.integer.value[0];
173 n810_ext_control(codec);
174
175 return 1;
176}
177
90b9e476
JN
178static int n810_get_input(struct snd_kcontrol *kcontrol,
179 struct snd_ctl_elem_value *ucontrol)
180{
181 ucontrol->value.integer.value[0] = n810_dmic_func;
182
183 return 0;
184}
185
186static int n810_set_input(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
190
191 if (n810_dmic_func == ucontrol->value.integer.value[0])
192 return 0;
193
194 n810_dmic_func = ucontrol->value.integer.value[0];
195 n810_ext_control(codec);
196
197 return 1;
198}
199
2e74796a
JN
200static int n810_spk_event(struct snd_soc_dapm_widget *w,
201 struct snd_kcontrol *k, int event)
202{
203 if (SND_SOC_DAPM_EVENT_ON(event))
f99a633a 204 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
2e74796a 205 else
f99a633a 206 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
2e74796a
JN
207
208 return 0;
209}
210
211static int n810_jack_event(struct snd_soc_dapm_widget *w,
212 struct snd_kcontrol *k, int event)
213{
214 if (SND_SOC_DAPM_EVENT_ON(event))
f99a633a 215 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
2e74796a 216 else
f99a633a 217 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
2e74796a
JN
218
219 return 0;
220}
221
222static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
223 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
224 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
90b9e476 225 SND_SOC_DAPM_MIC("DMic", NULL),
2e74796a
JN
226};
227
1a250598 228static const struct snd_soc_dapm_route audio_map[] = {
2e74796a
JN
229 {"Headphone Jack", NULL, "HPLOUT"},
230 {"Headphone Jack", NULL, "HPROUT"},
231
232 {"Ext Spk", NULL, "LLOUT"},
233 {"Ext Spk", NULL, "RLOUT"},
90b9e476
JN
234
235 {"DMic Rate 64", NULL, "Mic Bias 2V"},
236 {"Mic Bias 2V", NULL, "DMic"},
2e74796a
JN
237};
238
239static const char *spk_function[] = {"Off", "On"};
f8d5fc92 240static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
90b9e476 241static const char *input_function[] = {"ADC", "Digital Mic"};
2e74796a 242static const struct soc_enum n810_enum[] = {
3c172791
JN
243 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
244 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
90b9e476 245 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
2e74796a
JN
246};
247
248static const struct snd_kcontrol_new aic33_n810_controls[] = {
249 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
250 n810_get_spk, n810_set_spk),
251 SOC_ENUM_EXT("Jack Function", n810_enum[1],
252 n810_get_jack, n810_set_jack),
90b9e476
JN
253 SOC_ENUM_EXT("Input Select", n810_enum[2],
254 n810_get_input, n810_set_input),
2e74796a
JN
255};
256
f0fba2ad 257static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
2e74796a 258{
f0fba2ad 259 struct snd_soc_codec *codec = rtd->codec;
ce6120cc 260 struct snd_soc_dapm_context *dapm = &codec->dapm;
2e74796a
JN
261
262 /* Not connected */
ce6120cc
LG
263 snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
264 snd_soc_dapm_nc_pin(dapm, "HPLCOM");
265 snd_soc_dapm_nc_pin(dapm, "HPRCOM");
266 snd_soc_dapm_nc_pin(dapm, "MIC3L");
267 snd_soc_dapm_nc_pin(dapm, "MIC3R");
268 snd_soc_dapm_nc_pin(dapm, "LINE1R");
269 snd_soc_dapm_nc_pin(dapm, "LINE2L");
270 snd_soc_dapm_nc_pin(dapm, "LINE2R");
2e74796a 271
8966c2dd 272 return 0;
2e74796a
JN
273}
274
275/* Digital audio interface glue - connects codec <--> CPU */
276static struct snd_soc_dai_link n810_dai = {
277 .name = "TLV320AIC33",
278 .stream_name = "AIC33",
f0fba2ad
LG
279 .cpu_dai_name = "omap-mcbsp-dai.1",
280 .platform_name = "omap-pcm-audio",
281 .codec_name = "tlv320aic3x-codec.2-0018",
282 .codec_dai_name = "tlv320aic3x-hifi",
cf9feff2
JN
283 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
284 SND_SOC_DAIFMT_CBM_CFM,
2e74796a
JN
285 .init = n810_aic33_init,
286 .ops = &n810_ops,
287};
288
289/* Audio machine driver */
87506549 290static struct snd_soc_card snd_soc_n810 = {
2e74796a
JN
291 .name = "N810",
292 .dai_link = &n810_dai,
293 .num_links = 1,
8966c2dd
PU
294
295 .controls = aic33_n810_controls,
296 .num_controls = ARRAY_SIZE(aic33_n810_controls),
297 .dapm_widgets = aic33_dapm_widgets,
298 .num_dapm_widgets = ARRAY_SIZE(aic33_dapm_widgets),
299 .dapm_routes = audio_map,
300 .num_dapm_routes = ARRAY_SIZE(audio_map),
2e74796a
JN
301};
302
2e74796a
JN
303static struct platform_device *n810_snd_device;
304
305static int __init n810_soc_init(void)
306{
307 int err;
308 struct device *dev;
309
6e132fa6 310 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
2e74796a
JN
311 return -ENODEV;
312
313 n810_snd_device = platform_device_alloc("soc-audio", -1);
314 if (!n810_snd_device)
315 return -ENOMEM;
316
f0fba2ad 317 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
2e74796a
JN
318 err = platform_device_add(n810_snd_device);
319 if (err)
320 goto err1;
321
322 dev = &n810_snd_device->dev;
323
324 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
325 if (IS_ERR(sys_clkout2_src)) {
326 dev_err(dev, "Could not get sys_clkout2_src clock\n");
e784539f
JN
327 err = PTR_ERR(sys_clkout2_src);
328 goto err2;
2e74796a
JN
329 }
330 sys_clkout2 = clk_get(dev, "sys_clkout2");
331 if (IS_ERR(sys_clkout2)) {
332 dev_err(dev, "Could not get sys_clkout2\n");
e784539f
JN
333 err = PTR_ERR(sys_clkout2);
334 goto err3;
2e74796a
JN
335 }
336 /*
337 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
338 * 96 MHz as its parent in order to get 12 MHz
339 */
340 func96m_clk = clk_get(dev, "func_96m_ck");
341 if (IS_ERR(func96m_clk)) {
342 dev_err(dev, "Could not get func 96M clock\n");
e784539f
JN
343 err = PTR_ERR(func96m_clk);
344 goto err4;
2e74796a
JN
345 }
346 clk_set_parent(sys_clkout2_src, func96m_clk);
347 clk_set_rate(sys_clkout2, 12000000);
348
f4c1724f
AB
349 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
350 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
351
f99a633a
JN
352 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
353 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
2e74796a
JN
354
355 return 0;
e784539f 356err4:
2e74796a 357 clk_put(sys_clkout2);
e784539f
JN
358err3:
359 clk_put(sys_clkout2_src);
360err2:
2e74796a
JN
361 platform_device_del(n810_snd_device);
362err1:
363 platform_device_put(n810_snd_device);
364
365 return err;
2e74796a
JN
366}
367
368static void __exit n810_soc_exit(void)
369{
f99a633a
JN
370 gpio_free(N810_SPEAKER_AMP_GPIO);
371 gpio_free(N810_HEADSET_AMP_GPIO);
e784539f
JN
372 clk_put(sys_clkout2_src);
373 clk_put(sys_clkout2);
374 clk_put(func96m_clk);
f99a633a 375
2e74796a
JN
376 platform_device_unregister(n810_snd_device);
377}
378
379module_init(n810_soc_init);
380module_exit(n810_soc_exit);
381
7ec41ee5 382MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
2e74796a
JN
383MODULE_DESCRIPTION("ALSA SoC Nokia N810");
384MODULE_LICENSE("GPL");
This page took 0.2279 seconds and 5 git commands to generate.