blob: 597be412f1e40080336be9604940cbfab72012fd [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 Nikula7ec41ee2011-08-11 15:44:57 +03006 * Contact: Jarkko Nikula <jarkko.nikula@bitmer.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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040034#include <linux/module.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"
Jarkko Nikula2e747962008-04-25 13:55:19 +020039
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;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200119 int err;
120
Jarkko Nikula2e747962008-04-25 13:55:19 +0200121 /* Set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100122 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200123 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
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300178static 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
Jarkko Nikula2e747962008-04-25 13:55:19 +0200200static 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))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200204 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200205 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200206 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200207
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))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200215 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200216 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200217 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200218
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),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300225 SND_SOC_DAPM_MIC("DMic", NULL),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200226};
227
Mark Brown1a250592008-05-13 14:58:57 +0200228static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200229 {"Headphone Jack", NULL, "HPLOUT"},
230 {"Headphone Jack", NULL, "HPROUT"},
231
232 {"Ext Spk", NULL, "LLOUT"},
233 {"Ext Spk", NULL, "RLOUT"},
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300234
235 {"DMic Rate 64", NULL, "Mic Bias 2V"},
236 {"Mic Bias 2V", NULL, "DMic"},
Jarkko Nikula2e747962008-04-25 13:55:19 +0200237};
238
239static const char *spk_function[] = {"Off", "On"};
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +0200240static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300241static const char *input_function[] = {"ADC", "Digital Mic"};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200242static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200243 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
244 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300245 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200246};
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),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300253 SOC_ENUM_EXT("Input Select", n810_enum[2],
254 n810_get_input, n810_set_input),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200255};
256
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000257static int n810_aic33_init(struct snd_soc_pcm_runtime *rtd)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200258{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000259 struct snd_soc_codec *codec = rtd->codec;
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200260 struct snd_soc_dapm_context *dapm = &codec->dapm;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200261
262 /* Not connected */
Liam Girdwoodce6120c2010-11-05 15:53:46 +0200263 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");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200271
Peter Ujfalusi8966c2d2011-10-10 15:34:10 +0300272 return 0;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200273}
274
275/* Digital audio interface glue - connects codec <--> CPU */
276static struct snd_soc_dai_link n810_dai = {
277 .name = "TLV320AIC33",
278 .stream_name = "AIC33",
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000279 .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",
Jarkko Nikulacf9feff2011-09-30 16:07:45 +0300283 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
284 SND_SOC_DAIFMT_CBM_CFM,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200285 .init = n810_aic33_init,
286 .ops = &n810_ops,
287};
288
289/* Audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000290static struct snd_soc_card snd_soc_n810 = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200291 .name = "N810",
Axel Linb425b882011-12-22 11:08:59 +0800292 .owner = THIS_MODULE,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200293 .dai_link = &n810_dai,
294 .num_links = 1,
Peter Ujfalusi8966c2d2011-10-10 15:34:10 +0300295
296 .controls = aic33_n810_controls,
297 .num_controls = ARRAY_SIZE(aic33_n810_controls),
298 .dapm_widgets = aic33_dapm_widgets,
299 .num_dapm_widgets = ARRAY_SIZE(aic33_dapm_widgets),
300 .dapm_routes = audio_map,
301 .num_dapm_routes = ARRAY_SIZE(audio_map),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200302};
303
Jarkko Nikula2e747962008-04-25 13:55:19 +0200304static struct platform_device *n810_snd_device;
305
306static int __init n810_soc_init(void)
307{
308 int err;
309 struct device *dev;
310
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100311 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200312 return -ENODEV;
313
314 n810_snd_device = platform_device_alloc("soc-audio", -1);
315 if (!n810_snd_device)
316 return -ENOMEM;
317
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000318 platform_set_drvdata(n810_snd_device, &snd_soc_n810);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200319 err = platform_device_add(n810_snd_device);
320 if (err)
321 goto err1;
322
323 dev = &n810_snd_device->dev;
324
325 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
326 if (IS_ERR(sys_clkout2_src)) {
327 dev_err(dev, "Could not get sys_clkout2_src clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300328 err = PTR_ERR(sys_clkout2_src);
329 goto err2;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200330 }
331 sys_clkout2 = clk_get(dev, "sys_clkout2");
332 if (IS_ERR(sys_clkout2)) {
333 dev_err(dev, "Could not get sys_clkout2\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300334 err = PTR_ERR(sys_clkout2);
335 goto err3;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200336 }
337 /*
338 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
339 * 96 MHz as its parent in order to get 12 MHz
340 */
341 func96m_clk = clk_get(dev, "func_96m_ck");
342 if (IS_ERR(func96m_clk)) {
343 dev_err(dev, "Could not get func 96M clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300344 err = PTR_ERR(func96m_clk);
345 goto err4;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200346 }
347 clk_set_parent(sys_clkout2_src, func96m_clk);
348 clk_set_rate(sys_clkout2, 12000000);
349
Alexander Beregalovf4c17242009-04-12 05:04:43 +0400350 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
351 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
352
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200353 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
354 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200355
356 return 0;
Jarkko Nikulae7845392008-08-26 13:32:57 +0300357err4:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200358 clk_put(sys_clkout2);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300359err3:
360 clk_put(sys_clkout2_src);
361err2:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200362 platform_device_del(n810_snd_device);
363err1:
364 platform_device_put(n810_snd_device);
365
366 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200367}
368
369static void __exit n810_soc_exit(void)
370{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200371 gpio_free(N810_SPEAKER_AMP_GPIO);
372 gpio_free(N810_HEADSET_AMP_GPIO);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300373 clk_put(sys_clkout2_src);
374 clk_put(sys_clkout2);
375 clk_put(func96m_clk);
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200376
Jarkko Nikula2e747962008-04-25 13:55:19 +0200377 platform_device_unregister(n810_snd_device);
378}
379
380module_init(n810_soc_init);
381module_exit(n810_soc_exit);
382
Jarkko Nikula7ec41ee2011-08-11 15:44:57 +0300383MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@bitmer.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200384MODULE_DESCRIPTION("ALSA SoC Nokia N810");
385MODULE_LICENSE("GPL");