2 * Copyright (C) 2009, Yauhen Kharuzhy <jekhor@gmail.com>
4 * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <sound/core.h>
20 #include <sound/pcm.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/jack.h>
24 #include <linux/gpio.h>
25 #include <linux/workqueue.h>
27 #include "../codecs/jzcodec.h"
28 #include "jz4740-pcm.h"
29 #include "jz4740-i2s.h"
31 #include <asm/mach-jz4740/board-n516.h>
34 N516_SPEAKER_AUTO = 0,
39 static int n516_speaker_mode;
40 static struct snd_soc_codec *n516_codec;
41 static struct work_struct n516_headphone_work;
43 static void n516_ext_control(void)
48 switch (n516_speaker_mode) {
50 snd_soc_dapm_enable_pin(n516_codec, "Speaker");
52 case N516_SPEAKER_OFF:
53 snd_soc_dapm_disable_pin(n516_codec, "Speaker");
55 case N516_SPEAKER_AUTO:
56 if (snd_soc_dapm_get_pin_status(n516_codec, "Headphone"))
57 snd_soc_dapm_disable_pin(n516_codec, "Speaker");
59 snd_soc_dapm_enable_pin(n516_codec, "Speaker");
65 /* signal a DAPM event */
66 snd_soc_dapm_sync(n516_codec);
69 static int n516_speaker_event(struct snd_soc_dapm_widget *widget,
70 struct snd_kcontrol *ctrl, int event)
72 int on = !SND_SOC_DAPM_EVENT_OFF(event);
74 gpio_set_value(GPIO_SPEAKER_ENABLE, on);
79 static void n516_headphone_event_work(struct work_struct *work)
84 static int n516_headphone_event(struct snd_soc_dapm_widget *widget,
85 struct snd_kcontrol *ctrl, int event)
87 /* We can't call soc_dapm_sync from a event handler */
88 if (event & (SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD))
89 schedule_work(&n516_headphone_work);
93 static const struct snd_soc_dapm_widget n516_widgets[] = {
94 SND_SOC_DAPM_SPK("Speaker", n516_speaker_event),
95 SND_SOC_DAPM_HP("Headphone", n516_headphone_event),
96 SND_SOC_DAPM_MIC("Mic", NULL),
99 static const struct snd_soc_dapm_route n516_routes[] = {
100 {"Mic", NULL, "MIC"},
101 {"Speaker", NULL, "LOUT"},
102 {"Speaker", NULL, "ROUT"},
103 {"Headphone", NULL, "LOUT"},
104 {"Headphone", NULL, "ROUT"},
107 static const char *n516_speaker_modes[] = {"Auto", "Off", "On"};
108 static const struct soc_enum n516_speaker_mode_enum =
109 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(n516_speaker_modes), n516_speaker_modes);
111 static int n516_get_speaker_mode(struct snd_kcontrol *kcontrol,
112 struct snd_ctl_elem_value *ucontrol)
114 ucontrol->value.integer.value[0] = n516_speaker_mode;
118 static int n516_set_speaker_mode(struct snd_kcontrol *kcontrol,
119 struct snd_ctl_elem_value *ucontrol)
121 if (n516_speaker_mode == ucontrol->value.integer.value[0])
124 n516_speaker_mode = ucontrol->value.integer.value[0];
129 static const struct snd_kcontrol_new n516_controls[] = {
130 SOC_ENUM_EXT("Speaker Function", n516_speaker_mode_enum,
131 n516_get_speaker_mode, n516_set_speaker_mode),
134 #define N516_DAIFMT (SND_SOC_DAIFMT_I2S | \
135 SND_SOC_DAIFMT_NB_NF | \
136 SND_SOC_DAIFMT_CBM_CFM)
138 static int n516_codec_init(struct snd_soc_codec *codec)
141 struct snd_soc_dai *cpu_dai = codec->socdev->card->dai_link->cpu_dai;
142 struct snd_soc_dai *codec_dai = codec->socdev->card->dai_link->codec_dai;
146 snd_soc_dapm_nc_pin(codec, "LIN");
147 snd_soc_dapm_nc_pin(codec, "RIN");
149 ret = snd_soc_dai_set_fmt(codec_dai, N516_DAIFMT);
151 dev_err(codec->dev, "Failed to set codec dai format: %d\n", ret);
155 ret = snd_soc_dai_set_fmt(cpu_dai, N516_DAIFMT);
157 dev_err(codec->dev, "Failed to set cpu dai format: %d\n", ret);
161 ret = snd_soc_dai_set_sysclk(codec_dai, JZCODEC_SYSCLK, 111,
164 dev_err(codec->dev, "Failed to set codec dai sysclk: %d\n", ret);
168 ret = snd_soc_add_controls(codec, n516_controls,
169 ARRAY_SIZE(n516_controls));
171 dev_err(codec->dev, "Failed to add controls: %d\n", ret);
176 ret = snd_soc_dapm_new_controls(codec, n516_widgets,
177 ARRAY_SIZE(n516_widgets));
179 dev_err(codec->dev, "Failed to add dapm controls: %d\n", ret);
183 ret = snd_soc_dapm_add_routes(codec, n516_routes, ARRAY_SIZE(n516_routes));
185 dev_err(codec->dev, "Failed to add dapm routes: %d\n", ret);
189 snd_soc_dapm_sync(codec);
194 static struct snd_soc_dai_link n516_dai = {
196 .stream_name = "JZCODEC",
197 .cpu_dai = &jz4740_i2s_dai,
198 .codec_dai = &jz_codec_dai,
199 .init = n516_codec_init,
202 static struct snd_soc_card n516_card = {
204 .dai_link = &n516_dai,
206 .platform = &jz4740_soc_platform,
209 static struct snd_soc_device n516_snd_devdata = {
211 .codec_dev = &soc_codec_dev_jzcodec,
214 static struct platform_device *n516_snd_device;
216 static struct snd_soc_jack n516_hp_jack;
218 static struct snd_soc_jack_pin n516_hp_pin = {
220 .mask = SND_JACK_HEADPHONE,
223 static struct snd_soc_jack_gpio n516_hp_gpio = {
224 .gpio = GPIO_HPHONE_DETECT,
225 .name = "Headphone detect",
226 .report = SND_JACK_HEADPHONE,
227 .debounce_time = 100,
230 static int __init n516_add_headphone_jack(void)
234 ret = snd_soc_jack_new(&n516_card, "Headphone jack",
235 SND_JACK_HEADPHONE, &n516_hp_jack);
239 ret = snd_soc_jack_add_pins(&n516_hp_jack, 1, &n516_hp_pin);
243 ret = snd_soc_jack_add_gpios(&n516_hp_jack, 1, &n516_hp_gpio);
248 static int __init n516_init(void)
252 n516_snd_device = platform_device_alloc("soc-audio", -1);
254 if (!n516_snd_device)
257 ret = gpio_request(GPIO_SPEAKER_ENABLE, "Speaker enable");
259 pr_err("n516 snd: Failed to request SPEAKER_ENABLE GPIO(%d): %d\n",
260 GPIO_SPEAKER_ENABLE, ret);
264 gpio_direction_output(GPIO_SPEAKER_ENABLE, 0);
265 INIT_WORK(&n516_headphone_work, n516_headphone_event_work);
267 platform_set_drvdata(n516_snd_device, &n516_snd_devdata);
268 n516_snd_devdata.dev = &n516_snd_device->dev;
269 ret = platform_device_add(n516_snd_device);
271 pr_err("n516 snd: Failed to add snd soc device: %d\n", ret);
272 goto err_unset_pdata;
275 ret = n516_add_headphone_jack();
276 /* We can live without it, so just print a warning */
278 pr_warning("n516 snd: Failed to initalise headphone jack: %d\n", ret);
283 platform_set_drvdata(n516_snd_device, NULL);
284 /*err_gpio_free_speaker:*/
285 gpio_free(GPIO_SPEAKER_ENABLE);
287 platform_device_put(n516_snd_device);
291 module_init(n516_init);
293 static void __exit n516_exit(void)
295 snd_soc_jack_free_gpios(&n516_hp_jack, 1, &n516_hp_gpio);
296 gpio_free(GPIO_SPEAKER_ENABLE);
297 platform_device_unregister(n516_snd_device);
299 module_exit(n516_exit);
301 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
302 MODULE_DESCRIPTION("ALSA SoC N516 Audio support");
303 MODULE_LICENSE("GPL v2");