blob: 87d0ed01f65adf5682dcf8bdc931424d454ba60d [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 *
6 * Contact: Jarkko Nikula <jarkko.nikula@nokia.com>
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>
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
43static struct clk *sys_clkout2;
44static struct clk *sys_clkout2_src;
45static struct clk *func96m_clk;
46
47static int n810_spk_func;
48static int n810_jack_func;
Jarkko Nikula90b9e472008-06-25 14:58:47 +030049static int n810_dmic_func;
Jarkko Nikula2e747962008-04-25 13:55:19 +020050
51static void n810_ext_control(struct snd_soc_codec *codec)
52{
Liam Girdwooda5302182008-07-07 13:35:17 +010053 if (n810_spk_func)
54 snd_soc_dapm_enable_pin(codec, "Ext Spk");
55 else
56 snd_soc_dapm_disable_pin(codec, "Ext Spk");
Jarkko Nikula2e747962008-04-25 13:55:19 +020057
Liam Girdwooda5302182008-07-07 13:35:17 +010058 if (n810_jack_func)
59 snd_soc_dapm_enable_pin(codec, "Headphone Jack");
60 else
61 snd_soc_dapm_disable_pin(codec, "Headphone Jack");
62
63 if (n810_dmic_func)
64 snd_soc_dapm_enable_pin(codec, "DMic");
65 else
Liam Girdwoodac8615b2008-07-08 13:20:39 +010066 snd_soc_dapm_disable_pin(codec, "DMic");
Liam Girdwooda5302182008-07-07 13:35:17 +010067
68 snd_soc_dapm_sync(codec);
Jarkko Nikula2e747962008-04-25 13:55:19 +020069}
70
71static int n810_startup(struct snd_pcm_substream *substream)
72{
73 struct snd_soc_pcm_runtime *rtd = substream->private_data;
74 struct snd_soc_codec *codec = rtd->socdev->codec;
75
76 n810_ext_control(codec);
77 return clk_enable(sys_clkout2);
78}
79
80static void n810_shutdown(struct snd_pcm_substream *substream)
81{
82 clk_disable(sys_clkout2);
83}
84
85static int n810_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
87{
88 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwood8687eb82008-07-07 16:08:07 +010089 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
90 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
Jarkko Nikula2e747962008-04-25 13:55:19 +020091 int err;
92
93 /* Set codec DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +010094 err = snd_soc_dai_set_fmt(codec_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +020095 SND_SOC_DAIFMT_I2S |
96 SND_SOC_DAIFMT_NB_NF |
97 SND_SOC_DAIFMT_CBM_CFM);
98 if (err < 0)
99 return err;
100
101 /* Set cpu DAI configuration */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100102 err = snd_soc_dai_set_fmt(cpu_dai,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200103 SND_SOC_DAIFMT_I2S |
104 SND_SOC_DAIFMT_NB_NF |
105 SND_SOC_DAIFMT_CBM_CFM);
106 if (err < 0)
107 return err;
108
109 /* Set the codec system clock for DAC and ADC */
Liam Girdwood64105cf2008-07-08 13:19:18 +0100110 err = snd_soc_dai_set_sysclk(codec_dai, 0, 12000000,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200111 SND_SOC_CLOCK_IN);
112
113 return err;
114}
115
116static struct snd_soc_ops n810_ops = {
117 .startup = n810_startup,
118 .hw_params = n810_hw_params,
119 .shutdown = n810_shutdown,
120};
121
122static int n810_get_spk(struct snd_kcontrol *kcontrol,
123 struct snd_ctl_elem_value *ucontrol)
124{
125 ucontrol->value.integer.value[0] = n810_spk_func;
126
127 return 0;
128}
129
130static int n810_set_spk(struct snd_kcontrol *kcontrol,
131 struct snd_ctl_elem_value *ucontrol)
132{
133 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
134
135 if (n810_spk_func == ucontrol->value.integer.value[0])
136 return 0;
137
138 n810_spk_func = ucontrol->value.integer.value[0];
139 n810_ext_control(codec);
140
141 return 1;
142}
143
144static int n810_get_jack(struct snd_kcontrol *kcontrol,
145 struct snd_ctl_elem_value *ucontrol)
146{
147 ucontrol->value.integer.value[0] = n810_jack_func;
148
149 return 0;
150}
151
152static int n810_set_jack(struct snd_kcontrol *kcontrol,
153 struct snd_ctl_elem_value *ucontrol)
154{
155 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
156
157 if (n810_jack_func == ucontrol->value.integer.value[0])
158 return 0;
159
160 n810_jack_func = ucontrol->value.integer.value[0];
161 n810_ext_control(codec);
162
163 return 1;
164}
165
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300166static int n810_get_input(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
168{
169 ucontrol->value.integer.value[0] = n810_dmic_func;
170
171 return 0;
172}
173
174static int n810_set_input(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_value *ucontrol)
176{
177 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
178
179 if (n810_dmic_func == ucontrol->value.integer.value[0])
180 return 0;
181
182 n810_dmic_func = ucontrol->value.integer.value[0];
183 n810_ext_control(codec);
184
185 return 1;
186}
187
Jarkko Nikula2e747962008-04-25 13:55:19 +0200188static int n810_spk_event(struct snd_soc_dapm_widget *w,
189 struct snd_kcontrol *k, int event)
190{
191 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200192 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200193 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200194 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200195
196 return 0;
197}
198
199static int n810_jack_event(struct snd_soc_dapm_widget *w,
200 struct snd_kcontrol *k, int event)
201{
202 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200203 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200204 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200205 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200206
207 return 0;
208}
209
210static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
211 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
212 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300213 SND_SOC_DAPM_MIC("DMic", NULL),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200214};
215
Mark Brown1a250592008-05-13 14:58:57 +0200216static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200217 {"Headphone Jack", NULL, "HPLOUT"},
218 {"Headphone Jack", NULL, "HPROUT"},
219
220 {"Ext Spk", NULL, "LLOUT"},
221 {"Ext Spk", NULL, "RLOUT"},
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300222
223 {"DMic Rate 64", NULL, "Mic Bias 2V"},
224 {"Mic Bias 2V", NULL, "DMic"},
Jarkko Nikula2e747962008-04-25 13:55:19 +0200225};
226
227static const char *spk_function[] = {"Off", "On"};
228static const char *jack_function[] = {"Off", "Headphone"};
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300229static const char *input_function[] = {"ADC", "Digital Mic"};
Jarkko Nikula2e747962008-04-25 13:55:19 +0200230static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200231 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
232 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300233 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(input_function), input_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200234};
235
236static const struct snd_kcontrol_new aic33_n810_controls[] = {
237 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
238 n810_get_spk, n810_set_spk),
239 SOC_ENUM_EXT("Jack Function", n810_enum[1],
240 n810_get_jack, n810_set_jack),
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300241 SOC_ENUM_EXT("Input Select", n810_enum[2],
242 n810_get_input, n810_set_input),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200243};
244
245static int n810_aic33_init(struct snd_soc_codec *codec)
246{
247 int i, err;
248
249 /* Not connected */
Liam Girdwooda5302182008-07-07 13:35:17 +0100250 snd_soc_dapm_disable_pin(codec, "MONO_LOUT");
251 snd_soc_dapm_disable_pin(codec, "HPLCOM");
252 snd_soc_dapm_disable_pin(codec, "HPRCOM");
Jarkko Nikula2e747962008-04-25 13:55:19 +0200253
254 /* Add N810 specific controls */
255 for (i = 0; i < ARRAY_SIZE(aic33_n810_controls); i++) {
256 err = snd_ctl_add(codec->card,
257 snd_soc_cnew(&aic33_n810_controls[i], codec, NULL));
258 if (err < 0)
259 return err;
260 }
261
262 /* Add N810 specific widgets */
Mark Brown1a250592008-05-13 14:58:57 +0200263 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
264 ARRAY_SIZE(aic33_dapm_widgets));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200265
266 /* Set up N810 specific audio path audio_map */
Mark Brown1a250592008-05-13 14:58:57 +0200267 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200268
Liam Girdwooda5302182008-07-07 13:35:17 +0100269 snd_soc_dapm_sync(codec);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200270
271 return 0;
272}
273
274/* Digital audio interface glue - connects codec <--> CPU */
275static struct snd_soc_dai_link n810_dai = {
276 .name = "TLV320AIC33",
277 .stream_name = "AIC33",
278 .cpu_dai = &omap_mcbsp_dai[0],
279 .codec_dai = &aic3x_dai,
280 .init = n810_aic33_init,
281 .ops = &n810_ops,
282};
283
284/* Audio machine driver */
285static struct snd_soc_machine snd_soc_machine_n810 = {
286 .name = "N810",
287 .dai_link = &n810_dai,
288 .num_links = 1,
289};
290
291/* Audio private data */
292static struct aic3x_setup_data n810_aic33_setup = {
293 .i2c_address = 0x18,
Jarkko Nikula90b9e472008-06-25 14:58:47 +0300294 .gpio_func[0] = AIC3X_GPIO1_FUNC_DISABLED,
295 .gpio_func[1] = AIC3X_GPIO2_FUNC_DIGITAL_MIC_INPUT,
Jarkko Nikula2e747962008-04-25 13:55:19 +0200296};
297
298/* Audio subsystem */
299static struct snd_soc_device n810_snd_devdata = {
300 .machine = &snd_soc_machine_n810,
301 .platform = &omap_soc_platform,
302 .codec_dev = &soc_codec_dev_aic3x,
303 .codec_data = &n810_aic33_setup,
304};
305
306static struct platform_device *n810_snd_device;
307
308static int __init n810_soc_init(void)
309{
310 int err;
311 struct device *dev;
312
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100313 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200314 return -ENODEV;
315
316 n810_snd_device = platform_device_alloc("soc-audio", -1);
317 if (!n810_snd_device)
318 return -ENOMEM;
319
320 platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
321 n810_snd_devdata.dev = &n810_snd_device->dev;
322 *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
323 err = platform_device_add(n810_snd_device);
324 if (err)
325 goto err1;
326
327 dev = &n810_snd_device->dev;
328
329 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
330 if (IS_ERR(sys_clkout2_src)) {
331 dev_err(dev, "Could not get sys_clkout2_src clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300332 err = PTR_ERR(sys_clkout2_src);
333 goto err2;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200334 }
335 sys_clkout2 = clk_get(dev, "sys_clkout2");
336 if (IS_ERR(sys_clkout2)) {
337 dev_err(dev, "Could not get sys_clkout2\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300338 err = PTR_ERR(sys_clkout2);
339 goto err3;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200340 }
341 /*
342 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
343 * 96 MHz as its parent in order to get 12 MHz
344 */
345 func96m_clk = clk_get(dev, "func_96m_ck");
346 if (IS_ERR(func96m_clk)) {
347 dev_err(dev, "Could not get func 96M clock\n");
Jarkko Nikulae7845392008-08-26 13:32:57 +0300348 err = PTR_ERR(func96m_clk);
349 goto err4;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200350 }
351 clk_set_parent(sys_clkout2_src, func96m_clk);
352 clk_set_rate(sys_clkout2, 12000000);
353
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200354 if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200355 BUG();
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200356 if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200357 BUG();
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200358 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
359 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200360
361 return 0;
Jarkko Nikulae7845392008-08-26 13:32:57 +0300362err4:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200363 clk_put(sys_clkout2);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300364err3:
365 clk_put(sys_clkout2_src);
366err2:
Jarkko Nikula2e747962008-04-25 13:55:19 +0200367 platform_device_del(n810_snd_device);
368err1:
369 platform_device_put(n810_snd_device);
370
371 return err;
Jarkko Nikula2e747962008-04-25 13:55:19 +0200372}
373
374static void __exit n810_soc_exit(void)
375{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200376 gpio_free(N810_SPEAKER_AMP_GPIO);
377 gpio_free(N810_HEADSET_AMP_GPIO);
Jarkko Nikulae7845392008-08-26 13:32:57 +0300378 clk_put(sys_clkout2_src);
379 clk_put(sys_clkout2);
380 clk_put(func96m_clk);
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200381
Jarkko Nikula2e747962008-04-25 13:55:19 +0200382 platform_device_unregister(n810_snd_device);
383}
384
385module_init(n810_soc_init);
386module_exit(n810_soc_exit);
387
388MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
389MODULE_DESCRIPTION("ALSA SoC Nokia N810");
390MODULE_LICENSE("GPL");