blob: 296cd9b7eecbec73c86ea85656dcbc4d107aa0a2 [file] [log] [blame]
Jarkko Nikula2e747962008-04-25 13:55:19 +02001/*
2 * n810.c -- SoC audio for Nokia N810
3 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Jarkko Nikulab08f7a62009-04-17 14:42:26 +03006 * Contact: Jarkko Nikula <jhnikula@gmail.com>
Jarkko Nikula2e747962008-04-25 13:55:19 +02007 *
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>
Ben Dooksaa6b9042009-08-20 22:50:42 +010025#include <linux/i2c.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020026#include <linux/platform_device.h>
27#include <sound/core.h>
28#include <sound/pcm.h>
29#include <sound/soc.h>
30#include <sound/soc-dapm.h>
31
32#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010033#include <mach/hardware.h>
Jarkko Nikulaf99a6332008-05-15 11:01:36 +020034#include <linux/gpio.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/mcbsp.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020036
37#include "omap-mcbsp.h"
38#include "omap-pcm.h"
39#include "../codecs/tlv320aic3x.h"
40
Jarkko Nikulaf99a6332008-05-15 11:01:36 +020041#define N810_HEADSET_AMP_GPIO 10
42#define N810_SPEAKER_AMP_GPIO 101
Jarkko Nikula2e747962008-04-25 13:55:19 +020043
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020044enum {
45 N810_JACK_DISABLED,
46 N810_JACK_HP,
47 N810_JACK_HS,
48 N810_JACK_MIC,
49};
50
Jarkko Nikula2e747962008-04-25 13:55:19 +020051static struct clk *sys_clkout2;
52static struct clk *sys_clkout2_src;
53static struct clk *func96m_clk;
54
55static int n810_spk_func;
56static int n810_jack_func;
Jarkko Nikula90b9e472008-06-25 14:58:47 +030057static int n810_dmic_func;
Jarkko Nikula2e747962008-04-25 13:55:19 +020058
59static void n810_ext_control(struct snd_soc_codec *codec)
60{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020061 struct snd_soc_dapm_context *dapm = &codec->dapm;
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020062 int hp = 0, line1l = 0;
63
64 switch (n810_jack_func) {
65 case N810_JACK_HS:
66 line1l = 1;
67 case N810_JACK_HP:
68 hp = 1;
69 break;
70 case N810_JACK_MIC:
71 line1l = 1;
72 break;
73 }
74
Liam Girdwooda5302182008-07-07 13:35:17 +010075 if (n810_spk_func)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020076 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
Liam Girdwooda5302182008-07-07 13:35:17 +010077 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020078 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
Jarkko Nikula2e747962008-04-25 13:55:19 +020079
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020080 if (hp)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020081 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Liam Girdwooda5302182008-07-07 13:35:17 +010082 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020083 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020084 if (line1l)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020085 snd_soc_dapm_enable_pin(dapm, "LINE1L");
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020086 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020087 snd_soc_dapm_disable_pin(dapm, "LINE1L");
Liam Girdwooda5302182008-07-07 13:35:17 +010088
89 if (n810_dmic_func)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020090 snd_soc_dapm_enable_pin(dapm, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010091 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020092 snd_soc_dapm_disable_pin(dapm, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010093
Liam Girdwoodce6120c2010-11-05 15:53:46 +020094 snd_soc_dapm_sync(dapm);
Jarkko Nikula2e747962008-04-25 13:55:19 +020095}
96
97static int n810_startup(struct snd_pcm_substream *substream)
98{
Jarkko Nikula0be43052008-11-25 12:45:08 +020099 struct snd_pcm_runtime *runtime = substream->runtime;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000101 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200102
Jarkko Nikula0be43052008-11-25 12:45:08 +0200103 snd_pcm_hw_constraint_minmax(runtime,
104 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
105
Jarkko Nikula2e747962008-04-25 13:55:19 +0200106 n810_ext_control(codec);
107 return clk_enable(sys_clkout2);
108}
109
110static void n810_shutdown(struct snd_pcm_substream *substream)
111{
112 clk_disable(sys_clkout2);
113}
114
115static int n810_hw_params(struct snd_pcm_substream *substream,
116 struct snd_pcm_hw_params *params)
117{
118 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000119 struct snd_soc_dai *codec_dai = rtd->codec_dai;
120 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200121 int err;
122
123 /* Set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100124 err = snd_soc_dai_set_fmt(codec_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200125 SND_SOC_DAIFMT_I2S |
126 SND_SOC_DAIFMT_NB_NF |
127 SND_SOC_DAIFMT_CBM_CFM);
128 if (err < 0)
129 return err;
130
131 /* Set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100132 err = snd_soc_dai_set_fmt(cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200133 SND_SOC_DAIFMT_I2S |
134 SND_SOC_DAIFMT_NB_NF |
135 SND_SOC_DAIFMT_CBM_CFM);
136 if (err < 0)
137 return err;
138
139 /* Set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100140 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200141 SND_SOC_CLOCK_IN);
142
143 return err;
144}
145
146static struct snd_soc_ops n810_ops = {
147 .startup = n810_startup,
148 .hw_params = n810_hw_params,
149 .shutdown = n810_shutdown,
150};
151
152static int n810_get_spk(struct snd_kcontrol *kcontrol,
153 struct snd_ctl_elem_value *ucontrol)
154{
155 ucontrol->value.integer.value[0] = n810_spk_func;
156
157 return 0;
158}
159
160static int n810_set_spk(struct snd_kcontrol *kcontrol,
161 struct snd_ctl_elem_value *ucontrol)
162{
163 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
164
165 if (n810_spk_func == ucontrol->value.integer.value[0])
166 return 0;
167
168 n810_spk_func = ucontrol->value.integer.value[0];
169 n810_ext_control(codec);
170
171 return 1;
172}
173
174static int n810_get_jack(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176{
177 ucontrol->value.integer.value[0] = n810_jack_func;
178
179 return 0;
180}
181
182static int n810_set_jack(struct snd_kcontrol *kcontrol,
183 struct snd_ctl_elem_value *ucontrol)
184{
185 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
186
187 if (n810_jack_func == ucontrol->value.integer.value[0])
188 return 0;
189
190 n810_jack_func = ucontrol->value.integer.value[0];
191 n810_ext_control(codec);
192
193 return 1;
194}
195
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300196static int n810_get_input(struct snd_kcontrol *kcontrol,
197 struct snd_ctl_elem_value *ucontrol)
198{
199 ucontrol->value.integer.value[0] = n810_dmic_func;
200
201 return 0;
202}
203
204static int n810_set_input(struct snd_kcontrol *kcontrol,
205 struct snd_ctl_elem_value *ucontrol)
206{
207 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
208
209 if (n810_dmic_func == ucontrol->value.integer.value[0])
210 return 0;
211
212 n810_dmic_func = ucontrol->value.integer.value[0];
213 n810_ext_control(codec);
214
215 return 1;
216}
217
Jarkko Nikula2e747962008-04-25 13:55:19 +0200218static int n810_spk_event(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *k, int event)
220{
221 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200222 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200223 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200224 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200225
226 return 0;
227}
228
229static int n810_jack_event(struct snd_soc_dapm_widget *w,
230 struct snd_kcontrol *k, int event)
231{
232 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200233 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200234 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200235 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200236
237 return 0;
238}
239
240static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
241 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
242 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300243 SND_SOC_DAPM_MIC("DMic", NULL),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200244};
245
Mark Brown1a250592008-05-13 14:58:57 +0200246static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200247 {"Headphone Jack", NULL, "HPLOUT"},
248 {"Headphone Jack", NULL, "HPROUT"},
249
250 {"Ext Spk", NULL, "LLOUT"},
251 {"Ext Spk", NULL, "RLOUT"},
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300252
253 {"DMic Rate 64", NULL, "Mic Bias 2V"},
254 {"Mic Bias 2V", NULL, "DMic"},
Jarkko Nikula2e747962008-04-25 13:55:19 +0200255};
256
257static const char *spk_function[] = {"Off", "On"};
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +0200258static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300259static const char *input_function[] = {"ADC", "Digital Mic"};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200260static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300263 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200264};
265
266static const struct snd_kcontrol_new aic33_n810_controls[] = {
267 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
268 n810_get_spk, n810_set_spk),
269 SOC_ENUM_EXT("Jack Function", n810_enum[1],
270 n810_get_jack, n810_set_jack),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300271 SOC_ENUM_EXT("Input Select", n810_enum[2],
272 n810_get_input, n810_set_input),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200273};
274
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000275static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200276{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000277 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200278 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100279 int err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200280
281 /* Not connected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200282 snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
283 snd_soc_dapm_nc_pin(dapm, "HPLCOM");
284 snd_soc_dapm_nc_pin(dapm, "HPRCOM");
285 snd_soc_dapm_nc_pin(dapm, "MIC3L");
286 snd_soc_dapm_nc_pin(dapm, "MIC3R");
287 snd_soc_dapm_nc_pin(dapm, "LINE1R");
288 snd_soc_dapm_nc_pin(dapm, "LINE2L");
289 snd_soc_dapm_nc_pin(dapm, "LINE2R");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200290
291 /* Add N810 specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100292 err = snd_soc_add_controls(codec, aic33_n810_controls,
293 ARRAY_SIZE(aic33_n810_controls));
294 if (err < 0)
295 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296
297 /* Add N810 specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200298 snd_soc_dapm_new_controls(dapm, aic33_dapm_widgets,
Mark Brown1a250592008-05-13 14:58:57 +0200299 ARRAY_SIZE(aic33_dapm_widgets));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200300
301 /* Set up N810 specific audio path audio_map */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200302 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200303
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200304 snd_soc_dapm_sync(dapm);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200305
306 return 0;
307}
308
309/* Digital audio interface glue - connects codec <--> CPU */
310static struct snd_soc_dai_link n810_dai = {
311 .name = "TLV320AIC33",
312 .stream_name = "AIC33",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000313 .cpu_dai_name = "omap-mcbsp-dai.1",
314 .platform_name = "omap-pcm-audio",
315 .codec_name = "tlv320aic3x-codec.2-0018",
316 .codec_dai_name = "tlv320aic3x-hifi",
Jarkko Nikula2e747962008-04-25 13:55:19 +0200317 .init = n810_aic33_init,
318 .ops = &n810_ops,
319};
320
321/* Audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000322static struct snd_soc_card snd_soc_n810 = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200323 .name = "N810",
324 .dai_link = &n810_dai,
325 .num_links = 1,
326};
327
Jarkko Nikula2e747962008-04-25 13:55:19 +0200328static struct platform_device *n810_snd_device;
329
330static int __init n810_soc_init(void)
331{
332 int err;
333 struct device *dev;
334
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100335 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200336 return -ENODEV;
337
338 n810_snd_device = platform_device_alloc("soc-audio", -1);
339 if (!n810_snd_device)
340 return -ENOMEM;
341
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000342 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200343 err = platform_device_add(n810_snd_device);
344 if (err)
345 goto err1;
346
347 dev = &n810_snd_device->dev;
348
349 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
350 if (IS_ERR(sys_clkout2_src)) {
351 dev_err(dev, "Could not get sys_clkout2_src clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300352 err = PTR_ERR(sys_clkout2_src);
353 goto err2;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200354 }
355 sys_clkout2 = clk_get(dev, "sys_clkout2");
356 if (IS_ERR(sys_clkout2)) {
357 dev_err(dev, "Could not get sys_clkout2\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300358 err = PTR_ERR(sys_clkout2);
359 goto err3;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200360 }
361 /*
362 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
363 * 96 MHz as its parent in order to get 12 MHz
364 */
365 func96m_clk = clk_get(dev, "func_96m_ck");
366 if (IS_ERR(func96m_clk)) {
367 dev_err(dev, "Could not get func 96M clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300368 err = PTR_ERR(func96m_clk);
369 goto err4;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200370 }
371 clk_set_parent(sys_clkout2_src, func96m_clk);
372 clk_set_rate(sys_clkout2, 12000000);
373
Alexander Beregalovf4c17242009-04-12 05:04:43 +0400374 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
375 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
376
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200377 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
378 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200379
380 return 0;
Jarkko Nikulae7845392008-08-26 13:32:57 +0300381err4:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200382 clk_put(sys_clkout2);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300383err3:
384 clk_put(sys_clkout2_src);
385err2:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200386 platform_device_del(n810_snd_device);
387err1:
388 platform_device_put(n810_snd_device);
389
390 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200391}
392
393static void __exit n810_soc_exit(void)
394{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200395 gpio_free(N810_SPEAKER_AMP_GPIO);
396 gpio_free(N810_HEADSET_AMP_GPIO);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300397 clk_put(sys_clkout2_src);
398 clk_put(sys_clkout2);
399 clk_put(func96m_clk);
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200400
Jarkko Nikula2e747962008-04-25 13:55:19 +0200401 platform_device_unregister(n810_snd_device);
402}
403
404module_init(n810_soc_init);
405module_exit(n810_soc_exit);
406
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300407MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200408MODULE_DESCRIPTION("ALSA SoC Nokia N810");
409MODULE_LICENSE("GPL");