blob: fe215f3e8afc4791044349e61e3a895deb29654a [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>
Jarkko Nikula2e747962008-04-25 13:55:19 +020030
31#include <asm/mach-types.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010032#include <mach/hardware.h>
Jarkko Nikulaf99a6332008-05-15 11:01:36 +020033#include <linux/gpio.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070034#include <plat/mcbsp.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020035
36#include "omap-mcbsp.h"
37#include "omap-pcm.h"
38#include "../codecs/tlv320aic3x.h"
39
Jarkko Nikulaf99a6332008-05-15 11:01:36 +020040#define N810_HEADSET_AMP_GPIO 10
41#define N810_SPEAKER_AMP_GPIO 101
Jarkko Nikula2e747962008-04-25 13:55:19 +020042
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020043enum {
44 N810_JACK_DISABLED,
45 N810_JACK_HP,
46 N810_JACK_HS,
47 N810_JACK_MIC,
48};
49
Jarkko Nikula2e747962008-04-25 13:55:19 +020050static 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;
Jarkko Nikula90b9e472008-06-25 14:58:47 +030056static int n810_dmic_func;
Jarkko Nikula2e747962008-04-25 13:55:19 +020057
58static void n810_ext_control(struct snd_soc_codec *codec)
59{
Liam Girdwoodce6120c2010-11-05 15:53:46 +020060 struct snd_soc_dapm_context *dapm = &codec->dapm;
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020061 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
Liam Girdwooda5302182008-07-07 13:35:17 +010074 if (n810_spk_func)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020075 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
Liam Girdwooda5302182008-07-07 13:35:17 +010076 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020077 snd_soc_dapm_disable_pin(dapm, "Ext Spk");
Jarkko Nikula2e747962008-04-25 13:55:19 +020078
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020079 if (hp)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020080 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
Liam Girdwooda5302182008-07-07 13:35:17 +010081 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020082 snd_soc_dapm_disable_pin(dapm, "Headphone Jack");
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020083 if (line1l)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020084 snd_soc_dapm_enable_pin(dapm, "LINE1L");
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020085 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020086 snd_soc_dapm_disable_pin(dapm, "LINE1L");
Liam Girdwooda5302182008-07-07 13:35:17 +010087
88 if (n810_dmic_func)
Liam Girdwoodce6120c2010-11-05 15:53:46 +020089 snd_soc_dapm_enable_pin(dapm, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010090 else
Liam Girdwoodce6120c2010-11-05 15:53:46 +020091 snd_soc_dapm_disable_pin(dapm, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010092
Liam Girdwoodce6120c2010-11-05 15:53:46 +020093 snd_soc_dapm_sync(dapm);
Jarkko Nikula2e747962008-04-25 13:55:19 +020094}
95
96static int n810_startup(struct snd_pcm_substream *substream)
97{
Jarkko Nikula0be43052008-11-25 12:45:08 +020098 struct snd_pcm_runtime *runtime = substream->runtime;
Jarkko Nikula2e747962008-04-25 13:55:19 +020099 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000100 struct snd_soc_codec *codec = rtd->codec;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200101
Jarkko Nikula0be43052008-11-25 12:45:08 +0200102 snd_pcm_hw_constraint_minmax(runtime,
103 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
104
Jarkko Nikula2e747962008-04-25 13:55:19 +0200105 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;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000118 struct snd_soc_dai *codec_dai = rtd->codec_dai;
119 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200120 int err;
121
122 /* Set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100123 err = snd_soc_dai_set_fmt(codec_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200124 SND_SOC_DAIFMT_I2S |
125 SND_SOC_DAIFMT_NB_NF |
126 SND_SOC_DAIFMT_CBM_CFM);
127 if (err < 0)
128 return err;
129
130 /* Set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100131 err = snd_soc_dai_set_fmt(cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200132 SND_SOC_DAIFMT_I2S |
133 SND_SOC_DAIFMT_NB_NF |
134 SND_SOC_DAIFMT_CBM_CFM);
135 if (err < 0)
136 return err;
137
138 /* Set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100139 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200140 SND_SOC_CLOCK_IN);
141
142 return err;
143}
144
145static struct snd_soc_ops n810_ops = {
146 .startup = n810_startup,
147 .hw_params = n810_hw_params,
148 .shutdown = n810_shutdown,
149};
150
151static int n810_get_spk(struct snd_kcontrol *kcontrol,
152 struct snd_ctl_elem_value *ucontrol)
153{
154 ucontrol->value.integer.value[0] = n810_spk_func;
155
156 return 0;
157}
158
159static int n810_set_spk(struct snd_kcontrol *kcontrol,
160 struct snd_ctl_elem_value *ucontrol)
161{
162 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
163
164 if (n810_spk_func == ucontrol->value.integer.value[0])
165 return 0;
166
167 n810_spk_func = ucontrol->value.integer.value[0];
168 n810_ext_control(codec);
169
170 return 1;
171}
172
173static int n810_get_jack(struct snd_kcontrol *kcontrol,
174 struct snd_ctl_elem_value *ucontrol)
175{
176 ucontrol->value.integer.value[0] = n810_jack_func;
177
178 return 0;
179}
180
181static int n810_set_jack(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
183{
184 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
185
186 if (n810_jack_func == ucontrol->value.integer.value[0])
187 return 0;
188
189 n810_jack_func = ucontrol->value.integer.value[0];
190 n810_ext_control(codec);
191
192 return 1;
193}
194
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300195static int n810_get_input(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
197{
198 ucontrol->value.integer.value[0] = n810_dmic_func;
199
200 return 0;
201}
202
203static int n810_set_input(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
205{
206 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
207
208 if (n810_dmic_func == ucontrol->value.integer.value[0])
209 return 0;
210
211 n810_dmic_func = ucontrol->value.integer.value[0];
212 n810_ext_control(codec);
213
214 return 1;
215}
216
Jarkko Nikula2e747962008-04-25 13:55:19 +0200217static int n810_spk_event(struct snd_soc_dapm_widget *w,
218 struct snd_kcontrol *k, int event)
219{
220 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200221 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200222 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200223 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200224
225 return 0;
226}
227
228static int n810_jack_event(struct snd_soc_dapm_widget *w,
229 struct snd_kcontrol *k, int event)
230{
231 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200232 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200233 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200234 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200235
236 return 0;
237}
238
239static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
240 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
241 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300242 SND_SOC_DAPM_MIC("DMic", NULL),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200243};
244
Mark Brown1a250592008-05-13 14:58:57 +0200245static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200246 {"Headphone Jack", NULL, "HPLOUT"},
247 {"Headphone Jack", NULL, "HPROUT"},
248
249 {"Ext Spk", NULL, "LLOUT"},
250 {"Ext Spk", NULL, "RLOUT"},
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300251
252 {"DMic Rate 64", NULL, "Mic Bias 2V"},
253 {"Mic Bias 2V", NULL, "DMic"},
Jarkko Nikula2e747962008-04-25 13:55:19 +0200254};
255
256static const char *spk_function[] = {"Off", "On"};
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +0200257static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300258static const char *input_function[] = {"ADC", "Digital Mic"};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200259static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200260 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300262 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200263};
264
265static const struct snd_kcontrol_new aic33_n810_controls[] = {
266 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
267 n810_get_spk, n810_set_spk),
268 SOC_ENUM_EXT("Jack Function", n810_enum[1],
269 n810_get_jack, n810_set_jack),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300270 SOC_ENUM_EXT("Input Select", n810_enum[2],
271 n810_get_input, n810_set_input),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200272};
273
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000274static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200275{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000276 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200277 struct snd_soc_dapm_context *dapm = &codec->dapm;
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100278 int err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200279
280 /* Not connected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200281 snd_soc_dapm_nc_pin(dapm, "MONO_LOUT");
282 snd_soc_dapm_nc_pin(dapm, "HPLCOM");
283 snd_soc_dapm_nc_pin(dapm, "HPRCOM");
284 snd_soc_dapm_nc_pin(dapm, "MIC3L");
285 snd_soc_dapm_nc_pin(dapm, "MIC3R");
286 snd_soc_dapm_nc_pin(dapm, "LINE1R");
287 snd_soc_dapm_nc_pin(dapm, "LINE2L");
288 snd_soc_dapm_nc_pin(dapm, "LINE2R");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200289
290 /* Add N810 specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100291 err = snd_soc_add_controls(codec, aic33_n810_controls,
292 ARRAY_SIZE(aic33_n810_controls));
293 if (err < 0)
294 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200295
296 /* Add N810 specific widgets */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200297 snd_soc_dapm_new_controls(dapm, aic33_dapm_widgets,
Mark Brown1a250592008-05-13 14:58:57 +0200298 ARRAY_SIZE(aic33_dapm_widgets));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200299
300 /* Set up N810 specific audio path audio_map */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200301 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200302
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200303 snd_soc_dapm_sync(dapm);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200304
305 return 0;
306}
307
308/* Digital audio interface glue - connects codec <--> CPU */
309static struct snd_soc_dai_link n810_dai = {
310 .name = "TLV320AIC33",
311 .stream_name = "AIC33",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000312 .cpu_dai_name = "omap-mcbsp-dai.1",
313 .platform_name = "omap-pcm-audio",
314 .codec_name = "tlv320aic3x-codec.2-0018",
315 .codec_dai_name = "tlv320aic3x-hifi",
Jarkko Nikula2e747962008-04-25 13:55:19 +0200316 .init = n810_aic33_init,
317 .ops = &n810_ops,
318};
319
320/* Audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000321static struct snd_soc_card snd_soc_n810 = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200322 .name = "N810",
323 .dai_link = &n810_dai,
324 .num_links = 1,
325};
326
Jarkko Nikula2e747962008-04-25 13:55:19 +0200327static struct platform_device *n810_snd_device;
328
329static int __init n810_soc_init(void)
330{
331 int err;
332 struct device *dev;
333
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100334 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200335 return -ENODEV;
336
337 n810_snd_device = platform_device_alloc("soc-audio", -1);
338 if (!n810_snd_device)
339 return -ENOMEM;
340
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000341 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200342 err = platform_device_add(n810_snd_device);
343 if (err)
344 goto err1;
345
346 dev = &n810_snd_device->dev;
347
348 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
349 if (IS_ERR(sys_clkout2_src)) {
350 dev_err(dev, "Could not get sys_clkout2_src clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300351 err = PTR_ERR(sys_clkout2_src);
352 goto err2;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200353 }
354 sys_clkout2 = clk_get(dev, "sys_clkout2");
355 if (IS_ERR(sys_clkout2)) {
356 dev_err(dev, "Could not get sys_clkout2\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300357 err = PTR_ERR(sys_clkout2);
358 goto err3;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200359 }
360 /*
361 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
362 * 96 MHz as its parent in order to get 12 MHz
363 */
364 func96m_clk = clk_get(dev, "func_96m_ck");
365 if (IS_ERR(func96m_clk)) {
366 dev_err(dev, "Could not get func 96M clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300367 err = PTR_ERR(func96m_clk);
368 goto err4;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200369 }
370 clk_set_parent(sys_clkout2_src, func96m_clk);
371 clk_set_rate(sys_clkout2, 12000000);
372
Alexander Beregalovf4c17242009-04-12 05:04:43 +0400373 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
374 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
375
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200376 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
377 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200378
379 return 0;
Jarkko Nikulae7845392008-08-26 13:32:57 +0300380err4:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200381 clk_put(sys_clkout2);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300382err3:
383 clk_put(sys_clkout2_src);
384err2:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200385 platform_device_del(n810_snd_device);
386err1:
387 platform_device_put(n810_snd_device);
388
389 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200390}
391
392static void __exit n810_soc_exit(void)
393{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200394 gpio_free(N810_SPEAKER_AMP_GPIO);
395 gpio_free(N810_HEADSET_AMP_GPIO);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300396 clk_put(sys_clkout2_src);
397 clk_put(sys_clkout2);
398 clk_put(func96m_clk);
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200399
Jarkko Nikula2e747962008-04-25 13:55:19 +0200400 platform_device_unregister(n810_snd_device);
401}
402
403module_init(n810_soc_init);
404module_exit(n810_soc_exit);
405
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300406MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200407MODULE_DESCRIPTION("ALSA SoC Nokia N810");
408MODULE_LICENSE("GPL");