blob: b60b1dfbc435f0036f9d18447b6e45c617e39439 [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>
25#include <linux/platform_device.h>
26#include <sound/core.h>
27#include <sound/pcm.h>
28#include <sound/soc.h>
29#include <sound/soc-dapm.h>
30
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>
Russell Kinga09e64f2008-08-05 16:14:15 +010034#include <mach/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{
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020060 int hp = 0, line1l = 0;
61
62 switch (n810_jack_func) {
63 case N810_JACK_HS:
64 line1l = 1;
65 case N810_JACK_HP:
66 hp = 1;
67 break;
68 case N810_JACK_MIC:
69 line1l = 1;
70 break;
71 }
72
Liam Girdwooda5302182008-07-07 13:35:17 +010073 if (n810_spk_func)
74 snd_soc_dapm_enable_pin(codec, "Ext Spk");
75 else
76 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Jarkko Nikula2e747962008-04-25 13:55:19 +020077
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020078 if (hp)
Liam Girdwooda5302182008-07-07 13:35:17 +010079 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
80 else
81 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +020082 if (line1l)
83 snd_soc_dapm_enable_pin(codec, "LINE1L");
84 else
85 snd_soc_dapm_disable_pin(codec, "LINE1L");
Liam Girdwooda5302182008-07-07 13:35:17 +010086
87 if (n810_dmic_func)
88 snd_soc_dapm_enable_pin(codec, "DMic");
89 else
Liam Girdwoodac8615b2008-07-08 13:20:39 +010090 snd_soc_dapm_disable_pin(codec, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010091
92 snd_soc_dapm_sync(codec);
Jarkko Nikula2e747962008-04-25 13:55:19 +020093}
94
95static int n810_startup(struct snd_pcm_substream *substream)
96{
Jarkko Nikula0be43052008-11-25 12:45:08 +020097 struct snd_pcm_runtime *runtime = substream->runtime;
Jarkko Nikula2e747962008-04-25 13:55:19 +020098 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Takashi Iwai9e30d772009-02-11 08:28:04 +010099 struct snd_soc_codec *codec = rtd->socdev->card->codec;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200100
Jarkko Nikula0be43052008-11-25 12:45:08 +0200101 snd_pcm_hw_constraint_minmax(runtime,
102 SNDRV_PCM_HW_PARAM_CHANNELS, 2, 2);
103
Jarkko Nikula2e747962008-04-25 13:55:19 +0200104 n810_ext_control(codec);
105 return clk_enable(sys_clkout2);
106}
107
108static void n810_shutdown(struct snd_pcm_substream *substream)
109{
110 clk_disable(sys_clkout2);
111}
112
113static int n810_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
115{
116 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +0100117 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
118 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200119 int err;
120
121 /* Set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100122 err = snd_soc_dai_set_fmt(codec_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200123 SND_SOC_DAIFMT_I2S |
124 SND_SOC_DAIFMT_NB_NF |
125 SND_SOC_DAIFMT_CBM_CFM);
126 if (err < 0)
127 return err;
128
129 /* Set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100130 err = snd_soc_dai_set_fmt(cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200131 SND_SOC_DAIFMT_I2S |
132 SND_SOC_DAIFMT_NB_NF |
133 SND_SOC_DAIFMT_CBM_CFM);
134 if (err < 0)
135 return err;
136
137 /* Set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100138 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200139 SND_SOC_CLOCK_IN);
140
141 return err;
142}
143
144static struct snd_soc_ops n810_ops = {
145 .startup = n810_startup,
146 .hw_params = n810_hw_params,
147 .shutdown = n810_shutdown,
148};
149
150static int n810_get_spk(struct snd_kcontrol *kcontrol,
151 struct snd_ctl_elem_value *ucontrol)
152{
153 ucontrol->value.integer.value[0] = n810_spk_func;
154
155 return 0;
156}
157
158static int n810_set_spk(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
160{
161 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
162
163 if (n810_spk_func == ucontrol->value.integer.value[0])
164 return 0;
165
166 n810_spk_func = ucontrol->value.integer.value[0];
167 n810_ext_control(codec);
168
169 return 1;
170}
171
172static int n810_get_jack(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
174{
175 ucontrol->value.integer.value[0] = n810_jack_func;
176
177 return 0;
178}
179
180static int n810_set_jack(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_value *ucontrol)
182{
183 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
184
185 if (n810_jack_func == ucontrol->value.integer.value[0])
186 return 0;
187
188 n810_jack_func = ucontrol->value.integer.value[0];
189 n810_ext_control(codec);
190
191 return 1;
192}
193
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300194static int n810_get_input(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
196{
197 ucontrol->value.integer.value[0] = n810_dmic_func;
198
199 return 0;
200}
201
202static int n810_set_input(struct snd_kcontrol *kcontrol,
203 struct snd_ctl_elem_value *ucontrol)
204{
205 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
206
207 if (n810_dmic_func == ucontrol->value.integer.value[0])
208 return 0;
209
210 n810_dmic_func = ucontrol->value.integer.value[0];
211 n810_ext_control(codec);
212
213 return 1;
214}
215
Jarkko Nikula2e747962008-04-25 13:55:19 +0200216static int n810_spk_event(struct snd_soc_dapm_widget *w,
217 struct snd_kcontrol *k, int event)
218{
219 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200220 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200221 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200222 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200223
224 return 0;
225}
226
227static int n810_jack_event(struct snd_soc_dapm_widget *w,
228 struct snd_kcontrol *k, int event)
229{
230 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200231 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200232 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200233 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200234
235 return 0;
236}
237
238static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
239 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
240 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300241 SND_SOC_DAPM_MIC("DMic", NULL),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200242};
243
Mark Brown1a250592008-05-13 14:58:57 +0200244static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200245 {"Headphone Jack", NULL, "HPLOUT"},
246 {"Headphone Jack", NULL, "HPROUT"},
247
248 {"Ext Spk", NULL, "LLOUT"},
249 {"Ext Spk", NULL, "RLOUT"},
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300250
251 {"DMic Rate 64", NULL, "Mic Bias 2V"},
252 {"Mic Bias 2V", NULL, "DMic"},
Jarkko Nikula2e747962008-04-25 13:55:19 +0200253};
254
255static const char *spk_function[] = {"Off", "On"};
Jarkko Nikulaf8d5fc92009-03-18 16:46:54 +0200256static const char *jack_function[] = {"Off", "Headphone", "Headset", "Mic"};
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300257static const char *input_function[] = {"ADC", "Digital Mic"};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200258static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200259 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
260 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300261 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200262};
263
264static const struct snd_kcontrol_new aic33_n810_controls[] = {
265 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
266 n810_get_spk, n810_set_spk),
267 SOC_ENUM_EXT("Jack Function", n810_enum[1],
268 n810_get_jack, n810_set_jack),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300269 SOC_ENUM_EXT("Input Select", n810_enum[2],
270 n810_get_input, n810_set_input),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200271};
272
273static int n810_aic33_init(struct snd_soc_codec *codec)
274{
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100275 int err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200276
277 /* Not connected */
Mark Brown869fbb32008-09-27 10:48:31 +0100278 snd_soc_dapm_nc_pin(codec, "MONO_LOUT");
279 snd_soc_dapm_nc_pin(codec, "HPLCOM");
280 snd_soc_dapm_nc_pin(codec, "HPRCOM");
Jarkko Nikula13b9d2a2009-03-18 16:46:53 +0200281 snd_soc_dapm_nc_pin(codec, "MIC3L");
282 snd_soc_dapm_nc_pin(codec, "MIC3R");
283 snd_soc_dapm_nc_pin(codec, "LINE1R");
284 snd_soc_dapm_nc_pin(codec, "LINE2L");
285 snd_soc_dapm_nc_pin(codec, "LINE2R");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200286
287 /* Add N810 specific controls */
Philipp Zabeleb5f6d752009-03-12 11:07:54 +0100288 err = snd_soc_add_controls(codec, aic33_n810_controls,
289 ARRAY_SIZE(aic33_n810_controls));
290 if (err < 0)
291 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200292
293 /* Add N810 specific widgets */
Mark Brown1a250592008-05-13 14:58:57 +0200294 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
295 ARRAY_SIZE(aic33_dapm_widgets));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296
297 /* Set up N810 specific audio path audio_map */
Mark Brown1a250592008-05-13 14:58:57 +0200298 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200299
Liam Girdwooda5302182008-07-07 13:35:17 +0100300 snd_soc_dapm_sync(codec);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200301
302 return 0;
303}
304
305/* Digital audio interface glue - connects codec <--> CPU */
306static struct snd_soc_dai_link n810_dai = {
307 .name = "TLV320AIC33",
308 .stream_name = "AIC33",
309 .cpu_dai = &omap_mcbsp_dai[0],
310 .codec_dai = &aic3x_dai,
311 .init = n810_aic33_init,
312 .ops = &n810_ops,
313};
314
315/* Audio machine driver */
Mark Brown87506542008-11-18 20:50:34 +0000316static struct snd_soc_card snd_soc_n810 = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200317 .name = "N810",
Mark Brown87689d52008-12-02 16:01:14 +0000318 .platform = &omap_soc_platform,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200319 .dai_link = &n810_dai,
320 .num_links = 1,
321};
322
323/* Audio private data */
324static struct aic3x_setup_data n810_aic33_setup = {
Jean Delvareba8ed122008-09-22 14:15:53 +0200325 .i2c_bus = 2,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200326 .i2c_address = 0x18,
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300327 .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
328 .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200329};
330
331/* Audio subsystem */
332static struct snd_soc_device n810_snd_devdata = {
Mark Brown87506542008-11-18 20:50:34 +0000333 .card = &snd_soc_n810,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200334 .codec_dev = &soc_codec_dev_aic3x,
335 .codec_data = &n810_aic33_setup,
336};
337
338static struct platform_device *n810_snd_device;
339
340static int __init n810_soc_init(void)
341{
342 int err;
343 struct device *dev;
344
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100345 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200346 return -ENODEV;
347
348 n810_snd_device = platform_device_alloc("soc-audio", -1);
349 if (!n810_snd_device)
350 return -ENOMEM;
351
352 platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
353 n810_snd_devdata.dev = &n810_snd_device->dev;
354 *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
355 err = platform_device_add(n810_snd_device);
356 if (err)
357 goto err1;
358
359 dev = &n810_snd_device->dev;
360
361 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
362 if (IS_ERR(sys_clkout2_src)) {
363 dev_err(dev, "Could not get sys_clkout2_src clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300364 err = PTR_ERR(sys_clkout2_src);
365 goto err2;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200366 }
367 sys_clkout2 = clk_get(dev, "sys_clkout2");
368 if (IS_ERR(sys_clkout2)) {
369 dev_err(dev, "Could not get sys_clkout2\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300370 err = PTR_ERR(sys_clkout2);
371 goto err3;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200372 }
373 /*
374 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
375 * 96 MHz as its parent in order to get 12 MHz
376 */
377 func96m_clk = clk_get(dev, "func_96m_ck");
378 if (IS_ERR(func96m_clk)) {
379 dev_err(dev, "Could not get func 96M clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300380 err = PTR_ERR(func96m_clk);
381 goto err4;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200382 }
383 clk_set_parent(sys_clkout2_src, func96m_clk);
384 clk_set_rate(sys_clkout2, 12000000);
385
Alexander Beregalovf4c17242009-04-12 05:04:43 +0400386 BUG_ON((gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0) ||
387 (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0));
388
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200389 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
390 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200391
392 return 0;
Jarkko Nikulae7845392008-08-26 13:32:57 +0300393err4:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200394 clk_put(sys_clkout2);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300395err3:
396 clk_put(sys_clkout2_src);
397err2:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200398 platform_device_del(n810_snd_device);
399err1:
400 platform_device_put(n810_snd_device);
401
402 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200403}
404
405static void __exit n810_soc_exit(void)
406{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200407 gpio_free(N810_SPEAKER_AMP_GPIO);
408 gpio_free(N810_HEADSET_AMP_GPIO);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300409 clk_put(sys_clkout2_src);
410 clk_put(sys_clkout2);
411 clk_put(func96m_clk);
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200412
Jarkko Nikula2e747962008-04-25 13:55:19 +0200413 platform_device_unregister(n810_snd_device);
414}
415
416module_init(n810_soc_init);
417module_exit(n810_soc_exit);
418
Jarkko Nikulab08f7a62009-04-17 14:42:26 +0300419MODULE_AUTHOR("Jarkko Nikula <jhnikula@gmail.com>");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200420MODULE_DESCRIPTION("ALSA SoC Nokia N810");
421MODULE_LICENSE("GPL");