blob: c168a64e316230a6234f58fbccf29808290875c9 [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>
32#include <asm/arch/hardware.h>
Jarkko Nikulaf99a6332008-05-15 11:01:36 +020033#include <linux/gpio.h>
Jarkko Nikula2e747962008-04-25 13:55:19 +020034#include <asm/arch/mcbsp.h>
35
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;
49
50static void n810_ext_control(struct snd_soc_codec *codec)
51{
52 snd_soc_dapm_set_endpoint(codec, "Ext Spk", n810_spk_func);
53 snd_soc_dapm_set_endpoint(codec, "Headphone Jack", n810_jack_func);
54
55 snd_soc_dapm_sync_endpoints(codec);
56}
57
58static int n810_startup(struct snd_pcm_substream *substream)
59{
60 struct snd_soc_pcm_runtime *rtd = substream->private_data;
61 struct snd_soc_codec *codec = rtd->socdev->codec;
62
63 n810_ext_control(codec);
64 return clk_enable(sys_clkout2);
65}
66
67static void n810_shutdown(struct snd_pcm_substream *substream)
68{
69 clk_disable(sys_clkout2);
70}
71
72static int n810_hw_params(struct snd_pcm_substream *substream,
73 struct snd_pcm_hw_params *params)
74{
75 struct snd_soc_pcm_runtime *rtd = substream->private_data;
76 struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
77 struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
78 int err;
79
80 /* Set codec DAI configuration */
81 err = codec_dai->dai_ops.set_fmt(codec_dai,
82 SND_SOC_DAIFMT_I2S |
83 SND_SOC_DAIFMT_NB_NF |
84 SND_SOC_DAIFMT_CBM_CFM);
85 if (err < 0)
86 return err;
87
88 /* Set cpu DAI configuration */
89 err = cpu_dai->dai_ops.set_fmt(cpu_dai,
90 SND_SOC_DAIFMT_I2S |
91 SND_SOC_DAIFMT_NB_NF |
92 SND_SOC_DAIFMT_CBM_CFM);
93 if (err < 0)
94 return err;
95
96 /* Set the codec system clock for DAC and ADC */
97 err = codec_dai->dai_ops.set_sysclk(codec_dai, 0, 12000000,
98 SND_SOC_CLOCK_IN);
99
100 return err;
101}
102
103static struct snd_soc_ops n810_ops = {
104 .startup = n810_startup,
105 .hw_params = n810_hw_params,
106 .shutdown = n810_shutdown,
107};
108
109static int n810_get_spk(struct snd_kcontrol *kcontrol,
110 struct snd_ctl_elem_value *ucontrol)
111{
112 ucontrol->value.integer.value[0] = n810_spk_func;
113
114 return 0;
115}
116
117static int n810_set_spk(struct snd_kcontrol *kcontrol,
118 struct snd_ctl_elem_value *ucontrol)
119{
120 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
121
122 if (n810_spk_func == ucontrol->value.integer.value[0])
123 return 0;
124
125 n810_spk_func = ucontrol->value.integer.value[0];
126 n810_ext_control(codec);
127
128 return 1;
129}
130
131static int n810_get_jack(struct snd_kcontrol *kcontrol,
132 struct snd_ctl_elem_value *ucontrol)
133{
134 ucontrol->value.integer.value[0] = n810_jack_func;
135
136 return 0;
137}
138
139static int n810_set_jack(struct snd_kcontrol *kcontrol,
140 struct snd_ctl_elem_value *ucontrol)
141{
142 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
143
144 if (n810_jack_func == ucontrol->value.integer.value[0])
145 return 0;
146
147 n810_jack_func = ucontrol->value.integer.value[0];
148 n810_ext_control(codec);
149
150 return 1;
151}
152
153static int n810_spk_event(struct snd_soc_dapm_widget *w,
154 struct snd_kcontrol *k, int event)
155{
156 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200157 gpio_set_value(N810_SPEAKER_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200158 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200159 gpio_set_value(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200160
161 return 0;
162}
163
164static int n810_jack_event(struct snd_soc_dapm_widget *w,
165 struct snd_kcontrol *k, int event)
166{
167 if (SND_SOC_DAPM_EVENT_ON(event))
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200168 gpio_set_value(N810_HEADSET_AMP_GPIO, 1);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200169 else
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200170 gpio_set_value(N810_HEADSET_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200171
172 return 0;
173}
174
175static const struct snd_soc_dapm_widget aic33_dapm_widgets[] = {
176 SND_SOC_DAPM_SPK("Ext Spk", n810_spk_event),
177 SND_SOC_DAPM_HP("Headphone Jack", n810_jack_event),
178};
179
Mark Brown1a250592008-05-13 14:58:57 +0200180static const struct snd_soc_dapm_route audio_map[] = {
Jarkko Nikula2e747962008-04-25 13:55:19 +0200181 {"Headphone Jack", NULL, "HPLOUT"},
182 {"Headphone Jack", NULL, "HPROUT"},
183
184 {"Ext Spk", NULL, "LLOUT"},
185 {"Ext Spk", NULL, "RLOUT"},
186};
187
188static const char *spk_function[] = {"Off", "On"};
189static const char *jack_function[] = {"Off", "Headphone"};
190static const struct soc_enum n810_enum[] = {
Jarkko Nikula3c172792008-05-13 16:02:04 +0200191 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(spk_function), spk_function),
192 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(jack_function), jack_function),
Jarkko Nikula2e747962008-04-25 13:55:19 +0200193};
194
195static const struct snd_kcontrol_new aic33_n810_controls[] = {
196 SOC_ENUM_EXT("Speaker Function", n810_enum[0],
197 n810_get_spk, n810_set_spk),
198 SOC_ENUM_EXT("Jack Function", n810_enum[1],
199 n810_get_jack, n810_set_jack),
200};
201
202static int n810_aic33_init(struct snd_soc_codec *codec)
203{
204 int i, err;
205
206 /* Not connected */
207 snd_soc_dapm_set_endpoint(codec, "MONO_LOUT", 0);
208 snd_soc_dapm_set_endpoint(codec, "HPLCOM", 0);
209 snd_soc_dapm_set_endpoint(codec, "HPRCOM", 0);
210
211 /* Add N810 specific controls */
212 for (i = 0; i < ARRAY_SIZE(aic33_n810_controls); i++) {
213 err = snd_ctl_add(codec->card,
214 snd_soc_cnew(&aic33_n810_controls[i], codec, NULL));
215 if (err < 0)
216 return err;
217 }
218
219 /* Add N810 specific widgets */
Mark Brown1a250592008-05-13 14:58:57 +0200220 snd_soc_dapm_new_controls(codec, aic33_dapm_widgets,
221 ARRAY_SIZE(aic33_dapm_widgets));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200222
223 /* Set up N810 specific audio path audio_map */
Mark Brown1a250592008-05-13 14:58:57 +0200224 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
Jarkko Nikula2e747962008-04-25 13:55:19 +0200225
226 snd_soc_dapm_sync_endpoints(codec);
227
228 return 0;
229}
230
231/* Digital audio interface glue - connects codec <--> CPU */
232static struct snd_soc_dai_link n810_dai = {
233 .name = "TLV320AIC33",
234 .stream_name = "AIC33",
235 .cpu_dai = &omap_mcbsp_dai[0],
236 .codec_dai = &aic3x_dai,
237 .init = n810_aic33_init,
238 .ops = &n810_ops,
239};
240
241/* Audio machine driver */
242static struct snd_soc_machine snd_soc_machine_n810 = {
243 .name = "N810",
244 .dai_link = &n810_dai,
245 .num_links = 1,
246};
247
248/* Audio private data */
249static struct aic3x_setup_data n810_aic33_setup = {
250 .i2c_address = 0x18,
251};
252
253/* Audio subsystem */
254static struct snd_soc_device n810_snd_devdata = {
255 .machine = &snd_soc_machine_n810,
256 .platform = &omap_soc_platform,
257 .codec_dev = &soc_codec_dev_aic3x,
258 .codec_data = &n810_aic33_setup,
259};
260
261static struct platform_device *n810_snd_device;
262
263static int __init n810_soc_init(void)
264{
265 int err;
266 struct device *dev;
267
Jarkko Nikula6e132fa2008-06-11 13:47:15 +0100268 if (!(machine_is_nokia_n810() || machine_is_nokia_n810_wimax()))
Jarkko Nikula2e747962008-04-25 13:55:19 +0200269 return -ENODEV;
270
271 n810_snd_device = platform_device_alloc("soc-audio", -1);
272 if (!n810_snd_device)
273 return -ENOMEM;
274
275 platform_set_drvdata(n810_snd_device, &n810_snd_devdata);
276 n810_snd_devdata.dev = &n810_snd_device->dev;
277 *(unsigned int *)n810_dai.cpu_dai->private_data = 1; /* McBSP2 */
278 err = platform_device_add(n810_snd_device);
279 if (err)
280 goto err1;
281
282 dev = &n810_snd_device->dev;
283
284 sys_clkout2_src = clk_get(dev, "sys_clkout2_src");
285 if (IS_ERR(sys_clkout2_src)) {
286 dev_err(dev, "Could not get sys_clkout2_src clock\n");
287 return -ENODEV;
288 }
289 sys_clkout2 = clk_get(dev, "sys_clkout2");
290 if (IS_ERR(sys_clkout2)) {
291 dev_err(dev, "Could not get sys_clkout2\n");
292 goto err1;
293 }
294 /*
295 * Configure 12 MHz output on SYS_CLKOUT2. Therefore we must use
296 * 96 MHz as its parent in order to get 12 MHz
297 */
298 func96m_clk = clk_get(dev, "func_96m_ck");
299 if (IS_ERR(func96m_clk)) {
300 dev_err(dev, "Could not get func 96M clock\n");
301 goto err2;
302 }
303 clk_set_parent(sys_clkout2_src, func96m_clk);
304 clk_set_rate(sys_clkout2, 12000000);
305
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200306 if (gpio_request(N810_HEADSET_AMP_GPIO, "hs_amp") < 0)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200307 BUG();
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200308 if (gpio_request(N810_SPEAKER_AMP_GPIO, "spk_amp") < 0)
Jarkko Nikula2e747962008-04-25 13:55:19 +0200309 BUG();
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200310 gpio_direction_output(N810_HEADSET_AMP_GPIO, 0);
311 gpio_direction_output(N810_SPEAKER_AMP_GPIO, 0);
Jarkko Nikula2e747962008-04-25 13:55:19 +0200312
313 return 0;
314err2:
315 clk_put(sys_clkout2);
316 platform_device_del(n810_snd_device);
317err1:
318 platform_device_put(n810_snd_device);
319
320 return err;
321
322}
323
324static void __exit n810_soc_exit(void)
325{
Jarkko Nikulaf99a6332008-05-15 11:01:36 +0200326 gpio_free(N810_SPEAKER_AMP_GPIO);
327 gpio_free(N810_HEADSET_AMP_GPIO);
328
Jarkko Nikula2e747962008-04-25 13:55:19 +0200329 platform_device_unregister(n810_snd_device);
330}
331
332module_init(n810_soc_init);
333module_exit(n810_soc_exit);
334
335MODULE_AUTHOR("Jarkko Nikula <jarkko.nikula@nokia.com>");
336MODULE_DESCRIPTION("ALSA SoC Nokia N810");
337MODULE_LICENSE("GPL");